samedi 26 septembre 2015

Why does my Ember computed property on hasMany relationship not update?

I have an Ember Data model as:

export default DS.Model.extend({
  name: DS.attr('string'),
  friends: DS.hasMany('friend',{async:true}),
  requestedFriendIds: Ember.computed('friends',function(){
    return this.get('friends').then(function(friends){
      return friends.filterBy('status','Requested').mapBy('id');
    });
  })
 });

I have a route setup that uses it:

export default Ember.Route.extend({
  model: function(params){
    return Ember.RSVP.hash({
      memberProfile:this.store.find('member-profile', params.memberprofile_id).then(function(memberProfile)
      {
        return Ember.RSVP.hash({
          requestedFriendIds:memberProfile.get('requestedFriendIds'),
          UserId:memberProfile.get('user.id'),
          Id:memberProfile.get('id')
        });
      }),
    });
   }
 });

},

And htmlbars that utilize the route model. My computed property is always correctly called on a reload, but isn't refreshed on a user action 'Add Friend', which changes the store by adding a friend and the profile.friends' record like this:

actions:
{
  addFriend:function(profile_id,)
  {
    this.store.findRecord('member-profile',memberProfile).then(function(member){
      var friend = this.store.createRecord('friend',
      {
        member:member,
        status:'Requested',
        timestamp: new Date(),
      });
      friend.save();
      member.get('friends').pushObject(friend);
      member.save();
  }.bind(this));
}

}

Some notes: I've tried the computed property on 'friends','friends.[]'. The underlying data is correctly updated in the backing store (EmberFire). I've debugged into EmberData and I see that the property changed notifications invalidation code is called. This is only a selection of the code...

What am I missing...? Is there a better way to approach this?




Aucun commentaire:

Enregistrer un commentaire