Search code examples
backbone.jsmarionettecomposite-view

Marionette CompositeView - Child remains in this.children


I'm currently working with a CompositeView. The problem is, when I call destroy() on one of the child views from the CollectionView part, it still remains in the this.children._view obejct, even do it disappears from the screen. If I try to do anything with it (let's say call destroy() again) than it says, it has already been destroyed, but than why doesn't it get deleted from the object?

Thanks in advance.


Solution

  • Child views are not parent aware

    This may sound annoying, but it's set up like that by design, since a CollectionView child may also exists on its own. As far as I can't tell children of a Collection View are not aware that they are children (except for the case that the model is destroyed [and thus removed from permanent storage]).

    CollectionView.removeChildView can help

    However, Marionette does expose a CollectionView method that is made exactly for what you want to do, CollectionView.removeChildView(view).

    CollectionView.removeChildView will remove the view from the DOM, destroy the view (remove any listeners on the view) and remove it from the children container. Just pass in one of your views.

    If you need some tips on how to trigger CollectionView.removeChildView on an event on your child view, drop me a comment.