Reputation: 75
I'm attempting to construct a dynamic REST call in my ember app. I was attempting to use this solution as a starting point but it's not working and I'm not sure if it's because Ember is now using JSON API and I'm structuring it wrong: Dynamic segment in ember data adapter
In the back end the call looks like /posts/{postID}/comments
and I want to be able to dynamically get comments from post of ID 1, 2, 3, etc...
Here is my basic structure
Post model:
export default DS.Model.extend({
name: DS.attr('string'),
comments: DS.hasMany('comment', {async:true})
});
Comment Model:
export default DS.Model.extend({
name: DS.attr('string')
});
Template:
<ul>
{{#each model as |post|}}
{{#each post.comments as |comment|}}
<li>{{comment.name}}</li>
{{/each}}
{{/each}}
</ul>
Json Post Payload:
"data": [{
"type": "posts",
"id": "1",
"attributes": {
"id": 1
"name": "my title"
},
"links": {
"comments": "comments"
}
}]
My goal is for the call to comments to construct a namespace that looks like /posts/1/comments
using the template above. I'm getting the post model back and have verified that the first {{#each}}
loop works, but the call to post.comments
does nothing in the template.
Upvotes: 1
Views: 407
Reputation: 9537
Your payload is not confirm the JSON API. Because comments
is a relationship of post
, the payload should look something like this:
"data": [{
"type": "posts",
"id": "1",
"attributes": {
"id": 1,
"name": "my title"
},
"relationships": {
"comments": {
"links": {
"related": "/posts/1/comments"
}
}
}
}]
Read here for more info regarding JSON API relationships.
N.B. The way your payload was build, does work with the old RESTSerializer.
Upvotes: 2
Reputation: 18
This is assuming you have access to change the payload response. If not, you will have to work with a serializer to massage the data.
First, from what you have shown here, it doesn't look like you have loaded any of the comments into the model.
Your payload should probably look something like this:
"data": [{
"type": "posts",
"id": "1",
"attributes": {
"id": 1
"name": "my title"
},
"relationships": {
"comments": {
"data": [
{
"id": "3",
"name": "comment"
}
]
}
}
}]
Upvotes: 0