【问题标题】:Intersection of promises and dependent computed propertiesPromise 和相关计算属性的交集
【发布时间】: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


    【解决方案1】:

    在第一个例子中它返回一个空数组的原因如下。在 Ember.RSVP.all() 调用之后,将立即执行 return 语句,返回一个空的 ret 数组。在未来的某个时候,RSVP 承诺会实现,但是由于friends 函数已经返回了空数组,所以这将不起作用。

    你可以这样做:

    // 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),

    【讨论】:

    • 当正确使用 Ember.computed.intersect 时,它工作得非常好,至少朋友们是这样。但是在调试 myFriendShipStatus 时,“this.get('friends')”仍然是一个空数组。但我认为当我的相交完成时会自动重新计算友谊状态。
    • 也许尝试添加@each ?认为这是多余的
    • 现在该功能已正确触发,但包含检查不起作用。我已经检查了调试器中的数据,并且我的计算属性包含当前用户,但包含的内容不知何故无法正常工作。
    • 你能发布 this.get('friends') 和 this.container.lookup('user:current') 的输出吗?另外,我会说,有一个friendshipStatus(otherUser) 方法更有意义。
    • 如果你检查 user:current,你会发现它是一个 promise 对象。这就是contains 函数失败的原因。因此,您必须确保在初始化程序中注册了一个实际的用户对象。例如通过container.register('user:current', store.createRecord('user', App.User.FIXTURES[0]));
    猜你喜欢
    • 2011-12-27
    • 1970-01-01
    • 2019-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-17
    • 2016-10-03
    相关资源
    最近更新 更多