0

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

更新了 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'});       
     }); 
   });
});
4

3 回答 3

1

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

第一的

你不小心用下面Ember.Router的代替了Ember.Route

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

应该

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

第二

您不需要model在此路由中覆盖,默认的 Ember 行为很好。您还引用params_id了尚未声明该变量的时间。

EmBlog.PostRoute = Ember.Route.extend({
  model: function(params) {
     post = this.modelFor('posts');
     return post.get(params_id);
    //return EmBlog.Post.find(params.post_id);
    //return this.modelFor('post').filterProperty('id', params.post_id);
  },

第三

回应您在下面的评论

问题是您从帖子的上下文中链接到 editComment,而不是评论本身。修复后,我还将 TextArea 更改为model.body而不是body.

更改在此 Gist中逐项列出。现在需要实施编辑。

于 2013-04-11T23:40:16.497 回答
1

修改了循环。在您没有传递上下文之前,因此您在路径中变得未定义。现在您将每条评论传递给 linkTo,以便它可以生成正确的路线。这是更新的小提琴http://jsfiddle.net/VrR2T/4/的链接

<script type="text/x-handlebars" data-template-name="post/comments">
  <h1> Yes Comments template</h1>

    <p> {{#linkTo "post.newComment"}} Add comment{{/linkTo}}</p>
    <br/>
    {{#each comment in content}}
        <br/>
          {{comment.body}} <br/> 
     <p>{{#linkTo "post.editComment" comment}} Edit Comment {{/linkTo}}</p>

    {{/each}}
  {{outlet}}
</script>

这是更新的表格。需要绑定到 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>
于 2013-04-12T22:48:33.563 回答
0

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

问题 2:对不起,我想我在这里帮不上忙。

于 2013-04-10T14:14:05.923 回答