问题
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