【问题标题】:unable to edit a hasMany association child record in ember-data无法在 ember-data 中编辑 hasMany 关联子记录
【发布时间】:2013-04-02 22:35:52
【问题描述】:

我有一个帖子,里面有很多 cmets。问题是我能够获取所有 cmets,但我无法获取单个评论,因此无法编辑单个评论。由于我无法获取单个评论,这意味着我无法将单个记录添加到事务或编辑单个记录。

cmets 是侧载的,不会由路由支持,我不想要任何与评论相关的控制器的路由。因此,我使用 controllerFor 在 ApplicationRoute 中为 CommentController 设置模型,然后使用 [needs] api 将模型包含在可能需要模型内容的其他评论相关控制器中。

您可以通过点击post -> 然后点击post title -> 联系 cmets 点击添加评论*然后保存并重新点击editcomment

这是jsfiddle,但与此问题相关的代码如下:

 EmBlog.ApplicationRoute = Ember.Route.extend({
   setupController: function() {

    this.controllerFor('comment').set('model', EmBlog.Comment.find());      
   }
 });

评论控制器

  //Even when I use ArrayController, the error is still there
   EmBlog.CommentController = Ember.ObjectController.extend({
      content: null
   });

处理编辑的控制器,所有的错误都发生在editComment方法中

 EmBlog.CommentEditController = Ember.ObjectController.extend({

   needs: ['comment'],
   isEditing: false,

   editComment: function(post) {

     var comment =  this.get('controllers.comment.content');  

     var yk = comment.get('post');

     //this line is undefined 
     console.log(yk);

    var commentEdit = this.set('content', comment);
     console.log(commentEdit);

    transaction = this.get('store').transaction();

    //Uncaught Error: assertion failed: You must pass a record into transaction.add() 
    transaction.add(commentEdit);

    this.transaction = transaction;

    this.set('isEditing', true);

   }

  });

用于发布/展示的把手

  <script type="text/x-handlebars" data-template-name="posts/show">
    {{render 'comment/edit' comment}}
  </script>

【问题讨论】:

    标签: ember.js ember-data


    【解决方案1】:

    现在一切都是 working and here is the jsfiddle,我可以编辑灯具中的 cmets。

    但唯一存在的问题是我只能编辑夹具中的第一条评论。我无法编辑第二条评论或新评论无法编辑我添加的任何新评论

    我基本上是通过需求 api 获取 postShow 中的帖子,然后我得到帖子的 id 并将其作为 find 参数传递给 ** EmBlog.Comment.find**。然后我将 EmBlog.CommentEditController 的内容设置为我们刚刚找到的结果。在此之后,我使用 transaction.add(this.get('content'))

    EmBlog.CommentEditController 的新内容添加到事务中
    EmBlog.CommentEditController = Ember.ObjectController.extend({
    
       needs: ['comment', 'postsShow'],
       isEditing: false,
    
       editComment: function() {  
         var post = this.get('controllers.postsShow.content');
         console.log(post);
    
         var postId = post.get('id');
         console.log(postId);
    
         comment = EmBlog.Comment.find(postId);
         console.log(comment);
    
         transaction = this.get('store').transaction();
    
         var updatedContent = this.set('content', comment);
         console.log(updatedContent);
         console.log(this.get('content')); 
    
        transaction.add(this.get('content'));
        console.log(transaction);
    
        this.transaction = transaction;   
    
        this.set('isEditing', true);
    
       },
    
      save: function(){
    
       var comment = this.get('content');
       comment.one('didUpdate', this, function() {
          this.set('isEditing', false);
       });
    
      this.transaction.commit();
      this.transaction = null;
       console.log(this.get('content')); 
      }  
    
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-21
      • 1970-01-01
      • 1970-01-01
      • 2014-03-25
      • 1970-01-01
      • 1970-01-01
      • 2015-11-03
      • 1970-01-01
      相关资源
      最近更新 更多