Class Owner
publicFramework objects in an Ember application (components, services, routes, etc.) are created via a factory and dependency injection system. Each of these objects is the responsibility of an "owner", which handles its instantiation and manages its lifetime.
An Owner
is not a class you construct; it is one the framework constructs
for you. The normal way to get access to the relevant Owner
is using the
getOwner
function.
factoryFor (fullName) FactoryManager public
Inherited from BasicContainer packages/@ember/-internals/owner/index.ts:238
- fullName
- String
- returns
- FactoryManager
Given a FullName
, of the form "type:name"
return a FactoryManager
.
This method returns a manager which can be used for introspection of the factory's class or for the creation of factory instances with initial properties. The manager is an object with the following properties:
class
- The registered or resolved class.create
- A function that will create an instance of the class with any dependencies injected.
For example:
import { getOwner } from '@ember/application';
let owner = getOwner(otherInstance);
// the owner is commonly the `applicationInstance`, and can be accessed via
// an instance initializer.
let factory = owner.factoryFor('service:bespoke');
factory.class;
// The registered or resolved class. For example when used with an Ember-CLI
// app, this would be the default export from `app/services/bespoke.js`.
let instance = factory.create({
someProperty: 'an initial property value'
});
// Create an instance with any injections and the passed options as
// initial properties.
Any instances created via the factory's .create()
method must be destroyed
manually by the caller of .create()
. Typically, this is done during the creating
objects own destroy
or willDestroy
methods.
lookup (fullName, options) Any public
Inherited from BasicContainer packages/@ember/-internals/owner/index.ts:189
- fullName
- String
- options
- RegisterOptions
- returns
- Any
Given a fullName return a corresponding instance.
The default behavior is for lookup to return a singleton instance. The singleton is scoped to the container, allowing multiple containers to all have their own locally scoped singletons.
let registry = new Registry();
let container = registry.container();
registry.register('api:twitter', Twitter);
let twitter = container.lookup('api:twitter');
twitter instanceof Twitter; // => true
// by default the container will return singletons
let twitter2 = container.lookup('api:twitter');
twitter2 instanceof Twitter; // => true
twitter === twitter2; //=> true
If singletons are not wanted an optional flag can be provided at lookup.
let registry = new Registry();
let container = registry.container();
registry.register('api:twitter', Twitter);
let twitter = container.lookup('api:twitter', { singleton: false });
let twitter2 = container.lookup('api:twitter', { singleton: false });
twitter === twitter2; //=> false
register (fullName, factory, options) public
Inherited from BasicRegistry packages/@ember/-internals/owner/index.ts:98
- fullName
- String
type:name (e.g., 'model:user')
- factory
- Factory|object
(e.g., App.Person)
- options
- Object
(optional) disable instantiation or singleton usage
Registers a factory that can be used for dependency injection (with
inject
) or for service lookup. Each factory is registered with
a full name including two parts: type:name
.
A simple example:
import Application from '@ember/application';
import EmberObject from '@ember/object';
let App = Application.create();
App.Orange = EmberObject.extend();
App.register('fruit:favorite', App.Orange);
Ember will resolve factories from the App
namespace automatically.
For example App.CarsController
will be discovered and returned if
an application requests controller:cars
.
An example of registering a controller with a non-standard name:
import Application from '@ember/application';
import Controller from '@ember/controller';
let App = Application.create();
let Session = Controller.extend();
App.register('controller:session', Session);
// The Session controller can now be treated like a normal controller,
// despite its non-standard name.
App.ApplicationController = Controller.extend({
needs: ['session']
});
Registered factories are instantiated by having create
called on them. Additionally they are singletons, each time
they are looked up they return the same instance.
Some examples modifying that default behavior:
import Application from '@ember/application';
import EmberObject from '@ember/object';
let App = Application.create();
App.Person = EmberObject.extend();
App.Orange = EmberObject.extend();
App.Email = EmberObject.extend();
App.session = EmberObject.create();
App.register('model:user', App.Person, { singleton: false });
App.register('fruit:favorite', App.Orange);
App.register('communication:main', App.Email, { singleton: false });
App.register('session', App.session, { instantiate: false });