10

In Ember 2+, does anyone know how to get a reference to the Ember Store in order to troubleshoot Model mapping in the javascript console?

It was possible through App.__container__.lookup in Ember 1, but this doesn't exist anymore, and it's bloody hard to find documentation on this.

Thanks

Fabien Benoit-Koch
  • 2,784
  • 2
  • 21
  • 33
  • Couldn't you just use the Ember Inspector? – nem035 Oct 06 '15 at 13:51
  • Well it's only good for "normally" loaded entities. I've got a model that doesn't load correctly (lots of null fields), i suspect a mapping problem but have no visible errors in the console, so i wanted to manually trigger a store load . – Fabien Benoit-Koch Oct 06 '15 at 13:53

2 Answers2

20

If you look in your package.json, you should see a ember-export-application-global package that's installed by default (if not, install it). This will export your application not to the global App object, but to a global object that's named after your app. So you might have window.TodoList or window.ShoppingCart instead of window.App. From there you can use this line (similar to Ember 1.x.x):

AppName.__container__.lookup('service:store')

You can also do what I do and create an instance initializer for it:

export default {
    name: 'store-on-app',
    after: 'ember-data',
    initialize(instance) {
        const application = instance.container.lookup('application:main');
        const store = instance.container.lookup('service:store');
        application.set('store', store);
    }
}

Then you can just use AppName.store.

GJK
  • 37,023
  • 8
  • 55
  • 74
8

If you don't want to install a separate package to access your app in the console, you can do it through window.Ember.Namespace.NAMESPACES. For example, something you can run in the console to find your app instance is:

var app = Ember.A(Ember.Namespace.NAMESPACES).filter(n => {return n.name === 'your-app-name'})[0];

From here, you can access the store on the app's container as explained by @GJK

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

I used this for debugging an Ember app in production which didn't have its container registered on the window. I found it out by looking through the ember-inspector source code, since it always has access to the container.

https://github.com/emberjs/ember-inspector/blob/2237dc1b4818e31a856f3348f35305b10f42f60a/ember_debug/vendor/startup-wrapper.js#L201

kevinyuliawan
  • 156
  • 1
  • 7