在Ember中更新计算属性

时间:2015-03-19 15:16:57

标签: ember.js

我有一个简单的应用程序,其中用户有许多通知,通知具有readAt属性,我想显示未读通知的计数。我有以下代码:

用户模型:

import DS from 'ember-data';

export default DS.Model.extend({

    notifications: DS.hasMany('notification', {async: true}),

    unreadNotifications: function(){
        return this.get('notifications').filter(function(notification){
            return notification.get('readAt') === null;
        });
    }.property('notifications')

});

模板:

<span class="badge messages-count">
    {{currentUser.unreadNotifications.length}}
</span>

我正在动态推送新通知。如果我将其设置为只计算通知(即{{currentUser.notifications.length}},但计算的unreadNotifications属性不会更新,除非刷新页面,这样做非常有效。我是否错过了正确绑定这些数据的方法?

非常感谢

1 个答案:

答案 0 :(得分:1)

由于notifications是数组类型属性,因此您需要使用定义为here@each语法。

因此,您的unreadNotifications属性应定义如下:

unreadNotifications: function(){
    return this.get('notifications').filter(function(notification){
        return notification.get('readAt') === null;
    });
}.property('notifications.@each.readAt')
相关问题