I have:
Template.myTemplate.helpers({
reactiveVar: new ReactiveVar
});
How can I access reactiveVar
from onCreated to set it up?
Template.restaurantEdit.onCreated(function() {
// Access helpers.reactiveVar from here to set up
// My goal is to set up data to reactiveVar, ex:
helpers.reactiveVar = this.data.someData;
});
I found there is protected __helpers: this.view.template.__helpers
But is there any Meteor 's nice way to access helpers? or which is the Meteor's way to set up reactiveVar from loaded data
You basically don't access helpers in Meteor directly. If you want to use scoped reactivity with ReactiveVar, you should do it this way:
Template.restaurantEdit.onCreated(function() {
//define all your reactive variables here
this.reactiveVar = new ReactiveVar('default value');
});
Template.restaurantEdit.helpers({
reactiveVar: function() {
//access reactiveVar template variable from onCreated() hook
return Template.instance().reactiveVar.get();
}
});
Template.restaurantEdit.events({
'click yourselector': function(evt, template) {
template.reactiveVar.set('new value');
}
});
Read more about scoped reactivity here: https://dweldon.silvrback.com/scoped-reactivity
You should set up the reactive variable in your onCreated()
block and then access it via a helper
, not vice-versa.
Template.restaurantEdit.onCreated(function(){
this.foo = new ReactiveVar;
});
Template.restaurantEdit.helpers({
foo: function(){
return Template.instance().foo.get();
}
});
The helper will update whenever your ReactiveVar
changes.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.