Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 693
niceman Avatar asked Feb 16 '15 14:02

niceman


1 Answers

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);
});
like image 167
herrlock Avatar answered Sep 30 '22 06:09

herrlock