我正在尝试实现一个评论功能,以显示属于单个帖子的评论列表。然后单击编辑并从属于单个帖子的所有评论中编辑任何选定的评论。
更新了 jsfiddle。
我可以创建属于所选帖子的评论,如上面的小提琴所示。**但是我无法更新现有评论,评论编辑表单甚至不会显示任何评论。它始终为空白,并且不绑定到任何现有评论。
点击 editcomment 的 url 是posts/2/comments/undefined/edit。这是因为 EmBlog.PostCommentRoute 和 PostEditCommentRoute 仍然返回 null。
所有被注释掉的代码都是让它工作失败的不同尝试。我把它们留在这里,所以任何看到这个问题的人都会知道我到目前为止所做的尝试。
始终返回 null 并且最有可能导致问题的两条路线
EmBlog.PostEditCommentRoute = Ember.Route.extend({
model: function(params) {
var commentEdit = this.modelFor('post').get('comments');
return commentEdit.get(params.comment_id);
//return EmBlog.Comment.find({post: post.get('id'), id: params.comment_id});
//var comment = this.modelFor('post').get('comments');
//return comment.filterProperty('id', params.comment_id);
},
setupcontroller: function( controller, model) {
controller.set('content', model);
}
});
显示单个帖子的评论路线
EmBlog.PostCommentRoute = Ember.Route.extend({
model: function(params){
comment = this.modelFor('post').get('comments');
// comment = EmBlog.Comment.find(params.comment_id);
return comment.get(params.comment_id);
// return comment.filterProperty('body', params.comment_id);
},
setupController: function(controller, model) {
//var comment = this.controllerFor('postComments').get('body');
//controller.set('content', comment.filterProperty('body', model));
controller.set('content', model);
},
});
这是路由器。我已经尝试过其他的嵌套组合,但选择了这个,因为它是唯一允许添加评论的版本,这就是为什么这个问题只关注更新嵌套的动态段,否则我会问这两个:
EmBlog.Router.map(function() {
this.resource("posts", {path: '/posts'}, function(){
this.route('new');
this.resource('post', {path: '/:post_id/'}, function(){
this.route('edit', {path: '/edit'});
this.route('comments', {path: '/comments'});
this.route('newComment');
this.route('comment', {path: '/comments/:comment_id'});
this.route('editComment', {path: '/comments/:comment_id/edit'});
});
});
});