See if the variable has changed in Meteor

I found a meteorite variable observer and I use it like this:

Template.restaurantMap.onCreated(function() {
  this.location = ReactiveVar;

  this.watch("location", function(value){
    console.log('value changed', value);
  });
});

      

It works great. But is there a way for Meteor to watch ReactiveVar?

I only need to look at one ReactiveVar, not the entire list attached to the Template. And I need to watch it separately from the Meteor template, helpers, etc.

I need my own call in case a variable changes.

+3


source to share


2 answers


You can just use autorun , which is a built-in way to create a custom reactive context (a function that runs whenever a reactive variable changes). Here's an example:



Template.restaurantMap.onCreated(function() {
  this.location = new ReactiveVar;

  var self = this;
  this.autorun(function() {
    return console.log('location changed!', self.location.get());
  })
});

      

+8


source


Any ReactiveVar inside a reactive computation is automatically scanned by the meteorite. Reactive computing includes:

  • Template helpers
  • Iron hooks for router
  • Tracker.autorun
  • this.autorun


For me, template helpers that are reactive are almost always sufficient. When not, I use Tracker.autorun or this.autorun (from within onRendered templates).

+1


source







All Articles