-1

Related question: What is the replacement for metadataLoaded in JSON model?


When I use metadataLoaded in sap.ui.model.odata.v2.ODataModel, Everytime _onObjectMatched is called , metadataLoaded can also be fired.:

this.getRouter().getRoute("object").attachPatternMatched(this._onObjectMatched, this);

            _onObjectMatched : function (oEvent) {
                var sObjectId =  oEvent.getParameter("arguments").objectId;
                this.getModel().metadataLoaded().then( function() {
                    //fired every time
                    this._bindView(sObjectId);
                }.bind(this));
            },

But when I use RequestCompleted in sap.ui.model.json.JSONModel, RequestCompleted only fired once when the request data loaded.

this.getModel().attachRequestCompleted(function() {
    //fired only once 
    this._bindView(sObjectId);
});

I am curious why? I think metadata should also be loaded only once?


Update:

console

Actually, I have already find that metadataLoaded returns a promise (I should have known that when I saw then()), but just as @Nabi point out, I am not familiar with promise. I should have dig deeper before I ask this question.

The aim of both question is to find an elegant and official replacement for metadataLoaded in JSON model, should I combine them or sth.?

Tina Chen
  • 2,010
  • 5
  • 41
  • 73

1 Answers1

1

_onObjectMatched() is called everytime your route matches. In there you always call this.getModel().metadataLoaded().then(...) while metadataLoadad() returns a promise which resolves when the metadata is loaded (in the past or in the future). This happens everytime because this.getModel().metadataLoaded().then(...) is called everytime.

Probably it's a good idea to have a look at Promises, I'm sure you'll get the idea. In fact, make sure you understand the difference between events and Promises...

Nabi
  • 2,536
  • 1
  • 16
  • 18
  • Thanks, I use `this.getModel().attachRequestCompleted().pSequentialImportCompleted.then()`, and it solved my problem. (Although I did not find `pSequentialImportCompleted` in doc, just know it is returns a promise... – Tina Chen Jun 21 '17 at 08:43
  • Ok... Then be aware that you are using some private API which might cause issues in future.. ;-) – Nabi Jun 21 '17 at 08:45
  • @TinaChen `pSequentialImportCompleted` is still kept in the source code but if anyone is using v1.64 or above, the public API `dataLoaded` should be used: https://stackoverflow.com/a/61879725/5846045 – Boghyon Hoffmann May 18 '20 at 22:17