Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
  }
});
like image 537
user3783327 Avatar asked Nov 07 '14 10:11

user3783327


1 Answers

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

like image 189
herrlock Avatar answered Nov 05 '22 02:11

herrlock