Binding, Context, ContextBinding and BindingContext in ui5

michel luther picture michel luther · Aug 28, 2014 · Viewed 26.5k times · Source

I have been pondering the internals of and relationship between context, contextbinding, and bindingcontext for a few days now and i am not sure whether there is a major misconception on my side. Maybe some of you can help me sort it out. I am putting my assumptions below. I might want to say first that I always work with oData models here.

This is what I believe to understand reading the documentation:

A context is a reference to a data object in a model.

A binding is basically an event provider which (in case of a one way binding) observes the status of a specific context and emits events when it is changed/data loaded ... and therefore allows for registering event handlers for events on that specific context. In terms of programming objects, there are property bindings and list bindings (is this true - or is list binding all that is ever relevant?).

At any rate, my understanding is that a list binding is the model-side of a component's aggregation binding, while a property binding is called property binding both from a component's and a model's point of view (confusing?).

Now what I do not quite get is: The context binding new sap.ui.model.ContextBinding(oModel, sPath, oContext, mParameters?, oEvents?): takes a path and a context as a parameter. I am assuming that this oContext is not exactly the context described above but some metadata on the binding. is this correct? Or is this the definition of thep ath which the path parameter is relative to?

What also seems weird is when you want to create a context itself new sap.ui.model.Contextabov(oModel, sPath, oContext) takes a context again. I believe that this is just an unfortunate naming thing i am looking at, but I am not quite sure.

Then there is contextbinding and bindingcontext. I'd assume that contextBinding is the binding to a specific context as described e. And a bindingcontext is the meta data regarding a context- or list binding.

From a programming point of view, I do not understand why the following works:

  • create list binding to context via model.bindList() passing a path only.
  • attach change-event handler to binding
  • call get_contexts() on binding
  • receive data in change event handler (and see the oData-property filled in the model).

and there seems to be no way of doing the same for a property binding which i'd assume I can generate via model.bindProperty(). I can generate the binding, but the binding I receive seems to have no handle to actually fetch data.

I hope the ramble explains my problem. In case you ask : what do you want to do? I actually do not want to do anything with it, I just do not quite understand how this works. Binding to ui controls and so forth works just fine, but I'd prefer to really understand what is underneath the hood. I have been reading debug files and unit tests a bit, but discussing it with you guys seems a great way as well.

If this is unclear I'll happily add anything that helps.

Cheers Michel

Answer

Haojie picture Haojie · Aug 29, 2014

your questions are answered below. Hope it helps.

  1. Now what I do not quite get is: The context binding new sap.ui.model.ContextBinding(oModel, sPath, oContext, mParameters?, oEvents?): takes a path and a context as a parameter. I am assuming that this oContext is not exactly the context described above but some metadata on the binding. is this correct? Or is this the definition of thep ath which the path parameter is relative to?

the oContext is the context you mentioned above, to be precise, is sap.ui.model.Context.

  1. What also seems weird is when you want to create a context itself new sap.ui.model.Context(oModel, sPath, oContext) takes a context again. I believe that this is just an unfortunate naming thing i am looking at, but I am not quite sure.

I guess the documentation here confused you. Actually, sap.ui.model.Context only takes oModel and sPath as parameters. The following code is what i get from sap-ui-core.js. You can see the JSDoc part about parameters is actually inconsistent with the code. Maybe there is some kind of typo there.

    /**
    * Constructor for Context class.
    *
    * @class
    * The Context is a pointer to an object in the model data, which is used to 
    * allow definition of relative bindings, which are resolved relative to the
    * defined object.
    * Context elements are created either by the ListBinding for each list entry
    * or by using createBindingContext.
    *
    * @param {sap.ui.model.Model} oModel the model
    * @param {String} sPath the path
    * @param {Object} oContext the context object
    * @abstract
    * @public
    * @name sap.ui.model.Context
    */
    var Context = sap.ui.base.Object.extend("sap.ui.model.Context", 
    /** @lends sap.ui.model.Context.prototype */  {
    
    constructor : function(oModel, sPath){
    
        sap.ui.base.Object.apply(this);
    
        this.oModel = oModel;
        this.sPath = sPath;
    
    },
    
    metadata : {
        "abstract" : true,
      publicMethods : [
            "getModel", "getPath", "getProperty", "getObject"
        ]
    }
    
    });
  1. From a programming point of view, I do not understand why the following works:
  • create list binding to context via model.bindList() passing a path only.
  • attach change-event handler to binding
  • call get_contexts() on binding
  • receive data in change event handler (and see the oData-property filled in the model).

and there seems to be no way of doing the same for a property binding which i'd assume I can generate via model.bindProperty(). I can generate the binding, but the binding I receive seems to have no handle to actually fetch data.

Actually you can also attachChange event to sap.ui.model.PropertyBinding, and you can call getValue() to get the data.