简体   繁体   中英

unable to edit a hasMany association child record in ember-data

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

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM