引用 this在 Backbone.EventBinder 上发帖,我不知道如何将 EventBinder 与 Backbone View 一起使用(这是最流行的用例)。是否仍然建议按照 this 中的建议向 Backbone.View 原型(prototype)添加 close() 方法和向 View 添加 onClose() 方法邮政?还有一个人在哪里存储活页夹对象,以便可以在关闭时调用 binder.unbindAll()?关闭 subview 的推荐方法是什么(例如,集合上的父 View 在关联模型上具有 subview )。一个工作示例将是对 Backbone.EventBinder 项目的一个很好的补充。
最佳答案
是的,您仍应添加 close
方法来您的意见。 EventBinder 并没有否定 Zombies 帖子所说的任何内容。相反,它有助于更轻松地取消绑定(bind) View 中的所有事件,从而使许多过程自动化。
查看 Marionette.View 源代码,了解如何使用它的示例:
https://github.com/marionettejs/backbone.marionette/blob/master/src/marionette.view.js#L9
https://github.com/marionettejs/backbone.marionette/blob/master/src/marionette.view.js#L16
https://github.com/marionettejs/backbone.marionette/blob/master/src/marionette.view.js#L97
如果您使用 Marionette,则无需添加 close
自己方法,或自己添加事件绑定(bind)器。这是为你处理的。
如果您想将其添加到您自己的 View 中,很简单:
MyView = Backbone.View.extend({
initialize: function(){
// add the event binder
this.eventBinder = new Backbone.EventBinder();
// bind some stuff
this.eventBinder.bindTo(this.model, "change:foo", this.doStuff, this);
},
close: function(){
// ... other stuff
this.eventBinder.unbindAll();
}
});
关于events - 如何在 View 中使用 Backbone.EventBinder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13686165/