bookthief
bookthief

Reputation: 2451

Ember each statement

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?

Upvotes: 0

Views: 62

Answers (2)

Jorge Garc&#237;a
Jorge Garc&#237;a

Reputation: 5113

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}}

Upvotes: 1

Kingpin2k
Kingpin2k

Reputation: 47367

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}}

Upvotes: 0

Related Questions