1

I need to check in a child template the value of a reactive dict variable, which was created in the parent template.

<template name="parent">
    {{ > child }}
</template>

<template name="child">
    {{#if something}}
        checked
    {{/if}}
</template>

This is how I initialize a reactive dict variable to the parent template:

Template.parent.onCreated(function() {
    this.blabla = new ReactiveDict();
});

So for the parent template this helper is working:

Template.parent.helpers({
    anything: function(type) { return (Template.instance().blabla.get('foo') === 'bar') ? true : false; }
});

But it won't work for the child:

Template.child.helpers({
    something: function() { return (Template.instance().blabla.get('foo') === 'bar') ? true : false; }
});

So how can I check the value of the variable in the child helper?

user3142695
  • 15,844
  • 47
  • 176
  • 332

1 Answers1

2

At its core, this question is about how to find a template's parent instance. I'd recommend reading all of the answers here.

I'll build on the idea presented by Sacha, with this example:

html

<template name="parent">
    {{ > child parentHelper=parentHelper}}
</template>

<template name="child">
  {{#if parentHelper}}
    <h1>pass</h1>
  {{else}}
    <h1>fail</h1>
  {{/if}}
</template>

js

Template.parent.onCreated(function() {
    this.dict = new ReactiveDict();
    this.dict.set('foo', 'bar');
});

Template.parent.helpers({
  parentHelper: function() {
    return Template.instance().dict.equals('foo', 'bar');
  }
});

If that exact pattern doesn't work in your code, you can try one of the others from the linked question above.

David Weldon
  • 63,632
  • 11
  • 148
  • 146