0

It appears that Ember Model expects one of two things, the relationship key in the JSON response contains (1) the data of the records or (2) the ids of the records. The API that I am working with (and that I have no control over) provides neither. It simply returns an empty array indicating that it can have comments. I have pasted a sample JSON response for a call to /posts/1.

{
  id: 1,
  title: "Hello World",
  comments: []
}

The API endpoint for comments is /posts/1/comments. Using Ember Model, how would one go about fetching the comments of the post? This isn't only an issue when relationships are "empty" (no data or ids are provided), but also with deeply nested model hierarchies. The url property of an Ember Model would need (1) one or more dynamic segments and (2) the ability to replace those dynamic segments with data when the URL is build for the query.

The structure that I am currently working with (mirroring the API that I am working with) looks like this, /series/:serie_id/seasons/:season_id/episodes/:episode_id. I don't think it is reasonable to expect the API to return the entire data tree for one or more series.

Bart Jacobs
  • 9,022
  • 7
  • 47
  • 88

1 Answers1

0

You could use a computed property on posts model, and then write a custom adapter to get your posts out.

App.Post = Ember.Model.extend({
  comment_ids: attr(),
  comments: function() {
    return App.Comment.find(this.get("comment_ids"); 
  }.property('comment_ids.[]')
})
Nath
  • 6,774
  • 2
  • 28
  • 22