2013-10-24 27 views
4

我已經不工作了以下初始化:燼初始化沒有注入到店組件

Ember.onLoad 'Ember.Application', (Application) -> 
    Ember.Application.initializer 
    name: 'storeToComponent' 
    initialize: (container, application) -> 
     application.inject('component', 'store', 'store:main') 

該組件沒有商店屬性。我已經嘗試過很多東西,例如:

before: 'registerComponents' 

但是沒什麼效果。

我在做什麼錯?

回答

6

你的組件總是會在某些情況下(如控制器),因此您可以在您的組件做訪問來自這方面的店:

App.MyComponent = Ember.Component.extend({ 
    actions: { 
    foo: function() { 
     var store = this.get('targetObject.store'); 
    } 
    } 
}); 

但是,應該說,由於組件被認爲是孤立的,你應該將數據傳遞給它們,而不是從特定的商店創建依賴。 這就是說,如果你真的想有商店注入到你的組件,你可以嘗試做這樣的:

Ember.onLoad 'Ember.Application', (Application) -> 
    Ember.Application.initializer 
    name: 'storeToComponent' 
    before: 'registerComponents' 
    initialize: (container, application) -> 
     application.register('store:main', App.Store) 
     application.inject('component', 'store', 'store:main') 

希望它能幫助。

+0

對於Ember/Ember Data的更新版本,您應該在初始化程序中指定'after:'store'',並移除'application.register('store:main',App.Store)'行。 – locks