Search code examples
meteormeteor-blazemeteor-helpermeteor-react

Meteor helper doesn't refresh on Reactive var .set()


I update reactive var on autorun. The same reactive var is used in helper. But helper values doesn't refresh by autorun function. My below code will explain you clearly.

Template.home.onCreated(function () {
  var self = this;
  self.ITEMS_INCREMENT = 2;

  self.itemsLimit = new ReactiveVar(2);


});

Template.home.onRendered(function () {
  var self = this;
  this.autorun(function(){
    if( true ){
      self.itemsLimit.set(self.itemsLimit.get()+self.ITEMS_INCREMENT);
      console.log(self.itemsLimit.get()); // set values are fine
    }
  });

});

Template.home.helpers({
testHelper: function(){
  console.log(Template.instance().itemsLimit.get()); // console returns 2 and 4 only. no more update :(
  return true;
}
});

Anything wrong in handling of data or usage? How to make helpers are workable?


Solution

  • Well well well, I don't see where are you going to change the value of reactive variable. It gets changed only ones, when first computation runs in your tracker. So only ones you see the update. The idea of using ReactiveVar is that when you change it, it gets changed inside helper or inside autorun.

    Briefly, what you can do to test it is:

    Template.home.onRendered(function () {
      var self = this;
      Meteor.setInterval(function(){
          self.itemsLimit.set(self.itemsLimit.get()+self.ITEMS_INCREMENT);
        }, 500);
    });