2

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 ?

1 Answer 1

9

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));
Sign up to request clarification or add additional context in comments.

3 Comments

What would you do if container.lookup('store:main') returns undefined?
Hmm this is strange container.lookup('store:main') should be still available.
@BenMorganIO FWIW, that problem can probably be solved in that you add after: 'store' in the export default of the initializer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.