2013-12-20 81 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/

相关问题