v2.ODataModel:更喜欢哪个API?“绑定元素"还是“阅读"? [英] v2.ODataModel: which API is more preferred? "bindElement" or "read"?
问题描述
我设置了view.setModel(model)
,获取视图的模型,并请求model.read("/entitySet('10000')")
.然后用 /entitySet('10000')/properties
填充模型.
I set the view.setModel(model)
, get the model for the view, and request for model.read("/entitySet('10000')")
.
The model is then filled up with /entitySet('10000')/properties
.
但是很难将它们分配给视图字段,因为现在在视图中,
不起作用.它必须是
.
But it is difficult to assign them to view fields, as now in the view, <Text text="{property}">
doesn't work. It has to be <Text text="{/entitySet('10000')/property}">
.
另一方面,如果我将视图的上下文绑定设置为 "/entitySet('10000')"
,则
代码> 将开始工作.
On the other hand, if I set view's context binding to "/entitySet('10000')"
, then <Text text="{property}">
would start working.
哪一种是首选方法?什么时候使用 .read
?
Which one is the preferred method? When to use .read
?
推荐答案
如果我想直接在绑定上下文中使用 OData 调用的结果,我几乎从不使用 .read
.我唯一一次使用 .read
是如果我想在对结果进行任何操作之前对其进行操作.
I almost never use .read
if I want to use the results from an OData call directly in a binding context. The only time I use .read
is if I want to manipulate the results before doing anything with them.
例如从 sdk 中查看此示例:https://ui5.sap.com/#/entity/sap.ui.table.Table/sample/sap.ui.table.sample.OData
Look at this example from the sdk for instance: https://ui5.sap.com/#/entity/sap.ui.table.Table/sample/sap.ui.table.sample.OData
这种绑定的语法与 read 类似,但在事件上有一些不同,并且根据您要绑定的内容有几种不同类型的方法.例如绑定到视图使用 bindElement
:
Syntax on this kind of binding is similar to read but with a few differences in events, and a few different types of methods depending on what you want to bind. Binding to a view for instance uses bindElement
:
this.getView().bindElement("/entitySet('1000')");
此后,该特定实体上的字段可以作为<Text text="{property}"/>
访问.
After this, fields on that particular entity can be accessed as <Text text="{property}" />
.
以下是我当前的一个应用程序的示例,其中包含事件和其他一些调用参数:
Here's an example from one of my current apps with events and some other call parameters:
this.getView().bindElement({
path: `/Orders('${currentOrderNumber}')`,
parameters: {
expand: 'Texts'
},
events: {
dataRequested: _ => this.getView().setBusy(true),
dataReceived: data => {
if (!this.getView().getBindingContext()) {
// navigate to `Not Found` view
}
},
change: _ => this.getView().setBusy(false)
}
});
对于一个表,略有不同,因为它取决于您希望绑定的聚合,例如
For a table, it's slightly different, since it depends on the aggregation you wish to bind, such as
oTable.bindRows({
path: "properties"
});
与以下相同:
<Table rows="{properties}" />
这篇关于v2.ODataModel:更喜欢哪个API?“绑定元素"还是“阅读"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!