javascript 不再需要视图后取消委托事件的最佳方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10734210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The best way to undelegate events once a view is no longer needed
提问by Preslav Rachev
Is it a bad practice to call undelegateEvents()
in the view remove()
method? Why wasn't it included by default by the backbone guys?
调用undelegateEvents()
视图remove()
方法是一种不好的做法吗?为什么骨干人员默认不包含它?
I realized I am falling into so many binding issues, when simply reinitializing a view variable. Although undelegateEvents()
is being called automatically when a new view is created, it is trying to undelegate events for the newly instantiated view, not the previous one. Therefore, unless manually calling it every time, ghost event callbacks remain alive and screw up my apps.
我意识到在简单地重新初始化一个视图变量时,我陷入了很多绑定问题。尽管undelegateEvents()
在创建新视图时会自动调用,但它正在尝试为新实例化的视图而不是前一个视图取消委托事件。因此,除非每次都手动调用它,否则幽灵事件回调仍然存在并搞砸我的应用程序。
What's the best way to handle this?
处理这个问题的最佳方法是什么?
采纳答案by JMM
Is it a bad practice to call
undelegateEvents()
in the viewremove()
method?
调用
undelegateEvents()
视图remove()
方法是一种不好的做法吗?
It's not necessary unless you're implementing your own remove()
and you don't call Backbone.View.remove()
or this.$el.remove()
. That's if you're using jQuery, at least. Calling remove()
on a Backbone view will call jQuery.remove()
which will remove all of the DOM event listeners anyway.
除非您自己实现remove()
并且不调用Backbone.View.remove()
或 ,否则没有必要this.$el.remove()
。至少在您使用 jQuery 时是这样。调用remove()
Backbone 视图将调用jQuery.remove()
它无论如何都会删除所有 DOM 事件侦听器。
I realized I am falling into so many binding issues, when simply reinitializing a view variable.
我意识到在简单地重新初始化一个视图变量时,我陷入了很多绑定问题。
A lot of people seem to use Backbone.Events like it's some kind of magic that they don't need to clean up after, e.g.:
很多人似乎使用 Backbone.Events 就像是某种魔法,他们不需要清理之后,例如:
var View = Backbone.View.extend( {
initialize : function ( options ) {
// `on()` or `bind()`
this.model.on( 'something', this.render, this );
}
} );
See my answer on Delegating events to a parent view in Backbone
请参阅我关于将事件委派给 Backbone 中的父视图的答案
Is it possible that the ghost event issues you're experiencing are with Backbone events and not DOM events?
您遇到的幽灵事件问题是否可能与 Backbone 事件而不是 DOM 事件有关?
If you keep the model object around but want to get rid of that view object or its Backbone event registrations, you have to do view.model.off( null, null, this );
. You have to unbind the events that you've registered on any external objects. If you want, you could override Backbone.View.remove()
and do it there, but by default that method is just shorthand for view.$el.remove()
.
如果您保留模型对象,但想要摆脱该视图对象或其 Backbone 事件注册,则必须执行view.model.off( null, null, this );
. 您必须取消绑定在任何外部对象上注册的事件。如果需要,您可以覆盖Backbone.View.remove()
并在那里执行,但默认情况下该方法只是view.$el.remove()
.