我有两个模型m1
和m2
,其中相同的属性attr
会发生变化。我想将这两个模型分配给一个集合c1
。 Then I want to listen to changes in 'attr' inside the collection and determine in which model 'attr' is changed.
我尝试在某些教程中添加一个事件监听器,但是在更改模型的属性“attr”时,它没有收听更改。解决这个问题的最佳方法是什么?是否可以在Backbone中收听此类更改?
答案 0 :(得分: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/