【发布时间】:2013-12-30 16:20:25
【问题描述】:
我有一个用户模型,它有 2 个关系(myFriends 和 friendsWithMe)。交集是代表真实朋友的用户数组。我已经用 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'),
问题是我现在有另一个依赖于这个的计算属性:
/**
* 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')
当我现在调试 myFriendShipStatus 时,promise 尚未解决,“friends”数组还没有条目。
我还尝试将我的朋友函数更改为 ember.computed.intersect,然后看起来像这样:
friends: function() {
return Ember.computed.intersect('myFriends', 'friendsWithMe')
}.property('myFriends.@each', 'friendsWithMe.@each'),
但是我从这行得到一个异常:
if(this.get('friends').contains(this.container.lookup('user:current'))){
因为 ArrayComputedProperty 没有包含函数。
如何让我的朋友功能与 myFriendShipStatus 一起工作?我更喜欢使用 Ember.computed.intersect,但我不知道如何检查它的值。
【问题讨论】:
标签: ember.js