0
votes

I'm having some difficulty with the ember #each statement. I have a user who has a number of subjects associated with him, and I want to display these subjects in a list. This is what I have, but it doesn't render anything:

<script type = "text/x-handlebars" id = "user">

    {{#if deleteMode}}
        <div class="confirm-box">
            <h5>Really?</h5>
            <button {{action "confirmDelete"}}> yes </button>
            <button {{action "cancelDelete"}}> no </button>
        </div>
    {{/if}}

    <h3>Name: {{name}}</h3>
    <h3>Email: {{email}}</h3>
    <h3>Subjects:</h3>
    <ul>
        {{#each subject in this.user}}
            <li>
                {{subject.name}}
            </li>
        {{/each}}
    </ul>

    <button {{action "edit"}}>Edit</button>
    <button {{action "delete"}}>Delete</button> 

    {{outlet}}

</script>

Here's my user model-

App.User = DS.Model.extend({
  name: DS.attr('string'),
  email: DS.attr('string'),
  subjects: DS.hasMany('subject')    
});

And my subject model-

App.Subject = DS.Model.extend({
  name: DS.attr('string'),
  users: DS.hasMany('user')
});

Any suggestions?

2
do you provide the right model to the controller? if you do, can you please post a example with your problem on jsbin so we can retrace your problem? - Mythli

2 Answers

1
votes

You are doing wrong. You are in the context for the current user, So you can access to his currents subjects. Basically the thing that you need to do is this:

{{#each subject in subjects}}
    <li>
        {{subject.name}}
    </li>
{{/each}}

or

{{#each subjects}}
    <li>
        {{this.name}}
    </li>
{{/each}}
0
votes

user is your scope, so you send in the collection

   {{#each item in collection}}

For you

   {{#each subject in subjects}}
        <li>
            {{subject.name}}
        </li>
    {{/each}}