I always liked the new way of encapsulating reactive vars inside the template instead of using global variables (Session) by specifying it in the onCreated phase
Template.myTemplate.onCreated(function(){
var instance = this;
instance.someReactiveVar = new ReactiveVar(false);
instance.autorun(function(){
var someReactiveVar = instance.someReactiveVar.get();
instance.subscribe('collection', someReactiveVar);
});
});
Template.myTemplate.helpers({
helper: function() {
var instance = Template.instance();
return instance.someReactiveVar.get();
});
Template.myTemplate.events({
'click button': function(event,instance) {
instance.someReactiveVar.set( 'value' );
}
});
etc.
Unfortunately this way you can never modify a value from outside myTemplate
.
Today I had this epiphany that Template.myTemplate
is a global variable, so I figured out to do the following
Template.myTemplate.someReactiveVar = new ReactiveVar(false);
Template.myTemplate.onCreated(function(){
var instance = this;
instance.autorun(function(){
var someReactiveVar = instance.view.template.someReactiveVar.get();
instance.subscribe('collection', someReactiveVar);
});
});
Template.myTemplate.helpers({
helper: function() {
var instance = Template.instance();
return instance.view.template.someReactiveVar.get();
});
Template.myTemplate.events({
'click button': function(event,instance) {
instance.view.template.someReactiveVar.set( 'value' );
}
});
But now I can modify that value from anywhere
Template.otherTemplate.helpers({
helper: function() {
Template.myTemplate.someReactiveVar.set( 'value' );
}
});
This allowed me to clean up my code a lot.
I thought I’ld share. What do you think?