I have a list of posts, when I click in a post I should go to an page showing only the clicked post and all comments(async) of this. I do not know how I can display the list of comments.
pls tell me if something is outside of pattern, all code:
route.js
this.resource('posts', {path: '/'} , function() {
this.route('post', {path: 'posts/:post_id'});
});
Model's:
// Comment:
export default DS.Model.extend({
message: DS.attr('string'),
network: DS.attr('string'),
post: DS.belongsTo('post', {async: true}),
});
// Post:
export default DS.Model.extend({
message: DS.attr('string'),
network: DS.attr('string'),
comments: DS.hasMany('comment', {async: true}),
});
Routes:
// posts.index
model(params) {
return this.store.find('post');
}
// posts.post
model(params) {
var {post_id, network} = params;
return this.store.find('comment', post_id);
}
templates:
// posts.index
{{#each post in model}}
{{#link-to "posts.post" post.id (query-params network=post.network)}}
{{post.id}} / {{post.network}}
{{/link-to}}
{{/each}}
// posts.post
<p>Model {{model}}</p>
{{#each comment in model}}
{{comment.message}}
{{/each}}
API Mocked
// GET / posts
res.send({
'posts': [
{id: 1, message: 'I"m POST', network: 'twitter'}
]
});
// GET /:id comments
res.send({
'post': {
'data': [1]
},
'comments': [
{id: 1, post: 1, message: 'Hello 1', network: 'twitter'},
{id: 2, post: 1, message: 'Hello 2', network: 'twitter'},
]
});
Aucun commentaire:
Enregistrer un commentaire