How to access Ember container from the window?

Hi!

I found I had this as a question and I found the answer with some help. I hope this helps someone one day. You probably shouldn’t do this by the way - accessing private methods generally is a bad practice, but it can be a life saver if you have to make a deadline.

let app = Ember.Namespace.NAMESPACES.find(namespace => namespace instanceof Ember.Application)
let router = app.__container__.lookup('service:router');
1 Like

Thanks for writing this up!

I do think it is worth discussing how we could make this available in a sane way for debugging purposes. Perhaps a flag (whether it be environment or elsewhere) to tell Ember that we are entering debug mode and want access to the application in the devtools perhaps even when inspector isn’t available. Or some other debugging API.

Does anyone have any thoughts on how that might work?

1 Like

Just install: ember-export-application-global

2 Likes

Maybe that if your build isn’t production, then you can always access the application and thus the container with something like ember.app?

It’s true that it’s not a lot of work to inject App into window using an initializer, but it’s definitely something that newcomers wouldn’t know to do. Additionally, there’s a lack of documentation about the container in general, which is a shame, because it’s a huge part of understanding how Ember actually works.

1 Like

I didn’t know about this actually. Very neat! Thanks @Gaurav0!

How would you improve this: Dependency Injection - Application Concerns - Ember Guides

You can also use the Ember Inspector to view and grab stuff that is is in the container.

1 Like