Blaze: Access parent ReactiveDict , Meteor 1.3

Hello,

I have the following parent template

Template.parentTemplate.onCreated(function parentTemplateOnCreated() {
   this.state = new ReactiveDict();
   this.state.setDefault({
      foo: 1
   });
}

How can I now access this.state in a child template without using Sessions?

Thanks
Shibbn

1 Like

After looking at the blaze todos example app it seems like this is one way to do it

{{> childTemplate (getReactiveDictHelper)}}

helpers:

getReactiveDictHelper(){
    const reactiveDict = Template.instance().state;
    return reactiveDic;
  }

This doesn’t work — at least in meteor 1.6.
Any other way to access reactive var of the parent template from a child template?

I’m using aldeed:template-extension for this purpose:

https://atmospherejs.com/aldeed/template-extension

It allows to access parent properties using templateInstance.get().

The helper approach still works in 1.6. I tend to pass it as a named argument though:

Template.parentTemplate.onCreated(function () {
   this.state = new ReactiveDict();
   this.state.setDefault({
      foo: 1
   });
});
Template.parentTemplate.helpers({
  state() { return Template.instance().state; }
});
{{> childTemplate parentState=state }}
Template.childTemplate.onCreated(function () {
  this.data.parentState.set('childInitialized', true);
});