2

I have a initializer like this:

import Ember from 'ember';
import Session from 'simple-auth/session';

var SessionWithCurrentUser = Session.extend({
  store: Ember.inject.service(),
  currentUser: function() {
    console.log(this.get('store'));
    console.log(this.store);
    console.log(this.container.lookup('service:store'));
  }.property('secure.access_token')
});

export default {
  name: 'custom-session',
  after: 'ember-data',
  initialize(registry) {
    registry.register('session:withCurrentUser', SessionWithCurrentUser);
  }
};

currentUser gets called on user interaction, long after my app has finished loading. Only the last container lookup gives the store, the other 2 is an object:

{
 _lastData: Object,
 key: "ember_simple_auth:session"
 [..]
}

What's going on? Why can't I inject the store?

Kit Sunde
  • 35,972
  • 25
  • 125
  • 179
  • because "store" is also used by `simple-auth` as localStorage. Example of injecting here: http://stackoverflow.com/a/30894082/4950029 – artych Aug 09 '15 at 07:44
  • @Artych Ah that makes sense. I wonder what's the least surprising injecting `_store` or the container lookup. I suppose I'll just stick with the lookup. If you put that in an answer I'll accept it. – Kit Sunde Aug 09 '15 at 07:47
  • It worth to check if `_store: Ember.inject.service('store')` or something like this works for modern ED for a good answer. I have no time to dig in. – artych Aug 09 '15 at 07:54
  • @Artych No matter, they are changing it to a service and it'll work as expected in the near future. – Kit Sunde Aug 09 '15 at 07:59

1 Answers1

2

It's because store in the current version of simple-auth is being overridden by an instance-initializer with the session storage. The next major version of simple-auth will turn the session storage into a service and we'll be able to do:

import Ember from 'ember';

const { service } = Ember.inject;

export default Ember.Service.extend({
  session: service('session'),
  store: Ember.inject.service(),

  account: Ember.computed('session.content.secure.account_id', function() {
    const accountId = this.get('session.content.secure.account_id');
    if (!Ember.isEmpty(accountId)) {
      return DS.PromiseObject.create({
        promise: this.get('store').find('account', accountId)
      });
    }
  })
});

From the dummy app, once https://github.com/simplabs/ember-simple-auth/pull/602 is merged.

Kit Sunde
  • 35,972
  • 25
  • 125
  • 179