cyclomarc
cyclomarc

Reputation: 2012

Ember: store not available in Application.initializer

I have implemented an authentication based on this article

I thus create an App.Session object at application initialization:

    Ember.Application.initializer({
      name: 'session',

      initialize: function (container, application) {
        App.Session = Ember.Object.extend({
          init: function () {
            this._super();
            this.set('authToken', $.cookie('auth_token'));
            this.set('authAccountId', $.cookie('auth_accountId'));
            this.set('authAccountLanguage', $.cookie('auth_accountLanguage'));
          },

          authAccountIdChanged: function () {
            var authAccountId = this.get('authAccountId');
            $.cookie('auth_accountId', authAccountId);

            //Load the actual account record from the server if the authAccountId is set 
            //Used to have for example the full name or other properties of the account
            if (!Ember.isEmpty(authAccountId)) { 
              this.set('authAccount', this.store.find('account', authAccountId));
            }
          }.observes('authAccountId'),
...

I have an observer on authAccountId; thus each time the accountId (the id of the logged in user) is changed, I want to retrieve all details of that user (full name, preferences, etc.).

Before Ember data version 1.0.0, I was using:

this.set('authAccount', App.Account.find(authAccountId));

And this worked. Now I use: this.set('authAccount', this.store.find('account', authAccountId));

And I receive the error: Uncaught TypeError: Cannot call method 'find' of undefined. Also in debugger, this.get('store') results in undefined. I have the impression that the store is not available in Application.initializer. Can somebody help resolving this issue ?

Upvotes: 2

Views: 1444

Answers (1)

Marcio Junior
Marcio Junior

Reputation: 19128

You can use container.lookup('store:main') this will return the store registered in container:

var store = container.lookup('store:main');
this.set('authAccount', store.find('account', authAccountId));

Upvotes: 9

Related Questions