walmartlabs/thorax — pull request #96 by eastridge (2013-01-11 21:49:46+00:00)
Title: Destroy stale helper views

@kpdecker

This is a tricky problem I hadn't noticed until @Candid pointed out the problems with `preserveCollectionElement`. Presently each time `render` is called, new `HelperView` instances will be generated. In master the stale `HelperView` instances will not be wiped out, in this branch they will be properly cleaned up.

I would much prefer that we not destroy old `HelperView`s and re-embed them ala the `view` helper. But consider the following view (imagine JS has multiline strings):

Handlebars.registerViewHelper('test', function(){});

var view = new Thorax.View({
template: """
{{test}}
{{#if key}}
{{test}}
{{/if}}
"""
});

view.render();
view.key = true;
view.render();

After the first render we have one child view. After the second render we would have two. I can't figure out how to embed the first view back where it was. Instead we nuke it and re-generate the helper views.

Perhaps the solution is to keep things as is, and re-generate on `render`. We should then be aware that in this case:

var view = new Thorax.View({
collection: new Thorax.Collection(),
template: '{{#collection}}blah{{/collection}}'
});
view.render();
view.render();

that the view will be rendered 3 times, and the embedded collection view will be generated 3 times. Once on `setCollection` and twice on `render`. Also remember that I've added `ensureRendered` which will only render if the view has not been rendered once already.

Also note that for views that render their own collections without the collection helpers the collection will not be re-rendered on `render()` which is consistent with 1.x behavior.