mardi 1 août 2017

I'm struggling to create a new comment the correct way on the frontend ember

I am trying to create a comment with this addComment action where I want to use the input text as the comment text and do a save to create the comment.

I couldn't connect the input box to comment.body because the position of this code does not have the comment model available.

I created a body field on the item model so I connect item.body to the text box and then use this as the comment.body when creating the comment which seems very wrong. Does anyone have any suggestions as to how to do this the correct way?

<form class="comments-list__add-comment add-comment" action="">
  
  <button  type="button" class="btn add-comment__submit" name="button">Add comment</button>
</form>

addComment(item){
  const plan = item.get('plan');
  const text = this.get('item.body');
  const currentUserName = plan.get('appConfig.currentUser');
  const currentUserId = plan.get('appConfig.currentUserId');
  const itemid = item.id;
  if(text.trim() !== ''){
    let comment = this.get('item.store').createRecord('comment', {
      body: text,
      createdAt: new Date(),
      commentableId: itemid,
      commentableType: 'Plan',
      unread: true,
      commenterName: currentUserName,
      commenterId: currentUserId
    });
    item.get('comments').pushObject(comment);
    comment.save();
    item.set('displayAddCommentForm', false);
    this.set('item.body', '');
  }
},




Aucun commentaire:

Enregistrer un commentaire