我正在尝试通过cllection fetch在视图中执行我的重置事件,但它不起作用......
路由器:
var tablesCollection = new TablesCollection();
var tablesView = new TablesView({ collection: tablesCollection});
tablesCollection.fetch({ reset: true });
我的观点:
initialize: function(){
_.bindAll(this);
this.collection.bind('reset', this.render);
this.collection.bind('add', this.addTable);
this.collection.bind('change', this.changeTable);
this.collection.bind('destroy', this.delTable);
},
render: function() {
this.el.innerHTML = _.template( this.template, { data : _.groupBy( this.collection.toJSON(), 'status')} );
}
如果我重置了,则仍会触发更改事件:fetch in fetch。 我想通过重置事件渲染每个项目,然后通过刚刚编辑过的更改事件。 如果有任何帮助
答案 0 :(得分:2)
Resetting your collection does not remove your event binding. It's just removing the object from the collection.
If another view/server event start to populate your collection again then your view is still binded and still fire events.
First of all I would use .on (backbone event system) instead of .bind
Then if you want to unbind your view from the collection future potential changes use http://backbonejs.org/#Events-off
Or better, if you want to destroy this specific view (and it's still reacting maybe that's your problem) then use http://backbonejs.org/#View-remove
It will automatically unbind all your events.
I hope it helps
edit:
reset will always trigger 'change' cause it changes all the object of your collection. You should adapt your render function to be triggered on 'change' instead of 'reset'.