setBindingContext() 的用法和元素绑定的区别是什么? [英] What's the usage of setBindingContext() and the difference from element binding?

查看:21
本文介绍了setBindingContext() 的用法和元素绑定的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 OpenUI5 演示套件的 1.5.2.3 定义绑定路径部分:

In the 1.5.2.3 Defining a Binding Path section of OpenUI5 demokit:

在聚合绑定的情况下,聚合的每个条目都存在上下文,或者可以使用 setBindingContext 方法为控件显式设置上下文.

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 演示套件的 1.5.3.3 元素绑定部分:

In the 1.5.3.3 Element Binding section of OpenUI5 demokit:

元素绑定允许将元素绑定到模型数据中的特定对象,这将创建绑定上下文并允许在控件及其所有子项内进行相对绑定.

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,输出
但是,如果我将 oPanel.setBindingContext("/nameinfo"); 更改为 oPanel.bindElement("/nameinfo");,它会起作用,为什么?

It seems to me that the two techniques actually do the same thing. They both create a binding context for a control so that bindings of the containing controls will resolve relatively to it. But what's the difference between them? In what scenario will either of them come into play?

The setBindingContext doesn't work in the following code:
https://jsbin.com/bigope/edit?html,output
However, if I change oPanel.setBindingContext("/nameinfo"); to oPanel.bindElement("/nameinfo");, it works, why?

推荐答案

setBindingContext 要求您传递这样的上下文:

setBindingContext requires you to pass a Context like this:

oPanel.setBindingContext(new sap.ui.model.Context(oModel, "/nameinfo"));

这两者之间的区别是概念上的.绑定上下文用作该控件或其子控件中所有绑定(对于该模型)的父上下文.它只保存对所用模型的引用、路径(的一部分)和可选的另一个父上下文.它在创建相对绑定时使用.

The difference between those two is conceptual. The Binding Context is used as a parent context for all bindings (for that model) in that Control or its children. It only holds a reference to the used model, (a part of) the path and optional another parent context. It is used when creating relative bindings.

另一方面,bindElement 方法的行为与其他所有 bind* 方法一样.它创建一个绑定(在本例中为 ContextBinding),允许更改事件、数据绑定等.此外,创建的 ContextBinding 还用作其他绑定的 BindingContext,就像使用 setBindingContext 添加的 Context 一样.

The bindElement method on the other hand behaves like every other bind* method. It creates a binding (in this case, a ContextBinding) which allows change events, data binding, etc. Additionally the created ContextBinding also serves as a BindingContext for other bindings, just like a Context added with setBindingContext would do.

一点也不令人困惑,对吧;)?

Not confusing at all, right ;)?

阅读ManagedObject 可能会帮助您更好地了解内部结构.(bindObject = bindElement)

Reading the code for ManagedObject might help you to understand the internals better. (bindObject = bindElement)

这篇关于setBindingContext() 的用法和元素绑定的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆