Can't seem to cleanup detached DOM elements

五迷三道 提交于 2019-12-02 18:22:01

Is there a reason why you use this.$el.contents().remove() instead of this.$el.empty()?

Using this.$el.empty() in that jsFiddle of yours seemed to remedy the detached NodeList.

A few notes memory profiling:

  • watch http://www.youtube.com/watch?v=L3ugr9BJqIs
  • Use the 3 snapshots method, 2 is not enough. What does that mean?
    • Start fresh, incognito mode and refreshed
    • Take snapshot (forced GC will happen every time you take snapshot)
    • Do something, take snapshot (gc)
    • Do something similar, take snapshot (gc)
    • Compare snapshot 2 with snapshot 1, find deltas with +
    • Choose Summary and Objects allocated between Snapshots 1 and 2 for snapshot 3
    • Look for stuff that you found comparing 2 and 1 that shouldn´t be there. These are the leeks

I have found cases where jQuery seems to leek because they save the current jQuery object in .prevObject when doing some operations like calling .add(). Maybe that call to .contents() do some funky magic.

So I eventually fixed this problem (quite some time ago) by doing two things:

  1. Ditching jQueryUI in favor of Bootstrap
  2. Changing the closeTab function (see the original jsFiddle) to the following:

    closeTab: function (e) {
        e.stopPropagation();
        e.preventDefault();
        this.model.collection.remove(this.model);
        this.close();
    }
    

In that snippet, stopPropagation and preventDefault are really what stopped this element from remaining detached (and accumulating on subsequent adds/removes) in the DOM. To summarize: this problem was created by not calling stopPropagation/preventDefault on the event object after it was triggered. I've updated the jsFiddle so that it correctly removes the tab elements and for posterity, here's a screenshot of the result:

As you can see, none of the tab elements are remaining detached after clicking the "X" to close them. The only detached elements are the ones that come from jsFiddle's interface.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!