Backbone view DOM element removed

后端 未结 3 523
我在风中等你
我在风中等你 2021-02-06 16:39

I keep reading about the Backbone.js zombie (or memory leak) problem. Basically you have to unbind and remove the element from the DOM when you no longer need it to make sure al

3条回答
  •  闹比i
    闹比i (楼主)
    2021-02-06 17:33

    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. There are, however, 2 types of events, 1) Backbone events, 2) the events that are bound to your DOM elements with jQuery.

    So instead of your:

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

    Do this:

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

    You are now removing Backbone events as well; and the this.remove on a view will call $(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.

提交回复
热议问题