I have this code:
Template.temp.rendered = function () {
console.log('temp rendered');
}
Which logs only when the website is initialized.
But i do something like this:
more = function () {
Meteor.subscribe('Videos', Session.get('more_info'));
}
When i call more(); it does not log the "temp rendered" even though the template dom gets updated with the new documents. Also tried something like:
Template.temp.rerendered = function () {
console.log('temp re rendered');
}
It does not work; How do I know if A template is rerendered ?
For the moment i'm doing something like this
$('#list').bind("DOMSubtreeModified",function(){
console.log('template modified'}
//this logs like 200 times, every time the template is re rendered
)
How can I do it in the meteor way ?
list Template:
<template name="list">
{{#each list}}
<a id="{{_id}}" href="/{{category}}/{{_id}}" title="{{vTitle}}">
{{vTitle}}
</a>
{{/each}}
</template>
Helpers:
Template.list.helpers({
list : function () {
return Videos.find({},{sort : {date : -1}});
}
})
Tried(not working):
Template.list.rendered = function () {
this.autorun(function() {
Videos.find();
console.log('template re rendered');
});
}
Prefered Solution(from @richsilv):
Template.list.rendered = function () {
this.autorun(function() {
Videos.find().count();
console.log('template re rendered');
});
}
Also the solution from @Peppe L-G was good if you need to call a function every time the template is rendered and if you dont want to register an autorun.
Yes, the
renderedcallback is only fired once when the template is originally rendered, not when it changes due to computations on which it depends being invalidated.The Meteoric way to do things is to add a
this.autorunto the rendered callback which is dependent on the same thing that's causing the template to rerender (i.e. afindon theVideoscollection, or whatever). That way you are:autorunfor each source, where necessary).autorunblocks which are declared underthiswhen it's torn down, which obviates the need to stop them manually to avoid having loads of floating, unused autoruns taking up CPU and memory.