2015-10-07 139 views
1

我有我通過使用Ember.inject灰燼服務要組件

應用程序/通知/ service.js

import Ember from 'ember'; 

export default Ember.Service.extend({ 
    store: Ember.inject.service('store'), 
    notifications: function() { 
    return this.get('store').findAll('notification'); 
    }, 
}); 

然後我的應用程序/組件/應用程序的通知組件調用服務/component.js:

import Ember from 'ember'; 

export default Ember.Component.extend({ 
    notifications: Ember.inject.service('notifications'), 
}); 

然後我在/app/components/app-notifications/template.hbs

組件模板

但我不能讓它顯示我的模型的數據?我似乎無法在文檔中找到關於此的更多信息,所以我想知道是否有人知道如何做到這一點?

解決方案

好了,我得到了它與

import Ember from 'ember'; 

export default Ember.Service.extend({ 
    store: Ember.inject.service('store'), 
    notifications: function() { 
    return this.get('store').findAll('notification'); 
    }.property(), 
}); 
+0

的'store'是一種服務,所以你不需要在其他服務中使用它。此外,'store.findAll'返回一個承諾,並在服務中沒有足夠的時間來解決它。 Ember等待在'routes'的'model'中解決的承諾。 –

+0

顯然要獲得一個服務內部的燼數據存儲我不得不稱之爲http://stackoverflow.com/questions/29881771/how-to-inject-store-in-service-ember-js 因此,對於第二部分,這是否意味着我需要調用afterModel或.findAll('notification')。then(function(){? –

+1

您可以使用Ember.RSVP.hash({}返回多個模型。 )''' –

回答

2

工作是否有必要實施notifications作爲一種服務?

的常用方法做的工作是使用一個計算屬性:

import Ember from 'ember'; 

export default Ember.Component.extend({ 
    notifications: Ember.computed(function() { 
    return this.store.findAll('notification'); 
    }) 
}); 
+0

從與其他人的討論看來,這似乎是正確的方式,它基本上是應用程序和顯示在管理員的每個頁面上,計劃是使用一個套接字並且有實時的通知 –

+0

好的,所以我得到它的工作 import'Ember from'ember'; 導出默認值Ember.Service.extend({store.Ember.inject.service('store'), )通知:function(){ 返回this.get('store')。findAll('notification'); } .property(), }); –