Quantcast
Channel: Ember.JS - Latest topics
Viewing all articles
Browse latest Browse all 4830

One to many relationship not displaying on detail page

$
0
0

@therealbenhogan wrote:

Hi,

Rather having a master-detail view, whereby you might have a list of posts in the left hand column and the post detail to right, I have the posts and post detail on separate pages using this router

Router.map(function() {
   this.route('posts');  
   this.route('post',{path:'/post/:id'});
});

In my post route I have

export default Ember.Route.extend({  
  model(params) {    
      return this.store.findRecord('post',params.id) 
}});

The post model has a one to many relationship with comments (and other relationships which I've omitted here)

 export default DS.Model.extend({   
    name:DS.attr('string'),    
    description:DS.attr('string'),    
    comments:DS.hasMany('comment')
   });

I am sure my API is returning the correct response on /posts/1 for example - here is a sample

{"data":{"id":"1","type":"posts","links":{"self":"/api/posts/1"},"attributes":{"name":"Post 1","description":"post   text"},"relationships":{"comments":{"links":{"self":"/api/posts/1/relationships/comments","related":"/api/posts/1/comments"}}}}}

and here is the response on /posts/1/comments

{"data":[{"id":"1","type":"comments","links":{"self":"/api/comments/1"},"attributes":{"description":"comment for post 1"},"relationships":{"post":{"links":{"self":"/api/comments/1/relationships/post","related":"/api/comments/1/post"}}}},{"id":"2","type":"comments","links":{"self":"/api/comments/2"},"attributes":{"description":"another comment for post 1"},"relationships":{"post":{"links":{"self":"/api/comments/2/relationships/post","related":"/api/comments/2/post"}}}}]}

The problem is this .... on the post detail page I want to list the comments relating to the post. eg

 {{#each model.comments as |comment|}}<p>{{comment.description}}</p>{{/each}}

If I am viewing the post detail page and refresh, the comments show. If I go to the post page from /posts though - they don't show, they only do if I refresh the page again.

On the posts template I have tried passing the model and just the model id but neither makes any difference:

{{#each model as |post|}}
{{link-to post.name 'post' post.id}} //  {{link-to post.name 'post' post}}
{{/each}}

Many thanks in advance!

Posts: 8

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 4830

Trending Articles