简体   繁体   中英

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. 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() {} ). 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

var notifier = Object.getNotifier(this); 

and notify observers when you update _list using notifier.notify() . The above link gives an example of this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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