How to use ReactiveVar in template?


#1

I try use ReactiveVar in template like this

// html
<template name="hello">
   Reative: {{reactiveVal.get}}
   Helper: {{helperVal}}
</template>

// js
Template.hello.onCreated(function(){
   this.reactiveVal = new ReactiveVar(5);
});

Template.hello.helpers({
   helperVal: function(){
      return this.reactiveVal.get();
   }
})

But, don’t work.


#2

Instead of using this.reactiveVal, you need to use Template.instance().reactiveVal.

Template.hello.helpers({
   helperVal: function(){
      return Template.instance().reactiveVal.get();
   }
})

Also I am not sure if this: {{reactiveVal.get}} will work in your template… but the helper should work once you update it with the above.

<template name="hello">
   Reative: {{reactiveVal.get}}
   Helper: {{helperVal}}
</template>

#3

Very thanks for your reply, I try to use {{reactiveVal.get}} because I see it on peerlibrary:blaze-components.