Ember.js emberjs嵌套动态路由段返回null,无法更新子记录

Ember.js emberjs嵌套动态路由段返回null,无法更新子记录,ember.js,handlebars.js,ember-router,Ember.js,Handlebars.js,Ember Router,我正在尝试实现一个评论功能,以显示属于单个帖子的评论列表。然后单击“编辑”并从属于单个帖子的所有评论中编辑任何选定的评论 已更新 我能够创建一条属于所选帖子的评论,如上图所示**但是,我无法更新现有注释,并且注释编辑表单甚至不会显示任何注释。它始终为空,不绑定到任何现有注释 单击editcomment,url是posts/2/comments/undefined/edit。这是因为EmBlog.PostCommentRoute和PostEditCommentRoute仍返回null 所有注释掉的

我正在尝试实现一个评论功能,以显示属于单个帖子的评论列表。然后单击“编辑”并从属于单个帖子的所有评论中编辑任何选定的评论

已更新

我能够创建一条属于所选帖子的评论,如上图所示**但是,我无法更新现有注释,并且注释编辑表单甚至不会显示任何注释。它始终为空,不绑定到任何现有注释

单击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'});       
     }); 
   });
});

问题1:如果您不更改路径并将其保留为“/:post_id”,我认为这会有所帮助


问题2:对不起,我想我帮不了你。

我从小提琴上下载了你的代码,发现了一些问题

首先

您不小心使用了
Ember.Route
而不是下面的
Ember.Route

EmBlog.PostCommentsRoute=Ember.Router.extend({
// ...
EmBlog.PostCommentRoute=Ember.Router.extend({
应该是

EmBlog.PostCommentsRoute=Ember.Route.extend({
// ...
EmBlog.PostCommentRoute=Ember.Route.extend({

在此路由中,您不需要重写
模型
,默认的余烬行为是正常的。当未声明该变量时,您还引用了
参数id

EmBlog.PostRoute=Ember.Route.extend({
型号:功能(参数){
post=此.modelFor('posts');
返回post.get(参数id);
//返回EmBlog.Post.find(params.Post_id);
//返回此.modelFor('post').filterProperty('id',params.post_id);
},
第三

回应你下面的评论

问题是你是在文章的上下文中链接到editComment的,而不是评论本身。修复后,我还将文本区域更改为
model.body
,而不是
body


更改被逐项列出。现在需要进行编辑。

修改了循环。在您没有传递上下文之前,因此您在路径中没有定义。现在您正在将每个注释传递给linkTo,以便它可以生成正确的路由。下面是更新的fiddle的链接


是评论模板
{{{#linkTo“post.newComment”}添加注释{{/linkTo}


{{#内容中的每个注释}
{{comment.body}}
{{{#链接到“post.editComment”comment}编辑评论{{/linkTo}

{{/每个}} {{outlet}}
这是更新后的表单。需要绑定到content.body

<script type="text/x-handlebars" data-template-name="post/_commentForm">
   <form {{action save on='submit'}}>
{{view Ember.TextArea valueBinding="content.body" placeholder="body"}}
<button type="submit"> save comment </button> 
 <button {{action 'cancel' content}}> Cancel</button>
</form>
</script>

{{view Ember.TextArea valueBinding=“content.body”placeholder=“body”}
保存评论
取消

谢谢@finn。我会把路径改回“/:post_id”,然后在那里试用。谢谢你的时间。另外,既然你也在写博客,也许你可以使用一些代码。好的,谢谢,我会在那个repo中查看源代码。非常感谢。为什么不呢?我认为路径很可能是他的代码不起作用的原因。我可能是错的,但这仍然是一个答案。@finn,在更新的小提琴中,我一直保持在路径“/:post_id”上"并且通常使用了你在另一个问题中建议的结构。但是注释模板仍然没有呈现,并且所有的模板都嵌套在它下面。我已经重新编写了我的问题并删除了对rails的所有引用,因为问题实际上是emberjs。谢谢@mutewinter。我已经用更正更新了。我有一个问题,EmBlog.PostCommentRoute和PostEditCommentRoute仍然返回null。单击editcomment,url为posts/2/comments/undefined/edit。在JSFIDE中,您会看到我尝试了我知道的各种方法,使用各种排列来查看它是否有效,然后我注释掉了每一个不起作用的方法,但将它们留在代码中只是为了知道是什么我已经试过了。这也意味着,当编辑注释的表单显示时,它不会绑定到任何现有注释。谢谢,这一条让您更进一步。我得到了注释编辑的正确路径,并且内容显示在注释的文本区域中。主要问题是您的上下文混乱。我假设您希望ed循环浏览所有评论,用编辑链接显示它们,然后有一个链接添加一个新的。非常感谢Fred。是的,你的假设是正确的,现在一切都正常了。我尝试奖励你奖金,它闪烁了一条红色消息,我可能会在17小时内奖励它。因此我也会立即奖励它。再次感谢。谢谢,很高兴我能帮忙
<script type="text/x-handlebars" data-template-name="post/_commentForm">
   <form {{action save on='submit'}}>
{{view Ember.TextArea valueBinding="content.body" placeholder="body"}}
<button type="submit"> save comment </button> 
 <button {{action 'cancel' content}}> Cancel</button>
</form>
</script>