您可以在 Backbone View 中实现自定义 delegateEvents()
和 undelegateEvents()
。
Backbone.View 构造函数自动调用 delegateEvents。我认为当您使用 Backbone.View.prototype.remove 删除 View 时会调用 undelegateEvents,但事实并非如此。
那么,手动执行此操作的最佳方式是什么?我已经用这段代码覆盖了 remove()
View 方法:
Backbone.View.prototype.remove = function() {
var remove = Backbone.View.prototype.remove;
if (this.undelegateEvents) {
this.undelegateEvents();
}
return remove.apply(this, arguments);
};
它有效,但我不知道这是否是最佳选择。我应该怎么做?
最佳答案
由于 mu 太短建议,问题的真正答案
which is the best way to do this manually?
是不。事件绑定(bind)到 DOM 元素,如果这些元素消失,绑定(bind)也会消失。 undelegateEvents
旨在用于您没有删除 DOM 元素,但仍想取消它的事件绑定(bind)的情况(例如,当您设置 View 的新元素)。
关于javascript - 自定义 Backbone.View undelegateEvents(),它是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14144445/