Search code examples
javascriptmeteoriron-router

Meteor / Iron router - Get parent data in an {{#each}} loop


Considering:

Template.tasks.helpers({
    equals: function() {
        // var data = ?
        return data.test == this;
    }
});

And:

<template name="tasks">
    <ul>
    {{#each elements}}
        <li>
            {{this}} {{#if equals}}Equals!{{/if}}
        </li>
    {{/each}}
    </ul>
</template>

And for example:

Router.route('tasks', {
    data: function () {
        var example = {
            test: 42,
            elements: [1, 2, 42, 100]
        }
        return example;
    }
});

How can I get the parent data context (the one passed on iron router's route() method with the data field) ?

In the example above, how can we display 42 - Equals! ?


Solution

  • I believe you're looking for Template.parentData():

    Template.tasks.helpers({
        equals: function() {
            var data = Template.parentData();
            return data.test == this;
        }
    });
    

    Here is a live example: http://meteorpad.com/pad/LHxyDYkmNQB9H2Ezy/ParentData