setBindingContext() 的用法和与元素绑定的区别是什么?
What's the usage of setBindingContext() and the difference from element binding?
在 OpenUI5 dekit 的 1.5.2.3 定义绑定路径 部分:
A context exists either for each entry of the aggregation in case of aggregation binding or can be set explicitly for a control by using the setBindingContext method.
在 OpenUI5 dekit 的 1.5.3.3 元素绑定 部分:
Element binding allows to bind elements to a specific object in the model data, which will create a binding context and allow relative binding within the control and all of its children.
在我看来,这两种技术实际上做的是同一件事。它们都为控件创建绑定上下文,以便包含控件的绑定将相对于它解析。但是它们之间有什么区别呢?它们在什么情况下会发挥作用?
setBindingContext 在以下代码中不起作用:
https://jsbin.com/bigope/edit?html,output
但是,如果我将 oPanel.setBindingContext("/nameinfo"); 更改为 oPanel.bindElement("/nameinfo"); ,有效,为什么?
setBindingContext
要求您传递这样的上下文:
oPanel.setBindingContext(new sap.ui.model.Context(oModel, "/nameinfo"));
两者之间的区别是概念上的。
绑定上下文用作该控件或其子项中所有绑定(对于该模型)的父上下文。它只包含对所用模型的引用、(部分)路径和可选的另一个父上下文。它在创建相对绑定时使用。
另一方面,bindElement
方法的行为与所有其他 bind* 方法一样。
它创建一个绑定(在本例中为 ContextBinding
),允许更改事件、数据绑定等。
此外,创建的 ContextBinding
还用作其他绑定的 BindingContext,就像使用 setBindingContext 添加的 Context 一样。
一点也不混乱,对吧 ;)?
阅读 ManagedObject 的代码可能有助于您更好地理解内部结构。 (bindObject = bindElement)
在 OpenUI5 dekit 的 1.5.2.3 定义绑定路径 部分:
A context exists either for each entry of the aggregation in case of aggregation binding or can be set explicitly for a control by using the setBindingContext method.
在 OpenUI5 dekit 的 1.5.3.3 元素绑定 部分:
Element binding allows to bind elements to a specific object in the model data, which will create a binding context and allow relative binding within the control and all of its children.
在我看来,这两种技术实际上做的是同一件事。它们都为控件创建绑定上下文,以便包含控件的绑定将相对于它解析。但是它们之间有什么区别呢?它们在什么情况下会发挥作用?
setBindingContext 在以下代码中不起作用:
https://jsbin.com/bigope/edit?html,output
但是,如果我将 oPanel.setBindingContext("/nameinfo"); 更改为 oPanel.bindElement("/nameinfo"); ,有效,为什么?
setBindingContext
要求您传递这样的上下文:
oPanel.setBindingContext(new sap.ui.model.Context(oModel, "/nameinfo"));
两者之间的区别是概念上的。 绑定上下文用作该控件或其子项中所有绑定(对于该模型)的父上下文。它只包含对所用模型的引用、(部分)路径和可选的另一个父上下文。它在创建相对绑定时使用。
另一方面,bindElement
方法的行为与所有其他 bind* 方法一样。
它创建一个绑定(在本例中为 ContextBinding
),允许更改事件、数据绑定等。
此外,创建的 ContextBinding
还用作其他绑定的 BindingContext,就像使用 setBindingContext 添加的 Context 一样。
一点也不混乱,对吧 ;)?
阅读 ManagedObject 的代码可能有助于您更好地理解内部结构。 (bindObject = bindElement)