简体   繁体   中英

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:

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.

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

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