Ember.js 余烬服务到组件
我有一个使用Ember.inject的组件调用的服务 app/notifications/service.jsEmber.js 余烬服务到组件,ember.js,ember-data,ember-cli,Ember.js,Ember Data,Ember Cli,我有一个使用Ember.inject的组件调用的服务 app/notifications/service.js import Ember from 'ember'; export default Ember.Service.extend({ store: Ember.inject.service('store'), notifications: function() { return this.get('store').findAll('notification'); },
import Ember from 'ember';
export default Ember.Service.extend({
store: Ember.inject.service('store'),
notifications: function() {
return this.get('store').findAll('notification');
},
});
然后我的应用程序/components/app通知/component.js:
import Ember from 'ember';
export default Ember.Component.extend({
notifications: Ember.inject.service('notifications'),
});
然后我的组件模板位于/app/components/app notifications/template.hbs
{{notifications}}
<ul>
{{#each notifications as |notification|}}
<li>{{notification.title}} <small>{{moment-from-now dateAdded}}</small> </li>
{{/each}}
</ul>
{{notifications}
{{{#每个通知作为|通知}
- {{notification.title}{{moment from now dateAdded}}
{{/每个}}
但是我不能让它显示我模型中的数据?我在文档中似乎找不到关于这方面的太多信息,因此我想知道是否有人知道如何做到这一点?是否有必要将
通知作为一项服务实施
执行此作业的常用方法是使用计算属性:
import Ember from 'ember';
export default Ember.Component.extend({
notifications: Ember.computed( function () {
return this.store.findAll('notification');
})
});
好的,我和他一起工作
import Ember from 'ember';
export default Ember.Service.extend({
store: Ember.inject.service('store'),
notifications: function() {
return this.get('store').findAll('notification');
}.property(),
});
store
也是一项服务,因此您不需要在其他服务中使用它。另外,store.findAll
返回一个承诺,在服务中没有足够的时间来解决它。Ember等待在路由
的模型
中解决承诺。显然,要在服务中获得Ember数据存储,我必须调用它,所以对于第二部分,这是否意味着我需要调用afterModel或.findAll('notification')。然后(函数(){?您可以使用Ember.RSVP.hash({})从路由返回多个模型
我不想返回路线上的模型,我宁愿保留在服务->组件中。请不要将答案放入问题中。我添加了您的答案,作为您是否愿意自行回答ping me,我将删除下面的wiki。从与其他人的讨论中,这似乎是正确的方法。它基本上位于应用程序上,并显示在屏幕上在管理中的每个页面上。计划是使用套接字和实时通知。好的,所以我让它从“Ember”导入Ember;导出默认Ember.Service.extend({store:Ember.inject.Service('store'),notifications:function(){return this.get('store')。findAll('notification');}.property)(), });