在 Ember 中更新计算属性

Updating computed properties in Ember

我有一个简单的应用程序,其中用户有许多通知,通知具有 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 属性 不会更新,除非刷新页面,否则这将非常有效。我是否错过了一些正确绑定这些数据的方法?

非常感谢

由于 notifications 是数组类型 属性,您需要使用 @each 定义的语法 here.

所以,你的 unreadNotifications 属性 应该定义如下:

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