Intersection of promises and dependent computed properties

I have a user model that has 2 relationships (myFriends and friendsWithMe). An intersection is an array of users that represents real friends. I solved this computatation using RSVP.all:

friends: function() {
    var ret = [];
    Ember.RSVP.all([this.get('myFriends'), this.get('friendsWithMe')]).then(function(results) {
        ret.pushObjects(_.intersection(results[0].get('content'), results[1].get('content'))) ;
    });
    return ret;
}.property('myFriends.@each', 'friendsWithMe.@each'),

Now the problem is that I have another computed property that depends on this:

/**
 *  Gives the relation between two User
 *  4: has requested your friendship
 *  3: Yourself
 *  2: Friends
 *  1: FriendShip Request
 */
myFriendshipStatus: function() {
if(this.get('friends').contains(this.container.lookup('user:current'))){
    return 2;
} else if(this.get('friendsWithMe').contains(this.container.lookup('user:current'))){
    return 4;
} else if(this.get('myFriends').contains(this.container.lookup('user:current'))){
    return 1;
} else if (this.get('id') === this.container.lookup('user:current').get('id')){
    return 3;
} else {
    return 0;
}
}.property('friends.@each')

When I debug myFriendShipStatus, promises are not allowed, and there are no entries in the friends array.

I also tried changing the function of my friends to ember.computed.intersect, which would look like this:

friends: function() {
    return Ember.computed.intersect('myFriends', 'friendsWithMe')
}.property('myFriends.@each', 'friendsWithMe.@each'),

But then I get an exception from this line:

if (. This.get ('friends') contains (this.container.lookup ('user: current'))) {

Since ArrayComputedProperty does not have a function, it contains.

myFriendShipStatus? Ember.computed.intersect, , .

0
1

, , . Ember.RSVP.all() return , ret. - RSVP , , .

:

// See http://emberjs.com/api/#method_A
friends: Ember.A,

recalculateFriends: function() {
  Ember.RSVP.all([this.get('myFriends'), this.get('friendsWithMe')]).then(function(results) {
    var myFriends = results[0], friendsWithMe = results[1];
    this.set('friends', _.intersection(myFriends.get('content'), friendsWithMe.get('content')));
  });
}.property('myFriends', 'friendsWithMe'), // @each is redundant here

myFriendshipStatus: function() {
  // Will be recalculated when the friends array changes (which will in turn recalculate when myFriends or friendsWithMe changes
}.property('friends'),

... , Ember.computed.intersect : P :

friends: Ember.computed.intersect('myFriends', 'friendsWithMe')

(. : http://emberjs.com/api/#method_computed_intersect),

0

All Articles