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);
});
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With