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
- 是否可以使
和读取
属性而不是函数,以便可以在服务中创建计算属性来计算计数未读
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;
})
});
})
});