简体   繁体   English

聚合物,观察全局变量

[英]Polymer, observe global var

I have a multiple custom elements that share the same list of data. 我有多个共享相同数据列表的自定义元素。 I'm trying to fire an event when the global list is changed. 我正在尝试在更改全局列表时触发一个事件。

The folowing code is working on FF and Safari, but not on Chrome. 以下代码适用于FF和Safari,但不适用于Chrome。 Any suggestion for the issue, or maybe a better way to do it? 对这个问题有什么建议,或者有更好的解决方法?

Thanks, 谢谢,

 (function() {
        var _list = null;

        Polymer("dmw-datatypes", {
            ready:function(){
                ...retreiving a list async...
            },
            get list() {
                return _list;
            },
            listReceived: function(json) {
                _list=json;
            },
            listChanged: function(oldValue, newValue) {
                this.fire('list-received');
            }
        });
    })();

This sounds like a symptom of the fact that Object.observe() (which is native in Chrome) doesn't work out-of-the-box with computed properties (like your get list() {} ). 这听起来像是以下事实的征兆: Object.observe() (在Chrome中是本机) 无法通过计算属性(如您的get list() {}开箱即用。 The other browsers use manual dirty-checking to polyfill this behavior, so they work fine. 其他浏览器使用手动脏检查来填充此行为,因此它们可以正常工作。 Basically, you're going to need to create your own Object.observe() notifier 基本上,您将需要创建自己的Object.observe()通知程序

var notifier = Object.getNotifier(this); 

and notify observers when you update _list using notifier.notify() . 并在您使用notifier.notify()更新_list时通知观察者。 The above link gives an example of this. 上面的链接给出了一个例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM