为什么 metadataLoaded 可以被多次触发? [英] Why metadataLoaded can be fired multiple times?

查看:69
本文介绍了为什么 metadataLoaded 可以被多次触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相关问题:

其实我已经发现metadataLoaded返回了一个promise(当我看到then()时我应该知道的),但正如@Nabi指出的那样,我对promise并不熟悉.在问这个问题之前,我应该深入挖掘一下.

这两个问题的目的都是在 JSON 模型中为 metadataLoaded 找到一个优雅的官方替代品,我应该将它们结合起来还是……

解决方案

_onObjectMatched() 每次路由匹配时都会被调用.在那里你总是调用 this.getModel().metadataLoaded().then(...)metadataLoadad() 返回一个在元数据加载时解析的承诺(过去或将来).每次都会发生这种情况,因为 this.getModel().metadataLoaded().then(...) 每次都会被调用.

也许看看 Promises 是个好主意,我相信你会明白的.事实上,请确保您了解事件和承诺之间的区别...

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

解决方案

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

这篇关于为什么 metadataLoaded 可以被多次触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆