I'm instantiating a new ReactiveVar:
Template.myTemplate.onCreated(function() {
this.var1 = new ReactiveVar(0);
}
I'm having a Tracker.autorun force a rerun when the user selects a new year value:
Template.myTemplate.onRendered(function() {
Tracker.autorun(function() {
var year = Session.get('chartYear').toString(); // This session is from another template
// Do stuff
...
Template.instance().var1.set(new data);
}
}
My helpers to rerun the function:
Template.myTemplate.helpers({
function1: function () {
return Template.instance().var1.get();
}
Not sure what's going on but when a year value is selected on first go, everything runs fine. But when the user selects a new year value, a TypeError results:
"TypeError: null is not an object (evaluating 'Template.instance().var1')"
However, if I replace the ReactiveVars with Sessions instead, everything works fine when the user selects different year values. Could anyone help out?
It's not clear which line is throwing that error (if the following doesn't apply post the full error indicating if this is from the helper or onRendered event, etc).
However I suspect this is from the Template.onRendered
event, which requires you to access the template instance from the this
object, like you are in your Template.onCreated
event.
Template.onRendered
, Template.onCreated
, Template.onDestroyed
, the current template instance is the this
objectTemplate.instance()
'click #myButton'(event, instance)
, the current template instance is the second argument passed to the event handler. (Template.instance()
also works here, but convention is to name the second argument 'instance', and use in instead)So try changing your onRendered
function to this:
Template.myTemplate.onRendered(function() {
templateInstance = this;
Tracker.autorun(function() {
var year = Session.get('chartYear').toString(); // This session is from another template
// Do stuff
...
templateInstance.var1.set(newData);
}
}