我正在尝试使用 Emberjs需要 api * 从评论控制器访问 Postscontroller。PostController 由路由支持,而我不希望评论的控制器有路由。
在评论控制器中,我需要: ['posts', 'postsShow']。在评论控制器中,当我使用以下命令运行控制台日志时:
console.log( this.get('controllers.postsShow') );
console.log( this.get('controllers.posts') );
在控制台中我看到:
<EmBlog.PostsShowController:ember396> { target=<EmBlog.Router:ember316>, namespace=EmBlog, store=<EmBlog.Store:ember336>
<EmBlog.PostsController:ember304> { target=<EmBlog.Router:ember316>, namespace=EmBlog, store=<EmBlog.Store:ember336>
但是,当我尝试访问PostsShowController 或 PostsController 的控制器内容时,它总是返回post undefined。这些是我尝试过的各种方法,但仍然得到未定义的帖子:
var post = this.get('controllers.posts').get('content');
or
var post = this.get('controllers.posts.content');
我也试图从这样的内容中获得“评论”:
var post = this.get('controllers.posts')
var comment = post.get('comments');
or
comment = post.comments;
我仍然得到错误:
TypeError: post is undefined comment = post.comments;
TypeError: post is undefined var comment = post.get('comments');
这也意味着:
var post = this.get('controllers.posts.model').get('store.transaction');
also returns post is undefined.
这是jsfiddle,代码的相关部分粘贴在下面:
EmBlog.PostsNewController = Ember.ObjectController.extend({
content: null
});
EmBlog.PostsShowController =
Ember.ObjectController.extend({
content: null
});
EmBlog.CommentNewController = Em.ObjectController.extend({
needs: ['posts', 'postsShow'],
isAddingNew: false,
addComment: function(body){
console.log( this.get('controllers.postsShow') );
console.log( this.get('controllers.posts') );
var post = this.get('controllers.posts.content');
store = post.get('store.transaction');
}
});
非常感谢