📎 Webclip
Dependency injection in Ember.js - Going deeper
The post expands on Ember’s internal dependency injection setup. It shows where the app container is created, how objects from the container can access it, and how public APIs can replace direct use of the private __container__ property.
Reading notes#
- Ember creates an internal container when an app starts, and that container is the basis for its dependency setup.
optionsForTypecan define lookup behavior for a type, such as making components and views non-singletons.- The default for singleton behavior is
true, socontainer.register('store:main', Store, { singleton: true })matchesapplication.register('store', Store). - Objects created by the container have a
containerproperty, which lets routes callthis.container.lookupwithout relying on__container__. instantiate: falseis used for templates and helpers, since they are functions and do not need instantiation.container.lookupfirst returns a cached singleton if one exists, then instantiates the requested full name, caches it if needed, and finally returns the result.container.lookupalso checks that the full name follows thetype:namesyntax before doing the lookup.
