6

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.

alexwlchan
  • 5,699
  • 7
  • 38
  • 49
niceman
  • 251
  • 3
  • 16

1 Answers1

6

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);
});
herrlock
  • 1,454
  • 1
  • 13
  • 16