ndemoreau
ndemoreau

Reputation: 3869

meteor spacebars cannot access parent context

Why this does not work?

Template.tests.helpers
  test_value: -> "Test"
  contacts: -> Contacts.find()
  contact: -> Contacts.findOne()

Example 1:

<template name="tests">
    {{test_value}}
    {{#with contact}}
            <p>{{firstname}}</p>
            <p>{{../test_value}}</p>
    {{/with}}
</template>

../test_value inside contacts is not rendered

Example 2

<template name="tests">
    {{>testContact}}
</template>

<template name="testContact">
    {{test_value}}
    {{#with contact}}
        <p>{{firstname}}</p>
        <p>{{../test_value}}</p>
    {{/with}}
</template>

Nothing works in this example. I would assume that if no data context is defined in the testContact template, it would inherit the data context of his parent.

Example 3

<template name="tests">
    {{>testContact}}
</template>

<template name="testContact">
    {{test_value}}
    {{#with ../contact}}
        <p>{{firstname}}</p>
        <p>{{../test_value}}</p>
    {{/with}}
</template>

Doesn't work either.

This is driving me crazy!

EDIT

Example 4

<template name="tests">
    {{#with contact}}
        {{> testContact}}
    {{/with}}
</template>

<template name="testContact">
    <p>{{firstname}}</p>
    <p>{{../test_value}}</p>
    </p>
</template>

Here, I can see that the data context is contact in the testContact template. I would then expect {{../test_value}} to work as test_value is at the same level as contact in my helper but it doesn't.

Example 5

<template name="tests">
    {{#each contacts}}
        {{> testContact}}
    {{/each}}
</template>

<template name="testContact">
    <p>{{firstname}}</p>
    <p>{{../test_value}}</p>

</template>

Upvotes: 1

Views: 1109

Answers (1)

Elie Steinbock
Elie Steinbock

Reputation: 5088

Try example 1 without using ../ before testvalue.

Helpers aren't the same as data context which is why examples 2 and 3 don't work.

If the test value helper function depended on the parents context, then you'd have to use ../ to pass the arguments to the testvalue helper.

Upvotes: 1

Related Questions