简体   繁体   中英

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
  }
});

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

我建议这样做而不是模型(正如你所说的多次调用),在每个具有绑定的控件中调用它,使用属性“change”,你应该提供你的检查方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM