SAPUI5: How to get an event notification in case of ODataModel changes?

user3783327 picture user3783327 · Nov 7, 2014 · Viewed 17.5k times · Source

I am searching for an event that will be thrown if the ODataModel (at client side) will be changed. Problem is, that in my application are lots of different fields that are able to edit the model. In case of a model change I would have a function registered that enables a "Save" button. The "Save" button will call the submitChanges() of the model (I use the TwoWayBinding mode).

Currently I only detected the "hasPendingChanges()" method, but no event I could register.

What is the suggested solution to handle this problem?

To handle the change in each "Input" control looks not to be a nice way, because it's easy to forgot some fields (at least if someone else will maintain the code).

My current solutions looks similar to this now:

sap.ui.model.odata.ODataModel.extend("MyModel", {
  setProperty : function(sPath, oValue, oContext) {
  sap.ui.model.odata.ODataModel.prototype.setProperty.apply(this, [sPath, oValue, oContext]);
  // do something here
  }
});

Answer

herrlock picture herrlock · Dec 4, 2014

You can use sap.ui.model.Binding.attachChange()

var binding = new sap.ui.model.Binding(model, "/", model.getContext("/"));
binding.attachChange(function() {
    saveButton.setVisible(true);
    saveButton.setEnabled(true);
    //or anything else
});

The function is called every time the model changes, eg. by calling model.setProperty(key, value).

https://openui5.netweaver.ondemand.com/#docs/api/symbols/sap.ui.model.Binding.html