我有一个帖子模型和一个相关的评论模型,如下所示:
// app/models/post.js
import DS from 'ember-data';
export default DS.Model.extend({
//....
comments: DS.hasMany('comment'),
//....
})
// app/models/comment.js
import DS from 'ember-data'
export default DS.Model.extend({
//....
ad: DS.belongsTo('post'),,
//....
})
在 app/router.js
我有
Router.map(function() {
this.route('post', {path: '/post/:post_id'}, function() {
this.route('comments');
});
)};
我在 app/templates/post.hbs
中有一个 post
模板,如下所示:
<h2>{{model.title}}</h2>
<p>{{model.body}}</p>
{{#link-to "post.comments"}}
<h2>Comments</h2>
{{/link-to}}
{{outlet}}
我还有一个模板来呈现 app/templates/post/comments.hbs
中的评论
{#each model as |comment|}}
<p>{{comment.body}}</p>
{{/each}}
{{outlet}}
我希望当有人点击帖子模板上的评论链接时,评论模板在帖子模板的 {{outlet}}
上呈现。我遇到的问题是为 comments
模型获取数据。我在 app/routes/post/comments.js
中有一个用于 comments
的路由,它看起来像这样:
import Ember from 'ember';
export default Ember.Route.extend({
model(){
//I don't know what to do here.
}
});
我后端服务器中的评论公开在 /posts/:post_id/comments
端点。如何获取评论模板上的评论?
最佳答案
因为您已经在父 route:post
中加载了 Post
,您的 route:post.comments
可以简单地请求 注释
来自它:
// app/routes/post/comments.js
import Ember from 'ember';
export default Ember.Route.extend({
model(){
const post = this.modelFor('post');
return post.get('comments');
}
});
关于javascript - 如何从服务器获取嵌套路由 ember 2.3.0 的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35253052/