简体   繁体   English

触发更改事件,而在骨干视图中重置

[英]Change event fired instead reset in backbone view

I'm trying to execute my reset event in view through cllection fetch but it doesn't working... 我试图通过抓取来执行我的重置事件,但它不起作用...

router: 路由器:

var tablesCollection = new TablesCollection();
var tablesView = new TablesView({ collection: tablesCollection});
tablesCollection.fetch({ reset: true });

My view: 我的观点:

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')} );
}

Change event is still fired also if I have reset: true in fetch. 如果我已重置,则仍会触发Change事件:fetch中为true。 I want to render each item from collection through reset event then through change event just edited one. 我想通过重置事件然后通过刚刚编辑的更改事件来呈现集合中的每个项目。 Thans for any help 谢谢您的帮助

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 首先,我将使用.on(主干事件系统)代替.bind

Then if you want to unbind your view from the collection future potential changes use http://backbonejs.org/#Events-off 然后,如果您想从集合中解除视图的绑定,则可以使用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 或者更好的是,如果您想破坏这个特定的视图(并且它仍然在做出反应,也许这就是您的问题),请使用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'. 您应该调整渲染功能,使其在“更改”而不是“重置”时触发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM