inject (factoryNameOrType, property, injectionName) public
Define a dependency injection onto a specific factory or all factories of a type.
When Ember instantiates a controller, view, or other framework component it can attach a dependency to that component. This is often used to provide services to a set of framework components.
An example of providing a session object to all controllers:
1 2 3 4 5 6 7 8 9 10 11 12 13 | let App = Ember.Application.create(); let Session = Ember.Object.extend({ isAuthenticated: false }); // A factory must be registered before it can be injected App.register( 'session:main' , Session); // Inject 'session:main' onto all factories of the type 'controller' // with the name 'session' App.inject( 'controller' , 'session' , 'session:main' ); App.IndexController = Ember.Controller.extend({ isLoggedIn: Ember.computed.alias( 'session.isAuthenticated' ) }); |
Injections can also be performed on specific factories.
1 2 3 | App.inject(<full_name or type>, <property name>, <full_name>) App.inject( 'route' , 'source' , 'source:main' ) App.inject( 'route:application' , 'email' , 'model:email' ) |
It is important to note that injections can only be performed on classes that are instantiated by Ember itself. Instantiating a class directly (via create
or new
) bypasses the dependency injection system.
Note: Ember-Data instantiates its models in a unique manner, and consequently injections onto models (or all models) will not work as expected. Injections on models can be enabled by setting EmberENV.MODEL_FACTORY_INJECTIONS
to true
.
Parameters:
-
factoryNameOrType
String
-
property
String
-
injectionName
String
Please login to continue.