JSONModel更改的事件处理程序?

时间:2019-04-10 19:33:15

标签: sapui5

说,有一个sap.m.table,其项绑定到JSON模型-“ / rows”。在sap.m.table布局之外,还有一个工具栏,其中包含“添加”按钮,用于向表中添加行。 “添加”按钮使用模型的setProperty方法将行添加到表中。现在,要求是在JSON模型“ / rows”长度达到10时禁用“添加”按钮。我们如何创建一个处理程序以观察JSON模型的“ / rows”属性的变化? https://sapui5.netweaver.ondemand.com/1.52.22/#/api/sap.ui.model.Model/events/propertyChange指出 当前仅使用原因sap.ui.model.ChangeReason.Binding触发该事件,当对属性绑定的值进行两种更改时将触发该事件。 这意味着调用JSONModel的setProperty()时不会触发propertyChange的eventHandler。有没有办法可以观察到JSONModel属性的更改-在这种情况下,就是JSONModel的“ / rows”属性?

1 个答案:

答案 0 :(得分:1)

好吧,我可以想到几种方法来实现这一目标

1。标准视图绑定+格式器:

查看

...
<Button text="Add" press="onPressAdd" enabled="{path: '/rows', formatter: '.isAddEnabled'}" />
...

控制器

Controller.prototype.isAddEnabled = function(rows) {
    return rows && rows.length < 10;
}

2。表达式绑定(纯xml)

...
<Button text="Add" press="onPressAdd" enabled="{= ${/rows/length} &lt; 10 }" />
...

3。 JSONPropertyBinding(纯JavaScript)

您可以在 JSONModel 上调用 bindProperty 来创建可以观察到的更改的属性绑定:

https://sapui5.hana.ondemand.com/#/api/sap.ui.model.Model/methods/bindProperty https://sapui5.hana.ondemand.com/#/api/sap.ui.model.json.JSONPropertyBinding

Controller.prototype.onInit = function() {
    var model = this.getMyJsonModel();
    var button = this.getView().byId("myButtonId");

    model.bindProperty("/rows").attachChange(function(event) {
        button.setEnabled(event.getSource().getValue().length < 10);
    })
}