2013-12-20 79 views
0

我有兩個模型m1m2具有相同屬性attr的變化。我想將這兩個模型分配到一個集合c1Then I want to listen to changes in 'attr' inside the collection and determine in which model 'attr' is changed.我試圖在一些教程中給出的集合中附加一個事件偵聽器,但是在更改模型的屬性'attr'時,它並沒有聽到這個變化。解決此問題的最佳方法是什麼?是否有可能在Backbone中聽到像這樣的變化?收聽BackBone中多個模型的變化的一個集合

+0

你能告訴你做了什麼?這將有助於解釋你錯過了什麼。 – nikoshr

+0

兩個模型'm1'和'm2'是否具有相同'Model'?如果不將它們添加到一個「集合」中聽起來不是一個好方法。 –

回答

0

,如果你設置你的看法是這樣它應該工作:

var Vue = Backbone.View.extend({ 
    initialize: function() { 
     //attach a collection to the view 
     this.collection = collection1; 

     //fires when the 'attr' attribute changes in any model in the collection 
     this.listenTo(this.collection, 'change:attr', this.itHasChanged); 
    }, 
    itHasChanged: function(e) { 
     alert('the model with id: ' + e.get('id') + ' has changed'); 
     console.log(e) 
    } 
}); 

下面是一個例子:http://jsfiddle.net/Ss2BM/

相關問題