How to obtain the store in an application initializer?

I need to query from the store before the application boots and I want to use the app.deferReadiness() and app.advanceReadiness(). I’d like to use the store because it gets returned in json api format and i’d rather use ember data for this instead of writing a jquery.ajax call.

I’ve tried app.lookup and container.lookup and this function does not exist. Will I have to resort to private methods to make this happen?

I basically need to do the following:

export function initialize(container, app) {
  app.deferReadiness();
  
  /**
   * console.log(container.lookup); // undefined
   * console.log(app.lookup); // undefined
   */
  const store = container.lookup('store:main');

  store.findRecord('config', '105-1').then(data => {
    // handle stuff
    app.advanceReadiness();
  }).catch(err => {
    // handle error
    app.advanceReadiness();
  });
}

export default {
  name: 'my-initializer',
  after: 'store',
  initialize
};

Perhaps do something like this:

This.get(‘store’) is not available in an initializer

Hum. I try to understand this but store seems to be injected. Perhaps write after: "store"?? (Edit: I see you tried that)

See also this old commends:

https://github.com/emberjs/data/pull/1585#discussion_r8404415

Answer: You can’t in an application initializer, because the application has not been initialized yet. Instead you should use an instance initializer, which does provide you with the desired lookup function.

See: Initializers - Application Concerns - Ember Guides

1 Like