11

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
  }
});
user3783327
  • 616
  • 8
  • 30
  • 60
  • 2
    How about overriding sap.ui.model.odata.ODataModel.prototype.setProperty? – cschuff Nov 07 '14 at 20:35
  • I checked the documentation, and it's really dumb but there doesn't seem to be a change event. I think the answer from @cschuff is the way to go. – Jonathan Benn Nov 19 '14 at 14:04

2 Answers2

16

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

herrlock
  • 1,454
  • 1
  • 13
  • 16
  • Problem with `sap.ui.model.Binding` is every time we get new object and again we need to register event, thats why it is calling same function two times, is there any ways for getting existing `sap.ui.model.Binding` object, – Prashant Gurav Jun 15 '15 at 11:08
  • There is a method `someObject.getBinding();` (https://openui5.hana.ondemand.com/#docs/api/symbols/sap.ui.base.ManagedObject.html#getBinding). I think you can attach events there, too. – herrlock Jun 15 '15 at 12:47
  • Hello, where in the contoller are you setting this attach? – alexd May 24 '17 at 10:07
0

I would propose to do it instead of the model (as you say it is called multiple times), call it in every control which has binding, using property "change", where you should provide your check method.

Denis
  • 167
  • 3
  • 20