openui5 icon indicating copy to clipboard operation
openui5 copied to clipboard

sap.ui.model.Binding attachChange is always fired regardless of the sPath and context of binding

Open cmastrandrea opened this issue 12 years ago • 14 comments

I create a binding to a model using as specific sPath value and binding context, then I attach a change event listener to this binding. It would appear that the change event is fired for any change to the model, that is, the change event does not appear to consider the context or sPath set on the Binding.

OpenUI5 version: Version available from https://openui5.hana.ondemand.com/resources/sap-ui-core.js

Browser/version (+device/version): Not browser related.

Any other tested browsers/devices(OK/FAIL): N/A

URL (minimal example if possible): http://jsbin.com/gaca/56/edit?js,console,output

User/password (if required and possible - do not post any confidential information here): n/a

Steps to reproduce the problem:

  1. Open the provided jsbin and press "Run with JS"
  2. Examine the content of the Console window.

What is the expected result? As the code loops through the array of bindings for the model, it should only fire change events for bindings that have the appropriate sPath value and binding context. In the case of this example, I would have expected to only see the "Obj1 was changed" message in the console when the property was set using the obj1Context.

What happens instead? All bindings receive a change event regardless of their context binding and sPath values.

Any other information? (attach screenshot if possible) For additional consideration, it would be useful to have the change event return the old and new values, although I have not thought through all the implications of such a change, and so should perhaps just be a setting on a binding.

cmastrandrea avatar Apr 30 '14 17:04 cmastrandrea

The class sap.ui.model.Binding is an abstract base class and should not be used directly. It contains the default implementation of change handling which fires a change event on any model change. Subclasses like sap.ui.model.json.JSONPropertyBinding do only fire, if their bound property value was modified.

goligo avatar May 05 '14 13:05 goligo

As @goligo said, using the actual implementation (JSONPropertyBinding in this case) will work as expected. Therefore I am closing this issue.

matz3 avatar May 05 '14 13:05 matz3

Thank you for the update, and I will certainly use the sap.ui.model.json.JSONPropertyBinding. Can we re-open this bug on the documentation as from what I can see there is no reference to this class anywhere in the documentation, neither in the API or in the Developer Guide.

JSONTreeBinding JSONListBinding JSONPropertyBinding

Unless, I am looking in the wrong place for the information https://openui5.hana.ondemand.com/#search.html?q=jsonPropertyBinding.

Regards, Trevor

trevornc avatar May 05 '14 14:05 trevornc

Hmm, right... we are clarifying now whether they are intentionally missing or what's going on there. Hold on...

akudev avatar May 05 '14 15:05 akudev

Hi Trevor,

looks like you didn't look at any documentation or samples regarding databinding, but just try to put together some classes according to the JSDOC. Please do not create instances of context or bindings directly, but use the model API for that purpose, which will return bindings of the correct type which work as expected:

http://jsbin.com/gaca/62/edit

As you might have noticed there are different model implementations, but they share a common interface so that controls do not need to take care of differences in the model implementations. This common interface is defined in the abstract model base classes (in sap.ui.model).

Regards, Malte

goligo avatar May 06 '14 08:05 goligo

Ok, so the concrete "JSON*" classes are intentionally not documented because they should not be used directly. Instead, "PropertyBinding" etc. are documented and they are the common base classes that hide the model type specific details behind the common API.

Another running example which also uses actual controls and gets the bindings from the controls is here: http://jsbin.com/gaca/63/edit

Regards Andreas

akudev avatar May 06 '14 08:05 akudev

Andreas, Malte,

Thank you both for the clarifications. I am not sure why I didn't grasp the binding examples I guess I was trying to understand the process purely using the API documentation.

Please feel free to make this closed.

Regards, Trevor

trevornc avatar May 07 '14 12:05 trevornc

Andreas, Malte,

Is it possible to attach a change listener to part of a model ( in particular a JSONModel ) for example:

   var oData = {
        myObj1 : {
            item : "Item1"
        },
        myObj2 : {
            item : "Item2",
            item2 : {
                subItem : {
                    leafItem : "Aleaf"
                }
            }
        },
        myObj3 : {
            item : "Item3",
            thing : "Thing3"
        }
    };

Is it possible to "attachChange" to /myObj2/item2 so that any changes under this node (such as "leafItem") would fire the event ?

Regards, Trevor

trevornc avatar May 08 '14 16:05 trevornc

Looking at the code more closely, it would appear that using bindProperty to bind to a property that is an object does not work. The problem is in the code show below. When this._getValue() refers to an object, it returns the reference to the object (in the model), this means that later when the value is compared to the model it is always the same.

var ClientPropertyBinding = PropertyBinding.extend("sap.ui.model.ClientPropertyBinding", /** @lends sap.ui.model.ClientPropertyBinding */ {

    constructor : function(oModel, sPath, oContext, mParameters){
        PropertyBinding.apply(this, arguments);
        this.oValue = this._getValue();
    }

A solution would be to detect that the returned value is an object and then clone it using the jQuery.extend functionality.

trevornc avatar May 08 '14 20:05 trevornc

@goligo HI Goligo..

i am trying to track the changes in JSONModel. But i couldn't able to track the data changes.

Here is my jsonmodel data.

{ "sample1": [ {"property1": "1"}, {"property2": "1"} ], "sample2": [ {"property1": "1"}, {"property2": "1"} ]

}

Here, i want to track the "sample1" changes. is there any way of tracking these changes ?

warstick avatar Aug 21 '16 12:08 warstick

Is there a live example that correctly use sap.ui.model.json.JSONPropertyBinding?

StErMi avatar Sep 27 '16 14:09 StErMi

Hi @trevornc , did you ever find a way to fire the event only when a specified node of the model is changed instead of when any part is changed?

fredericDaigle avatar Feb 16 '17 21:02 fredericDaigle

Thanks , whole thread helped alot!!!

subhashchaganti avatar Aug 01 '18 09:08 subhashchaganti

Thinking out of the box, maybe we should ditch the event based reactivity altogether and jump on the modern observability/reactivity wagon by leveraging Proxy. I am in awe when I learn about what Vue can do with method/watch/computed and intend to introduce them to UI5 ego system.

johnmiroki avatar Dec 20 '21 06:12 johnmiroki