Registry Class
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.
Item Index
Methods
Methods
container
-
options
Creates a container based on this registry.
Parameters:
-
options
Object
Returns:
created container
describe
-
fullName
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:
described fullName
expandLocalLookup
-
fullName
-
[options]
Given a fullName and a source fullName returns the fully resolved fullName. Used to allow for local lookup.
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:
Returns:
fullName
factoryInjection
-
factoryName
-
property
-
injectionName
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:
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
factoryTypeInjection
-
type
-
property
-
fullName
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:
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
has
-
fullName
-
[options]
Given a fullName check if the container is aware of its factory or singleton instance.
Parameters:
Returns:
injection
-
factoryName
-
property
-
injectionName
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:
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
makeToString
-
factory
-
fullName
Parameters:
-
factory
Any -
fullName
String
Returns:
toString function
normalize
-
fullName
Normalize a fullName based on the application's conventions
Parameters:
-
fullName
String
Returns:
normalized fullName
normalizeFullName
-
fullName
A hook to enable custom fullName normalization behaviour
Parameters:
-
fullName
String
Returns:
normalized fullName
optionsForType
-
type
-
options
Allow registering options for all factories of a type.
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
Registers a factory for later injection.
Example:
let registry = new Registry();
registry.register('model:user', Person, {singleton: false });
registry.register('fruit:favorite', Orange);
registry.register('communication:main', Email, {singleton: false});
resolve
-
fullName
-
[options]
Given a fullName return the corresponding factory.
By default resolve
will retrieve the factory from
the registry.
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.
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:
Returns:
fullName's factory
typeInjection
-
type
-
property
-
fullName
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:
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
unregister
-
fullName
Unregister a fullName
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
Properties
_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.