SAPUI5模型加载

SAPUI5 model loading

有什么方法可以知道模型何时加载?

示例:

sap.ui.controller("myapp.MyViewController", {

    onInit: function() {
        this.router = sap.ui.core.UIComponent.getRouterFor(this);
        this.router.attachRoutePatternMatched(this._handleRouteMatched, this);
        this.model = sap.ui.getCore().byId("app").getModel("mymodel");
    },

    onAfterRendering: function() {
        console.log(this.model);
    }
...

在本例中,模型实例已定义,但它包含空对象,因为未加载任何数据。

如果我将 console.log 方法包装为:

setTimeout(function() {
    console.log(sap.ui.getCore().byId("app").getModel("mymodel"));
}, 0);

然后模型数据被正确加载,但我想要比使用 setTimeout 更可靠的东西。

class sap.ui.model.Model has an event called requestCompleted (link). So you can attach a function to that event with the method attachRequestCompleted (link):

this.model.attachRequestCompleted(function(oEvent){
    var model = oEvent.getSource();
    console.log(model);
});