Why metadataLoaded can be fired multiple times?

故事扮演 提交于 2019-12-25 20:07:42

问题


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:

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.?


回答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...



来源:https://stackoverflow.com/questions/44670186/why-metadataloaded-can-be-fired-multiple-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!