How to access Ember container from the window?


#1

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');

#2

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?


#3

Just install: ember-export-application-global


#4

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.


#5

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


#6

How would you improve this: https://guides.emberjs.com/v3.1.0/applications/dependency-injection


#7

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