简体   繁体   English

删除了骨干视图DOM元素

[英]Backbone view DOM element removed

I keep reading about the Backbone.js zombie (or memory leak) problem. 我一直在阅读Backbone.js僵尸(或内存泄漏)问题。 Basically you have to unbind and remove the element from the DOM when you no longer need it to make sure all events are removed as well. 基本上,当您不再需要它以确保所有事件都被删除时,您必须解除绑定并从DOM中删除该元素。

Now, I have a single page app with a few containers: 现在,我有一个带有几个容器的单页应用程序:

<div id="page1"></div>
<div id="page2"></div>

and add my underscore.js templates to these placeholders. 并将我的underscore.js模板添加到这些占位符。 I have a model per page like: 我每页有一个模型,如:

HomeView = Backbone.View.extend({
  el: '#page1'
)}

Now, when I click on an element on that page I navigate to another Backbone.js view: 现在,当我点击该页面上的一个元素时,我导航到另一个Backbone.js视图:

clicked: function(ev){
  $(this.el).remove(); // <-- this is the problem
  $(this.el).unbind();
  App.navigate('page/2', true);
}

This works fine but... I removed the page1 element from the DOM so when I use the back button to go to the previous page my element is gone and there is nothing to attach the HTML to. 这工作正常但是...我从DOM中删除了page1元素,所以当我使用后退按钮转到上一页时,我的元素消失了,没有任何内容可以附加HTML。

I probably don't understand how to link Backbone.js views with the DOM... should I keep the element with the risk of memory leaks? 我可能不明白如何将Backbone.js视图与DOM链接...我应该保持元素存在内存泄漏的风险吗?

Thanks! 谢谢!

In regard to losing the page1 element in your page, and therefore not being able to populate the item with HTML, I did the following. 关于丢失页面中的page1元素,因此无法使用HTML填充项目,我执行了以下操作。

Instead of using: 而不是使用:

this.remove();

... which removes the element entirely, and then try to figure out how to add it back, I use jQuery's: ...完全删除元素,然后尝试弄清楚如何添加它,我使用jQuery:

$(this).empty;

This empties all child elements, text, and data and event handlers. 这会清空所有子元素,文本,数据和事件处理程序。 More info at: http://api.jquery.com/empty/ 更多信息请访问: http//api.jquery.com/empty/

In fact, I use all of the following, which may be overkill but we'll see: 事实上,我使用以下所有内容,这可能有点矫枉过正,但我​​们会看到:

this.undelegateEvents();
$(this).empty;
this.unbind();

Hope this is useful! 希望这很有用!

As the article says, (yes, I've tried his methods before in my own projects), you have to find a way to remove your view's DOM element and unbind the events. 正如文章所说,(是的,我之前在我自己的项目中尝试过他的方法),你必须找到一种方法来删除视图的DOM元素并取消绑定事件。 There are, however, 2 types of events, 1) Backbone events, 2) the events that are bound to your DOM elements with jQuery. 但是,有两种类型的事件,1)Backbone事件,2)使用jQuery绑定到DOM元素的事件。

So instead of your: 而不是你的:

$(this.el).remove();
$(this.el).unbind();

Do this: 做这个:

this.remove();
this.unbind();

You are now removing Backbone events as well; 您现在也正在删除Backbone事件; and the this.remove on a view will call $(this.el).remove(); 并且在this.remove上的this.remove将调用$(this.el).remove(); .

However, that is only how to remove a view and not leave zombies. 但是,这只是如何删除视图而不是留下僵尸。 You should consider his methods for showing a view to make this process more automatic. 你应该考虑他的方法来显示一个视图,使这个过程更自动化。 This is all in his article. 这一切都在他的文章中。

Backbone development version(master) now exposes _removeElement() 骨干开发版本(master)现在公开_removeElement()

 remove: function() {
      this._removeElement();
      this.stopListening();
      return this;
    },

Remove this view's element from the document and all event listeners attached to it. 从文档和附加到它的所有事件侦听器中删除此视图的元素。 Exposed for subclasses using an alternative DOM manipulation API. 使用备用DOM操作API暴露给子类。

_removeElement: function() {
      this.$el.remove();
    },

http://backbonejs.org/docs/backbone.html http://backbonejs.org/docs/backbone.html

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

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