Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember.js: How to get access to store from App object

Since Ember.Data 1.0 Beta we have to use store.find('model') instead App.Model.find(). How to get access to store object inside App object?

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());
like image 458
Nikolay Avatar asked Oct 16 '13 10:10

Nikolay


1 Answers

You can create an initializer that injects the store into the application object.

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

Afterwards you can use this.get('store') in the application. Of course you can circumvent the container by simply setting the store (retrieved via container.lookUp) on the application object, but that defeats the purpose of the container.

like image 134
Dominik Schmidt Avatar answered Oct 21 '22 10:10

Dominik Schmidt