DeliciousMe
DeliciousMe

Reputation: 71

can handlebars each loop access parent model id

If I have a model like this:

App.Group = DS.Model.extend({
  title: DS.attr('string'),
  persons : DS.hasMany('App.Person')
});

App.Person = DS.Model.extend({
  name : DS.attr('string'),
  age : DS.attr('object'),
  group : DS.belongsTo('App.Group')
});

And if I have fixture data like this:

App.Group.FIXTURES = [{
 title : "Group A",
 persons : [10,12]
 },{
 title: "Group B",
 persons: [13,14]
}]

App.Person.FIXTURES = [{
 name: "Bill",
 age: 24
},{
 name: "Ted",
 age: 25
},{
 name: "Mr. Excellent",
 age: 30
},{
 name: "Mr. Adventures",
 age: 21
}]

And a template like this:

<script type="text/x-handlebars" data-template-name="Group">
  {{title}}
  {{render person}}
</script>

<script type="text/x-handlebars" data-template-name="Person">
  {{#each in controller}}
     {{name}}
  {{/each}}
</script>

So what happens right now is the person template displays all the persons in the person array. How can I get the each loop to only go over the Persons in Group A, rather than displaying every Person in the fixture? Can this be done in handlebars? Or is this something I need to specify in the route?

Upvotes: 0

Views: 259

Answers (1)

Edu
Edu

Reputation: 2520

You can use the second argument of the render helper to pass the model for the person controller...

{{render person persons}}

But your Person.FIXTURES and group.FIXTURES must have the ids for the relations

App.Person.FIXTURES = [{
  id:10,
  name: "Bill",
  age: 24
},
[...]
App.Group.FIXTURES = [{
  id: 1,
  title : "Group A",
  persons : [10,12]
},
[...]

Upvotes: 1

Related Questions