Access to helpers from onCreated in Meteor

I have:

Template.myTemplate.helpers({
  reactiveVar: new ReactiveVar
});

      

How can I access reactiveVar

from onCreated to customize it?

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 that __helpers is protected: this.view.template.__helpers

But does Meteor have a good way to access helpers? or which is Meteor's way of setting reactiveVar from loadeddata

+3


source to share


2 answers


Basically you don't access the helpers in Meteor directly. If you want to use active reactivity with ReactiveVar you should do it like this:

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');
  }
});

      



Learn more about reactive activity here: https://dweldon.silvrback.com/scoped-reactivity

+4


source


You have to set up a reactive variable in your block onCreated()

and then access it through helper

, not the other way around.

Template.restaurantEdit.onCreated(function(){
  this.foo = new ReactiveVar;
});

Template.restaurantEdit.helpers({
  foo: function(){
    return Template.instance().foo.get();
  }
});

      



The assistant will update whenever yours changes ReactiveVar

.

+1


source







All Articles