无法在ember-data中编辑hasMany关联子记录 [英] unable to edit a hasMany association child record in ember-data

查看:95
本文介绍了无法在ember-data中编辑hasMany关联子记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一篇有很多意见的帖子。问题是我能够获取所有的评论,但是我无法获取单个评论,因此无法编辑单个评论。由于我无法获取单个评论,这意味着我无法为交易添加单个记录或编辑单个记录。



评论是侧面加载的,没有路由支持,我不想要任何注释相关控制器的路由。所以我使用 controllerFor 在ApplicationRoute中设置CommentController的模型,然后使用[needs] api将模型包含在可能需要模型内容的其他注释相关的控制器中。



您可以点击发布 - >然后点击帖子标题 - >
点击添加评论 *然后保存并重新显示 editcomment



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

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

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

评论控制器

  //即使我使用ArrayController,错误仍然存​​在
EmBlog.CommentController = Ember.ObjectController.extend({
content:null
});

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

  EmBlog.CommentEditController = Ember.ObjectController.extend({

需要:['comment'] ,
isEditing:false,

editComment:function(post){

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

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

//此行未定义
console.log(yk);

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

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

//未捕获错误:断言失败:您必须将记录传递到transaction.add()
transaction.add(commentEdit);

this.transaction = transaction ;

this.set('isEditing',true);

}

});

发布/展示的手柄

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


解决方案

现在一切都是 工作,这里是jsfiddle ,我可以编辑灯具中的注释。



但唯一现有的问题是我只能编辑灯具中的第一个注释。我无法编辑第二个评论或新的评论,无法编辑任何我添加的新评论



我基本上通过postShow中的帖子需要api,然后我得到了该帖子的id,并将其作为一个查找参数传递给了EmBlog.Comment.find **。然后我将 EmBlog.CommentEditController 的内容设置为我们刚刚发现的结果。之后,我使用 transaction.add(this.get('content'))将新内容 EmBlog.CommentEditController 添加到事务

  EmBlog.CommentEditController = Ember.ObjectController.extend({

需要:['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内容',评论);
console.log(updatedContent);
console.log(this.get('content'));

transaction.add(this.get ('content'));
console.log(transaction);

this.transaction = tran saction;

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

});


I have a Post which has many comments. The problem is that I am able to fetch all the comments but I am unable to fetch a single comment and as a result cannot edit a single comment. Since I can't fetch a single comment, It means I can't add a single record to the transaction or edit a single record.

The comments is sideloaded and will not be backed by a route and I don't want a route for any of the comment related controllers. So I am setting the model for the CommentController in the ApplicationRoute using controllerFor and then use the [needs] api to include the model in other comment related controller that may want the model's content.

You can reach the comments by clicking post -> then click post title -> click add comment* then save and reclick editcomment.

This is the jsfiddle but the relevant bit of the code related to this question is below:

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

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

The Comment Controller

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

The controller that handles editing, all the errors happen in the editComment method

 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);

   }

  });

Handlebars for post/show

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

解决方案

For now everything is working and here is the jsfiddle and I am able to edit the comments in the fixtures.

But the only existing problem is that I am only able to edit the first comment in the fixture. I can't edit the 2nd comment or an new comment unable to edit any new comment that I add.

I basically fetched the post in postShow through the needs api, then I got the id of the post and passed it as a find argument to ** EmBlog.Comment.find**. Then I set the content of EmBlog.CommentEditController to the result of that find we just did. After this I added the new content of EmBlog.CommentEditController to the transaction with transaction.add(this.get('content'))

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

});

这篇关于无法在ember-data中编辑hasMany关联子记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆