I am inserting templates using UI.render() and UI.insert().
When I tried to remove the template I inserted, it seems to stay in memory and the destroyed method is not called.
According to the documentation, it should clean up property if I use jquery to remove the element.
I'm testing it using the following code:
test.html:
<head>
<title>removeTest</title>
<style>
#content {
height: 500px;
width: 500px;
background-color: gray;
}
</style>
</head>
<body><div id="content"></div></body>
<template name="foo"><div id="{{id}}">Foo</div></template>
test.js:
if (Meteor.isClient) {
UI.body.events({
"click": function(event) {
var instance = UI.renderWithData(Template.foo, { });
UI.insert(instance, $("#content")[0]);
}
});
Template.foo.created = function() {
this.data.id = "handle";
var self = this;
this.x = setInterval(function() { console.log("running...") }, 1000);
setTimeout(function() { $("#handle").remove() }, 1000);
};
Template.foo.destroyed = function() {
// never called.
clearTimeout(this.x);
};
}
What did I do wrong?
Thanks.
"Uncaught TypeError: Cannot set property 'id' of null"
. TheUI.render
function sets thedata
context tonull
. You could probably make it work withUI.renderWithData
, but I suspect there is a more elegant solution using handlebars and letting Meteor handle the lower level rendering as normal. – AcidifyUI.render
. You should be able to use a helper function to return a cursor from a local collection to drive the rendering of the templates with an{{#each}}
block. Then your click event can just insert into the local collection, and yourdestroyed
function will be called when the associated document is removed from the collection (by the timeout). – Acidify{{#each}}
block. Thedestroyed
function will be called in that case. The original issue still exists thou, and I think it is a bug in current implementation. I have filed an issue on github regarding that. – Lore$("#handle").remove()
is intend to remove the template. It only removes the first child of the template. Please see @pent answer to see if that solves your problem. Here is the meteor issue: https://github.com/meteor/meteor/issues/2021 – Lore