Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn on uncontained top-level expressions #202

Open
andrejewski opened this issue May 24, 2017 · 2 comments · May be fixed by #308
Open

Warn on uncontained top-level expressions #202

andrejewski opened this issue May 24, 2017 · 2 comments · May be fixed by #308
Assignees

Comments

@andrejewski
Copy link
Contributor

Currently, there is no way for top-level expressions to be correctly bound to a parent container when written in a plain template. Components do not need a warning, neither do any templates provided a parentNode or nodeList to render inside when passed to can-view-live/live.html().

For examples, these templates should warn:

{{#if username}}<p>Hello {{username}}</p>{{/if}}
<div>Hello CanJs</div>

<!-- this is still top-level -->
{{#if info}}<p>Info</p>{{/if}}

I'm unsure what the best warning message would be. Maybe Top-level template expressions cannot be bound in a performant manner, please place "${given expression}" within an element. Please see ${link to better explanation because there definitely should be one}.

There are technical limitations to how nodes are transferred from a DocumentFragment to the main document which makes this an unsolvable bug for now /cc @justinbmeyer.

@justinbmeyer
Copy link
Contributor

var view = stache('{{key}}');
var nodeList = [];
var frag = view( {}, {}, nodeList );

body.appendChild(frag);
// remove elements 
nodeList.unregister();

@bmomberger-bitovi
Copy link
Contributor

bmomberger-bitovi commented Sep 15, 2017

After discussion with Justin, I learned that it's not a performance problem and it is about the inability to tear down properly without an element to hitch to.

If you create a Stache with a top level HTML element, it can tear down bindings associated with itself because the root element can listen for a mutation that removes it. Without this as the top level, a Stache that's rendered and placed into the document outside of other can-view-callbacks rendered DOM trees will not be able to tear down its bindings when the text nodes at the top level are removed, and a memory leak could result. This isn't a problem if you don't ever intend to remove that stache or its ancestor from the page, but this is not the only use case.

So we need some resources to help people make staches that can tear down if removed, if they're not just a top level element (i.e. they contain text nodes or magic tags not contained in an element). This should take the form of docs with a recipe at the very minimum (and we can point to the docs in the warning message), a helper function that abstracts the top level nodelist and teardown function that would need to be created is better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants