Meteor.render() should keep fragment updated no matter where it's moved to
If I render, say, a template and append it somewhere to the DOM. And then I move it later, it loses it's automatic updating capabilities.
A similar issue is if I move an element out of a list, and then update it, it won't automatically be updated (and the list will update with the dom node that I took out--which is understandable). I guess the idea is if you manually move a node out of a list, it should maintain its automatic updating capabilities.
In general, the stickiness of the automatic updating capabilities could be a lot better. It's lost to easily.
This is a fundamentally hard problem :) I'm not sure that it can be efficiently solved in the general case.
We do have some limited support for it. If you render a template to DOM nodes (with, say, Meteor.render()), then you can insert the resulting DocumentFragment anywhere you like on the page, and you can even move it around as long as you move the entire DocumentFragment (from start node to end node and everything in between.) However, having removed it, you must immediately reinsert it in its new location, because if it is still out of the document when Meteor.flush() next runs (Meteor.flush() can be called manually, but is also called automatically from the event loop) then it will be cleaned up (its dynamic Spark behaviors will be removed and the destroyed() callbacks if any will be called on its template instances.) This cleanup is definitely a feature rather than a bug -- without it, every DOM node you'd ever created would keep updating until the end of time, even after being removed from the DOM. I don't think we make any guarantees about what will happen if you move just part of a rendering (say, just one tree of subtemplates) rather than the whole thing.
I'm not sure what you're doing but a possible solution would be to call Meteor.render() manually for each template that you want to be able to dynamically relocated. Hope this helps :)
it definitely does. ...on a side note: it would be great if you could do Meteor.render() or Template.name() and return HTML in a template helper. But the fragment returned from Meteor.render() can't be turned into a string, and Template.name() returns a string that has all the Spark tokens.
What I'm trying to do, which I've accomplished via many if statements in a Template, is dynamically render different templates, e.g:
//returns fragment, when string is needed Template.some_name.dynamicTemplate = function() { return Meteor.render(function() { return TemplateSession.get('current_template'); }); };
OR:
//returns tokenized html string, rather than a string with the tokens replaced by their real values Template.some_name.dynamicTemplate = function() { return TemplateSession.get('current_template'); };
The view is something like this:
{{dynamicTemplate}}...instead of {{dynamicTemplate}} i have about 10 if statements backed by a helper to get the view to render, e.g:
{{#if whichTemplate 'template_1'}} {{> template_1}} {{/if}} {{#if whichTemplate 'template_etc'}}
{{> template_etc}}
{{/if}}
Template.some_name.side = whichTemplate(templateName) { if(templateName == Session.get('current_template')) return true; return false; };
This allows me to set the 'current_template' Session variable to automatically adjusted the template nested here. A way more elegant way to do is to simply render the entire template dynamically in a single helper with no 'if' statements as described above. It seems you guys could come up with a simple method to return such a string rather than a Document fragment or tokenized html string.
Assuming I understand you right, here's what I usually do:
In the view:
<template name="mytemplate">
<!-- Note triple brace (turns off HTML escaping) -->
{{{contents}}}
</template>
In JavaScript:
Template.mytemplate.contents = function () {
var whichTemplate = Template[Session.get('currentTemplate')];
return whichTemplate(this);
};
Instead of this you could pass arbitrary data to your dynamically chosen template.
The idea here is, templates are just ordinary functions that return HTML strings. When called inside Meteor.render/Spark.render, they include inline Spark annotations (which will be spliced out and turned into Spark behaviors when Spark actually turns the string into DOM elements.) Either way, they're just HTML and you can combine them just by concatenating the strings.
Does this solve your problem?
Sorry I took so long to respond. This did help. The triple braces was the exact solution I was looking for. Thanks again!