justspamjustin / BossView

Manage your Marionette.js views like a boss! Manages events and rendering of sub-views.
http://justspamjustin.github.io/BossView/
MIT License
48 stars 6 forks source link

Call destroy on parent doesn't destroy the subviews. #9

Open SirbyAlive opened 9 years ago

SirbyAlive commented 9 years ago

Problem :

Calling destroy on BossView object doesn't call destroy on subviews, resulting on a potential memory leak.

Subviews' "onDestroy" never get called, and in the Chrome Backbone Debugger we can see that the subviews get destroyed, in a way (?!)(but this debugger is not always accurate), but all their own children are kept alive.


Solution :

I've added destroy as the remove call

destroy: function () {
  Marionette.ItemView.prototype.destroy.apply(this, arguments);
  this._destroySubViews();
},

_destroySubViews: function () {
  _.each(this.initializedSubViews, function(subView) {
    subView.destroy();
  });
}

It does the trick for me but I don't have the time to make the unit test and a pull request. Add if you think it's relevant.

Desp163 commented 8 years ago

Also found this problem when was hunting down huge memory leaks. Here's my simplier fix:

onDestroy: function onDestroy() {
    _.invoke(this.initializedSubViews, 'destroy');
}