3
votes

I have a model I use in a controller and have setup both model change and property change event functions on that model like so:

onInit: function() {
    var oData = {
        "Products": [ 
        {
            "ProductID": 1,
            "ProductName": "Chai",
            "SupplierID": 1,
            "CategoryID": 1,
            "QuantityPerUnit": "10 boxes x 20 bags",
            "UnitPrice": "18.0000",
            "UnitsInStock": 39,
            "UnitsOnOrder": 0,
            "ReorderLevel": 10,
            "Discontinued": false
        }    
    ]
    };

    var oModel = new sap.ui.model.json.JSONModel();
    oModel.setData(oData);
    this.attachModelEventHandlers(oModel);
}

attachModelEventHandlers: function(oModel){
    oModel.attachPropertyChange(this.handlePropertyChanged, this);
    var binding = new sap.ui.model.PropertyBinding(oModel, "/", oModel.getContext("/"));
    binding.attachChange(this.handleDataChanged, this);
},

handlePropertyChanged: function(oEvent) {
    // Do whatever here...
}

handleDataChanged: function(oEvent) {
    // Do whatever here...
}

I have bound the model to a table and everything works fine... if I update properties, the relevant model change and property change events fire.

If I use something like below to update a property however, only the handleDataChanged event function will fire, not the handlePropertyChanged event function:

oModel.setProperty("Products/0/ProductName", "SomeNewValue");

Any idea why? Is my PropertyBinding correct?

1

1 Answers

0
votes

The SAPUI5 documentation suggests a reason parameter which describes the cause of the property value change. When you update the model with two way bindings using some control like the table in your case, the propertyChange event is fired with the reason sap.ui.model.ChangeReason.Binding. The documentation also says that the event is currently only fired when two way changes occur to a value of a property binding. So directly setting the model property would not trigger the event.

You can check the Events > propertyChange in the API Reference for sap.ui.model.Model