问题
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