简体   繁体   English

SAPUI5模型加载

[英]SAPUI5 model loading

Is there some way how to know when model is loaded? 有什么方法可以知道何时加载模型?

Example: 例:

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

In this case the model instance is defined, but it contains empty object, because no data is loaded. 在这种情况下,定义了模型实例,但是它包含空对象,因为没有数据被加载。

If I wrap the console.log method with: 如果我用以下命令包装console.log方法:

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

then model data gets loaded correctly, but I would like something more reliable than using setTimeout. 然后可以正确加载模型数据,但是我想要比使用setTimeout更可靠的东西。

The class sap.ui.model.Model has an event called requestCompleted ( link ). sap.ui.model.Model类具有一个名为requestCompletedlink )的事件。 So you can attach a function to that event with the method attachRequestCompleted ( link ): 因此,您可以使用方法attachRequestCompletedlink )将函数附加到该事件:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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