Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the usage of setBindingContext() and the difference from element binding?

Tags:

sapui5

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

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.

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.

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?

like image 380
Zhengquan Bai Avatar asked Jul 08 '15 03:07

Zhengquan Bai


1 Answers

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.

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 ;)?

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

like image 76
masch Avatar answered Oct 22 '22 18:10

masch