2013-10-16 34 views
5

由於Ember.Data 1.0 Beta,我們必須使用store.find('model'),而不是App.Model.find()。如何訪問App對象內的存儲對象?Ember.js:如何從應用程序對象訪問存儲

var App = Ember.Application.create({ 
    auth: function() { 
    return new Ember.RSVP.Promise(function(resolve, reject) { 
     // ... get token somehow ... 
     // how to get store? 
     this.store.find('user').then(function(users) { 
     App.set('user', users.get('firstObject'); 
     resolve(); 
     }, function(err) { 
     reject(); 
     }); 
    }); 
    } 
}); 

App.deferReadiness(); 
App.auth().then(App.advanceReadiness()); 

回答

6

您可以創建一個將存儲注入應用程序對象的初始化程序。

App.initializer({ 
    name: 'Inject Store', 
    initialize: function(container, application) { 
     container.injection('application:main', 'store', 'store:main'); 
    } 
}); 

之後,您可以在應用程序中使用this.get('store')。當然,您可以通過簡單地將商店(通過container.lookUp檢索)設置在應用程序對象上來避開容器,但這會破壞容器的用途。

5

我已經使用這個獲得我的主應用程序對象的商店。這不太好,但它完成了工作。

var store = App.__container__.lookup('store:main') 
+0

應用程序容器不打算公開使用。來自Yehuda Katz的關於這個問題的[這篇文章](https://github.com/emberjs/ember.js/commit/5becdc4467573f80a5c5dbb51d97c6b9239714a8)。您應該根據以前的答案注入商店。 – blimmer

相關問題