How to get store instance in intializer?

How to get the store instance in initializer?

i ended up doing this:

var store = application.__container__.lookup('service:store');

but looks hacky to me. What is the correct way to get store instance.

-Thanks

It would belong in an instance initializer, not an initializer. This is all assuming you’re on >= Ember 1.12

// app/instance-initializers/foo.js
export default {
  name: 'foo',
  // schedule invoking instance initializer after ember-data's
  after: 'ember-data',
  initialize(application) {
    var container = application.lookup ? application : application.container;
    var store = container.lookup('service:store');
    // ...
  }
};
2 Likes

Thank you for the answer, but I am using the latest 2.0.2 version with ember data 2.0.0. Sorry, i did not mention it before.

The code applies to (as stated) >= 1.12, which includes 2.0.x (greater than or equal to).

1 Like

@elithrar: that doesn’t work. it gives container is undefined

Not sure what’s wrong, I’m on 2.0.2 and validated that it works as intended. Are you sure you’re dealing with an instance-initializer and not an initializer. Feel free to post an example that reproduces the issue.

oh. got it. but, ember-cli doesn’t have a command to create the instance-initializers. should I just create the file in instance-initializer folder?

Yes, just be sure to make it plural: instance-initializers