lundi 29 février 2016

hasMany relationship with JSON links

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.




Aucun commentaire:

Enregistrer un commentaire