James_1x0
James_1x0

Reputation: 931

Emberjs - Accessing a parent inside {{#each}} loop

I've searched and searched and I cannot find the solution to the following question:

How do you access a parent inside an each loop?

So here's the scenario, I need to pass an id of the parent with the action helper within a nested each loop.

{{#each}} {{! iterating the model here (arrayController) }}
    {{#if showingApplicants}} {{! this is set by a button that changes a property inside this particular model object}}
        {{#each applicants}}
            <button {{action addLabel _id}}>Add Label...</button>
        {{/each}}
    {{/if}}
{{/each}}

I have tried doing ../_id and ../../_id. Both of which represent an undefined value. Any clues? ALSO: Is it possible to pass two values in the action helper?

Upvotes: 1

Views: 822

Answers (2)

Miguel Madero
Miguel Madero

Reputation: 1948

You can create variables on your #each blocks so you can refer to them on any context

{{#each x in content}}
    {{#if x.showingApplicants}} 
        {{#each applicant in x.applicants}}
            <button {{action addLabel x.id}}>Add Label...</button>
            {{this.controllerProperty}}
            {{x.parentModelProperty}}
            {{y.childModelProperty}}
        {{/each}}
    {{/if}}
{{/each}}

You could also not name applicant, but if you start by naming the outer context, I would suggest doing it for the nested each as well for consistency and to avoid possible name clashes (between the outer variable and child properties).

Hope this helps

Upvotes: 2

James_1x0
James_1x0

Reputation: 931

The workaround for this issue was to map out the model data to have an id object that contained the parent id and the child id.

Upvotes: -1

Related Questions