Reputation: 36900
The change in behavior for the Template.foo.rendered
callback in Meteor 0.8.0 means that we don't get to automatically use the rendered callback as a way to manipulate the DOM whenever the contents of the template change. One way to achieve this is by using reactive helpers as in https://github.com/avital/meteor-ui-new-rendered-callback. The reactive helpers should theoretically help performance by only being triggered when relevant items change.
However, there is now a new problem: the helper no longer has access to the template instance, like the rendered
callback used to. This means that anything used to maintain state on the template instance cannot be done by helpers.
Is there a way to access both the template instance's state as well as use reactive helpers to trigger DOM updates in Blaze?
Upvotes: 9
Views: 10898
Reputation: 2325
In the latest versions you can use more convenient Template.instance()
instead.
Upvotes: 23
Reputation: 75945
Now there's Template.instance()
which allows you to access a template's instance in helpers. e.g
Template.myTemplate.helpers({
myvalue: function() {
var tmpl = Template.instance();
...
}
});
Along with reactiveDict, you could use them to pass values down set in the rendered callback.
Template.myTemplate.created = function() {
this.templatedata = new ReactiveDict();
}
Template.myTemplate.rendered = function() {
this.templatedata.set("myname", "value");
};
Template.myTemplate.helpers({
myvalue: function() {
var tmpl = Template.instance();
return tmpl.templatedata.get('myname');
}
});
Upvotes: 12
Reputation: 36900
This is currently being tracked as "one of the first things to be added" in post 0.8.0 Meteor:
Another related issue is the ability to access data reactively in the rendered callback, which avoids this issue in the first place:
Upvotes: 0