How to access the Ember Data Store from the console?

天涯浪子 提交于 2020-05-25 17:13:26

问题


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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/32971707/how-to-access-the-ember-data-store-from-the-console

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!