How to obtain the store in an application initializer?


#1

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
};

#2

Perhaps do something like this:


#3

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


#4

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:


#5

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: https://guides.emberjs.com/v2.8.0/applications/initializers/#toc_application-instance-initializers