backbone.js视图事件不会被破坏

时间:2013-03-08 21:53:47

标签: events backbone.js view click

我正在尝试在不再需要视图时销毁视图/取消绑定所有事件。我所做的是:

view.$el.removeData().unbind(); 
view.undelegateEvents();
view.remove();

视图被破坏,我无法再看到DOM中的关联元素,但事件似乎仍然存在。我正在使用Chrome开发人员工具并检查内存使用情况,我发现每次渲染视图然后销毁它时,事件监听器都会上升。

我尝试通过执行以下操作输出视图事件:

this.$el.data("events"); 

但是这给了我不确定的。

有什么想法吗?

谢谢。

1 个答案:

答案 0 :(得分:2)

这篇优秀的文章描述了如何防止Backbone中的内存泄漏:http://andrewhenderson.me/tutorial/how-to-detect-backbone-memory-leaks/

他破坏视图的完整例程如下:

this.unbind(); // Unbind all local event bindings
this.model.unbind( 'change', this.render, this ); // Unbind reference to the model
this.options.parent.unbind( 'close:all', this.close, this ); // Unbind reference to the parent view

this.remove(); // Remove view from DOM

delete this.$el; // Delete the jQuery wrapped object variable
delete this.el; // Delete the variable reference to this node