我试图创建两个视图并在不同情况下更改集合。我不知道如何设置this.collection.bind,因此它会在每次集合更改时引发事件呈现。主干不发射呈现事件
有3种情况,我希望视图BusinessListView
火render
this.businesslist.collection = new Businesses([{ name: '1'}, { name: '2' }]);
this.businesslist.set();
这就要求this.collection = new Businesses([{ name: '3'}, { name: '4' }]);
this.search_location = new SearchLocation();
这是不同的看法,然后把收集来查看BusinessListView
我曾期待看到数据在控制台上1和2,它不起作用。如果手动添加.render(),我可以看到集合已更改。你能解释一下这是如何工作的吗?
UPDATE
感谢Alex这是完全可行的解决方案:
http://jsfiddle.net/feronovak/RAPjM/
var App = {
run: function() {
this.businesslist = new BusinessListView();
this.businesslist.collection = new Businesses([{ name: '1'}, { name: '2' }]);
// this.businesslist.render(); // uncomment to see collection change
this.businesslist.set();
this.search_location = new SearchLocation();
}
};
Business = Backbone.Model.extend({});
Businesses = Backbone.Collection.extend({
model: Business
});
BusinessListView = Backbone.View.extend({
initialize: function(options) {
this.collection = new Businesses();
this.collection.bind("reset", this.render(), this);
},
render: function() {
console.log(this.collection.toJSON());
},
set: function()
{
this.collection = new Businesses([{ name: '3'}, { name: '4' }]);
// this.render(); // uncomment to see collection change
}
});
SearchLocation = Backbone.View.extend({
el: "#search",
initialize: function() {
this.sendData();
},
sendData: function() {
// Send [{ name: '5'}, { name: '6' }] to this.businesslist = new Businesses([{ name: '5'}, { name: '6' }]);
}
});
$(document).ready(function(e) {
App.run();
});
不改变收集调用复位事件?我希望如果我手动更改集合(仅在此示例中),则会调用渲染。 – feronovak 2013-03-27 13:20:36
删除我最后的评论,因为它是不正确的,我猜。你的jsfiddle显示渲染方法正在运行。 – Loamhoof 2013-03-27 13:21:40
通过创建一个新对象,侦听器消失。所以你只是不听新的。 – Loamhoof 2013-03-27 13:25:59