简体   繁体   中英

Is there a way to listen silent events on backbonejs?

I'm trying to listen backbone's model's ´changed´-event for certain attribute, like

this.listenTo(this, 'change:someAttr', this.eventListener);

However, it is changed silently, so normal event listening does not work. Is there a way to listen backbone's silent events?

The only way I can think of is via some sort of "polling", ie checking over and over via a setInterval.

initialize: function() {
    this.listenSilent();
},
listenSilent: function() {
    var startVal = this.get('myAttr');
    var that = this;
    var silentListener = setInterval(function() {
        if (that.get('myAttr') !== startVal) {
            that.trigger('myevent'); // any custom event name that you can listen to
            stopInterval(silentListener);
            that.listenSilent();
        }
    }, 100);
}

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