Ember.js 如何获取余烬路径中当前对象的子对象
《灰烬指南》中的“模板”部分介绍了如何使用“链接到”帮助器。然而,我似乎找不到以下问题的答案: 我有一个页面显示Post对象的一些属性。该页面包含一个“评论”链接,需要显示属于当前帖子的评论Ember.js 如何获取余烬路径中当前对象的子对象,ember.js,ember-router,Ember.js,Ember Router,《灰烬指南》中的“模板”部分介绍了如何使用“链接到”帮助器。然而,我似乎找不到以下问题的答案: 我有一个页面显示Post对象的一些属性。该页面包含一个“评论”链接,需要显示属于当前帖子的评论 // Router App.Router.map(function(match) { match('/posts').to('posts', function(match) { match('/:post_id').to('post', function(match) { match
// Router
App.Router.map(function(match) {
match('/posts').to('posts', function(match) {
match('/:post_id').to('post', function(match) {
match('/').to('postIndex');
match('/comments').to('comments');
});
});
});
// post template
...
{{#linkTo "comments"}}Comments{{/linkTo}}
...
{{outlet}}
我需要如何定义我的CommentsRoute以使用当前帖子的注释填充控制器的内容
App.CommentsRoute = Ember.Route.extend({
model: function() {
// I need to get the content of the postController here
// this.controllerFor('post') seemed obvious, but doesn't work
post = ????;
post.get('comments')
}
})
提前感谢。您可以使用
控制器从以下路径访问后置控制器:
App.CommentsRoute = Ember.Route.extend({
model: function() {
var controller = this.controllerFor('post');
return controller.get('comments');
}
});
你在使用余烬数据吗?在这种情况下,在加载post数据时,将注释数据侧向加载可能是有意义的
更新:吟诵get('comments')
至get('comments.content')
更新:还原了get('comments')
我尝试了controllerFor
但是当我在模板中循环注释并访问注释的属性时,我得到了“Uncaught TypeError:无法调用undefined的方法'hasOwnProperty'。可能是因为我在报告中描述的。而且:是的,我会把评论放在一边。谢谢。注释是否真的存储在controller.comments
中?你能发布你的模型和控制器吗?(或者做一个jsFiddle)?没关系。我在你的另一篇文章中见过这个模特。请查看我的更新。未捕获的类型错误是由我在关联中使用的导致的。