3
votes

How can I tell Ember to retrieve not only a list of matches, but also get objects for each of the players associated?

In my router, I have:

App.MatchesRoute = Ember.Route.extend
  model: ->
    App.Match.find()

Here is what my JSON data is for localhost:3000/matches.json

{"matches":[{"id":1,"player_one":{"id":1,"name":"Lex"},"player_two":{"id":20,"name":"Superman"}}]}

Update to provide more info 03/15/13

Here is the matches.emblem (Emblem being the haml-slim-like-handlebars)

#matches
  each match in controller
    linkTo "match" match class="panel six columns"
      p Match between {{match.player_one.name}} and {{match.player_two.name}}

We are successfully getting the Match objects, and I can call the id on it (not shown), but we need to get the names of match.player_one and match.player_two, which are returning undefined.

Match

App.Match = DS.Model.extend
  player_one: DS.belongsTo('App.Player')
  player_two: DS.belongsTo('App.Player')

Player

App.Player = DS.Model.extend
  name: DS.attr('string')
1
I'm not sure about this, but I think your Player model has to reference the Match with a belongsTo as well. And you'd have to tell your adapter to embed the child object. Something like: DS.RESTAdapter.map( 'App.Match', { player_one: { embedded: 'load' } }); ... something like this.. but I'm not 100% sure of how to properly do this, I just know it's something along these lines (I use a modified version of the adapter)MilkyWayJoe
Thanks for thinking about this, is there a reason you use a modified version of the adapter? Maybe that's something I should be looking into as well.ardavis
The only reason I use a different adapter/serializer is because I use .NET backend, which is slightly different from Rails. It is part of the Ember Web API template in the .NET world. You can also see it in use here. For Rails, use the built-in RESTAdapterMilkyWayJoe

1 Answers

0
votes

If you always returns the players with a match, you probably want to declare the players as embedded.

You would write something like this:

Adapter.map(App.Match, {
  player_one: {embedded: load},
  player_two: {embedded: load},
});