2017-07-28 121 views
0

我想聽聽骨幹模型'changed'事件的某些屬性,如有沒有辦法在backbonejs上收聽沉默事件?

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

但是,它被悄悄地發生變化,所以正常情況下聆聽不起作用。有沒有辦法聽取骨幹的沉默事件?

+4

該選項是有原因的。你可能需要猴子補丁骨幹,並禁止選項 –

+0

選項本身是geat。但如果需要上升,能夠手動覆蓋它會很好。 – Tohveli

+0

這意味着你必須去覆蓋源代碼。提供一個選項和另一個選項來抑制先前選項的功能沒有任何意義。如果你不想要功能 - >不要使用它。這個用例聽起來像是在試圖繞過已經構建的應用程序。提供可供選擇的解決方案並非框架工作 - 您必須自己動手...... –

回答

0

我能想到的唯一方法就是通過某種「輪詢」,即通過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); 
} 
相關問題