The results of the 2017 Ember Community Survey !

Registry Class packages/container/lib/registry.js:7


PRIVATE

A registry used to store factory and option information keyed by type.

A Registry stores the factory and option information needed by a Container to instantiate and cache objects.

The API for Registry is still in flux and should not be considered stable.

Show:

container

(options) Container private

Creates a container based on this registry.

Parameters:

options Object

Returns:

Container
created container

describe

(fullName) String private

A hook that can be used to describe how the resolver will attempt to find the factory.

For example, the default Ember .describe returns the full class name (including namespace) where Ember's resolver expects to find the fullName.

Parameters:

fullName String

Returns:

String
described fullName

expandLocalLookup

(fullName, options) String private

Given a fullName and a source fullName returns the fully resolved fullName. Used to allow for local lookup.

1
2
3
4
let registry = new Registry();

// the twitter factory is added to the module system
registry.expandLocalLookup('component:post-title', { source: 'template:post' }) // => component:post/post-title

Parameters:

fullName String
options [Object]
source [String]
the fullname of the request source (used for local lookups)

Returns:

String
fullName

factoryInjection

(factoryName, property, injectionName) private

Defines factory injection rules.

Similar to regular injection rules, but are run against factories, via Registry#lookupFactory.

These rules are used to inject objects onto factories when they are looked up.

Two forms of injections are possible:

  • Injecting one fullName on another fullName
  • Injecting one fullName on a type

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
let registry = new Registry();
let container = registry.container();

registry.register('store:main', Store);
registry.register('store:secondary', OtherStore);
registry.register('model:user', User);
registry.register('model:post', Post);

// injecting one fullName on another type
registry.factoryInjection('model', 'store', 'store:main');

// injecting one fullName on another fullName
registry.factoryInjection('model:post', 'secondaryStore', 'store:secondary');

let UserFactory = container.lookupFactory('model:user');
let PostFactory = container.lookupFactory('model:post');
let store = container.lookup('store:main');

UserFactory.store instanceof Store; //=> true
UserFactory.secondaryStore instanceof OtherStore; //=> false

PostFactory.store instanceof Store; //=> true
PostFactory.secondaryStore instanceof OtherStore; //=> true

// and both models share the same source instance
UserFactory.store === PostFactory.store; //=> true

Parameters:

factoryName String
property String
injectionName String

factoryTypeInjection

(type, property, fullName) private

Used only via factoryInjection.

Provides a specialized form of injection, specifically enabling all factory of one type to be injected with a reference to another object.

For example, provided each factory of type model needed a store. one would do the following:

1
2
3
4
5
6
7
8
9
10
let registry = new Registry();

registry.register('store:main', SomeStore);

registry.factoryTypeInjection('model', 'store', 'store:main');

let store = registry.lookup('store:main');
let UserFactory = registry.lookupFactory('model:user');

UserFactory.store instanceof SomeStore; //=> true

Parameters:

type String
property String
fullName String

has

(fullName, options) Boolean private

Given a fullName check if the container is aware of its factory or singleton instance.

Parameters:

fullName String
options [Object]
source [String]
the fullname of the request source (used for local lookups)

Returns:

Boolean

injection

(factoryName, property, injectionName) private

Defines injection rules.

These rules are used to inject dependencies onto objects when they are instantiated.

Two forms of injections are possible:

  • Injecting one fullName on another fullName
  • Injecting one fullName on a type

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
let registry = new Registry();
let container = registry.container();

registry.register('source:main', Source);
registry.register('model:user', User);
registry.register('model:post', Post);

// injecting one fullName on another fullName
// eg. each user model gets a post model
registry.injection('model:user', 'post', 'model:post');

// injecting one fullName on another type
registry.injection('model', 'source', 'source:main');

let user = container.lookup('model:user');
let post = container.lookup('model:post');

user.source instanceof Source; //=> true
post.source instanceof Source; //=> true

user.post instanceof Post; //=> true

// and both models share the same source
user.source === post.source; //=> true

Parameters:

factoryName String
property String
injectionName String

knownForType

(type) private

Parameters:

type String
the type to iterate over

makeToString

(factory, fullName) Function private

Parameters:

factory Any
fullName String

Returns:

Function
toString function

normalize

(fullName) String private

Normalize a fullName based on the application's conventions

Parameters:

fullName String

Returns:

String
normalized fullName

normalizeFullName

(fullName) String private

A hook to enable custom fullName normalization behaviour

Parameters:

fullName String

Returns:

String
normalized fullName

options

(fullName, options) private

Parameters:

fullName String
options Object

optionsForType

(type, options) private

Allow registering options for all factories of a type.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
let registry = new Registry();
let container = registry.container();

// if all of type `connection` must not be singletons
registry.optionsForType('connection', { singleton: false });

registry.register('connection:twitter', TwitterConnection);
registry.register('connection:facebook', FacebookConnection);

let twitter = container.lookup('connection:twitter');
let twitter2 = container.lookup('connection:twitter');

twitter === twitter2; // => false

let facebook = container.lookup('connection:facebook');
let facebook2 = container.lookup('connection:facebook');

facebook === facebook2; // => false

Parameters:

type String
options Object

register

(fullName, factory, options) private

Registers a factory for later injection.

Example:

1
2
3
4
5
let registry = new Registry();

registry.register('model:user', Person, {singleton: false });
registry.register('fruit:favorite', Orange);
registry.register('communication:main', Email, {singleton: false});

Parameters:

fullName String
factory Function
options Object

resolve

(fullName, options) Function private

Given a fullName return the corresponding factory.

By default resolve will retrieve the factory from the registry.

1
2
3
4
let registry = new Registry();
registry.register('api:twitter', Twitter);

registry.resolve('api:twitter') // => Twitter

Optionally the registry can be provided with a custom resolver. If provided, resolve will first provide the custom resolver the opportunity to resolve the fullName, otherwise it will fallback to the registry.

1
2
3
4
5
6
7
let registry = new Registry();
registry.resolver = function(fullName) {
   // lookup via the module system of choice
 };

// the twitter factory is added to the module system
registry.resolve('api:twitter') // => Twitter

Parameters:

fullName String
options [Object]
source [String]
the fullname of the request source (used for local lookups)

Returns:

Function
fullName's factory

typeInjection

(type, property, fullName) private

Used only via injection.

Provides a specialized form of injection, specifically enabling all objects of one type to be injected with a reference to another object.

For example, provided each object of type controller needed a router. one would do the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
let registry = new Registry();
let container = registry.container();

registry.register('router:main', Router);
registry.register('controller:user', UserController);
registry.register('controller:post', PostController);

registry.typeInjection('controller', 'router', 'router:main');

let user = container.lookup('controller:user');
let post = container.lookup('controller:post');

user.router instanceof Router; //=> true
post.router instanceof Router; //=> true

// both controllers share the same router
user.router === post.router; //=> true

Parameters:

type String
property String
fullName String

unregister

(fullName) private

Unregister a fullName

1
2
3
4
5
6
7
let registry = new Registry();
registry.register('model:user', User);

registry.resolve('model:user').create() instanceof User //=> true

registry.unregister('model:user')
registry.resolve('model:user') === undefined //=> true

Parameters:

fullName String
Show:

_factoryInjections

InheritingDict private

_factoryTypeInjections

InheritingDict private

_injections

InheritingDict private

_normalizeCache

InheritingDict private

_options

InheritingDict private

_resolveCache

InheritingDict private

_typeInjections

InheritingDict private

_typeOptions

InheritingDict private

fallback

Registry private

A backup registry for resolving registrations when no matches can be found.

registrations

InheritingDict private

resolver

Resolver private

An object that has a resolve method that resolves a name.