vendredi 29 avril 2016

Emberjs - how to reset a field on a component after saving?

I have an embedded object called Comment, inside a Game. Each game can have many Comments.

When a user (called a Parent) views the game page, they can leave a comment.

The problem I have is that I cannot seem to reset the body of the comment field back to empty after the comment is saved.

This is the component:

MyApp.AddCommentComponent = Ember.Component.extend({

  body: '',
  actions: {
    addComment: function() {

      var comment, failure, success;
      if (!!this.get('body').trim()) {
        comment = this.store.createRecord('comment', {
          body: this.get('body'),
          game: this.get('game'),
          parent_id: this.get('parent_id'),
          parent_name: this.get('parent_name')
        });

        success = (function() {
          this.set('body', '');
        });
        failure = (function() {
          return console.log("Failed to save comment");
        });
        return comment.save().then(success, failure);
      }
    }
  }
});

The error is on the 'this.set' line - this.set is not a function

All the examples I find are about doing this in a controller or by creating a new record upon route change (but the route is not changing in my example, since it is just adding another embedded object to the existing page).




Aucun commentaire:

Enregistrer un commentaire