SAPUI5模型加载

nic*_*man 5 javascript sapui5

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

例:

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);
    }
...
Run Code Online (Sandbox Code Playgroud)

在这种情况下,定义了模型实例,但它包含空对象,因为没有加载数据.

如果我用:包装console.log方法:

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

然后模型数据被正确加载,但我想比使用setTimeout更可靠.

her*_*ock 5

sap.ui.model.Model有一个名为requestCompleted(link)的事件.因此,您可以使用方法attachRequestCompleted(链接)将函数附加到该事件:

this.model.attachRequestCompleted(function(oEvent){
    var model = oEvent.getSource();
    console.log(model);
});
Run Code Online (Sandbox Code Playgroud)