How do I make an Ember-Data has-many collection that is defined by another API endpoint?
I've got a model, Post
that has many Comment
s. The response for GET /posts/12
is pretty Ember-Data-friendly:
{
"post": {
"id": 12,
"title": "I Love Ramen",
"created_at": "2011-08-19T14:22",
"updated_at": "2011-08-19T14:22",
"body": "..."
}
}
The API for that Post
's Comment
s, however, is GET /posts/12/comments
, which returns
{
"comments": [
{
"id": 673,
"author_id": 48,
"created_at": "2011-08-21T18:03",
"body": "Me too!"
}
]
}
What can I do to my model or my adapter to tell it that for Post 12
's Comment
s, use /posts/12/comments
? Notice that the Post
itself has no knowledge of the Comment
IDs.
Update
In response to buuda's answer, here are some clarifications:
The Post
must be able to look up its Comment
s so that I can (a) show the comments on the PostRoute
and (b) have properties on Post
like
hasComments: function() {
return this.get('comments.length') > 0;
}.property('comments')
It's fine with me if I have to implement the comments
computed property, though. In the above-mentioned answer, buuda suggests
App.Comments.find({ id: postId });
How do I get the datastore to fetch /posts/:postId/comments
instead of /comments?postId=:postId
?