lundi 4 avril 2016

Ember-data: model refresh on DS.Store.createRecord

Hi, Embsters!

I am trying to figure out why my model isn't refreshed after I create a new record and save it to the store.

My route computes the model as follows:

model: function (params) {
  var postID = params.post_id,
      userID = this.get('session.currentUser.id');

  var post = this.store.findRecord('post', postID) ;

  var followings = this.store.query('post-following', {
      filter: { post: postID }
  }) ;
  var userFollowing = this.store.queryRecord('post-following', {
      filter: { post: postID, user: userID }
  }) ;

  return new Ember.RSVP.hash({
      post:          post,
      followings:    followings,
      userFollowing: userFollowing
  });
}

My template then renders a list and a button:

{{#each model.followings as |following|}}
    ...
{{/each}}

{{#if model.userFollowing}}
    <button {{action 'follow'}}>Follow</button>
{{else}}
    <button {{action 'unFollow'}}>Unfollow</button>
{{/if}}

And my controller creates/deletes the relevant post-following record:

actions: {
    follow: function () {
        var user = this.get('session.currentUser'),
            post = this.get('post') ;

        this.store.createRecord('post-following', {
            user: user,
            post: post
        }).save();
    },
    unFollow: function () {
        this.get('model.userFollowing').destroyRecord() ;
    }
}  

When I click the [Follow] button:

  • a successful POST request is sent
  • the button is not updated
  • the list is not updated

When I (refresh the page then) click the [Unfollow] button:

  • a successful DELETE request is sent
  • the button is not updated
  • the list is updated

Do you have any idea of what I'm doing wrong?
Could it be a problem with my payload?

Thanks a lot !!!
Brou




Aucun commentaire:

Enregistrer un commentaire