Gabriel Gartz
Gabriel Gartz

Reputation: 2870

How to remove a rendered template in MeteorJS?

I have added a template rendering it dynamically:

var myTemplate = Meteor.render(Meteor.template.foo);
document.body.appendChild(myTemplate);

Now I want to remove this template from DOM, but this template have a list of elements.

The Meteor render, use a documentFragment, so I can't reuse the myTemplate variable because it doesn't have the content anymore.

What is the correct way to remove the template element contents from DOM using MeteorJS?

Upvotes: 0

Views: 284

Answers (1)

richsilv
richsilv

Reputation: 8013

Under the current rendering engine, I believe this will clean your injected template up satisfactorily if you've given the top level element in it the id myTemplate:

var thisNode = $('#myTemplate')[0];
Spark.finalize(thisNode);
$(thisNode).remove();

As you probably know, Spark is about to go away forever though, so this is only good for the time being. I suspect that just .removeing the element will be good enough in Blaze, but I can't really back that up with any evidence as I haven't made the switch yet...

Upvotes: 1

Related Questions