Ember.js 访问存储的服务的计算属性

Ember.js 访问存储的服务的计算属性,ember.js,promise,ember-data,Ember.js,Promise,Ember Data,我编写了一个用于加载通知的服务: import Ember from 'ember'; export default Ember.Service.extend({ sessionUser: Ember.inject.service(), store: Ember.inject.service(), read() { let currentUserId = this.get('sessionUser.user.id'); return this.get('store

我编写了一个用于加载通知的服务:

import Ember from 'ember';

export default Ember.Service.extend({
  sessionUser: Ember.inject.service(),
  store: Ember.inject.service(),

  read() {
    let currentUserId = this.get('sessionUser.user.id');
    return this.get('store').query('notification', {
      userId: currentUserId,
      read: true
    });
  },

  unread() {
    let currentUserId = this.get('sessionUser.user.id');
    return this.get('store').query('notification', {
      userId: currentUserId,
      read: false
    });
  }
});
当有未读通知时,我想更改导航栏中图标的颜色。导航栏是一个组件:

import Ember from 'ember';

export default Ember.Component.extend({
  notifications: Ember.inject.service(),
  session: Ember.inject.service(),

  hasUnreadNotifications: Ember.computed('notifications', function() {
    return this.get('notifications').unread().then((unread) => {
      return unread.get('length') > 0;
    });
  })
});
然后,模板使用
hasUnderNotifications
属性来决定是否应该使用highlight类:

<span class="icon">
  <i class="fa fa-bell {{if hasUnreadNotifications 'has-notifications'}}"></i>
</span>


但是,它不起作用。尽管调用存储并返回通知,
hadunadNotifications
不会解析为布尔值。我认为这是因为它返回了一个承诺,而模板无法处理这个问题,但我不确定

问题

  • 用这样的服务来包装商店是一种特殊的余烬吗。我这样做是因为在应用程序路由中加载通知只是为了显示计数感觉很笨拙
  • 为什么
    hasUnderNotifications
    不返回布尔值
  • 是否可以使
    读取
    未读
    属性而不是函数,以便可以在服务中创建计算属性来计算计数

从计算属性返回承诺将不起作用。计算属性不支持承诺。要使其正常工作,您需要返回DS.PrmoiseObject或DS.PromiseArray

您可以从中阅读其他可用选项

import Ember from 'ember';
import DS from 'ember-data';

export default Ember.Component.extend({
    notifications: Ember.inject.service(),
    session: Ember.inject.service(),

    hasUnreadNotifications: Ember.computed('notifications', function() {
        return DS.PromiseObject.create({
            promise: this.get('notifications').unread().then((unread) => {
                return unread.get('length') > 0;
            })
        });
    })

});