Registry
Registry Class
PRIVATE
Defined in: packages/container/lib/registry.js:7
Module: ember
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.
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.
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:
-
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:
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) 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:
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) Boolean
private
Given a fullName check if the container is aware of its factory or singleton instance.
Parameters:
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:
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
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.
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:
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) Function
private
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:
-
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:
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) private
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
_factoryInjectionsInheritingDict
private
_factoryTypeInjectionsInheritingDict
private
_injectionsInheritingDict
private
_normalizeCacheInheritingDict
private
_optionsInheritingDict
private
_resolveCacheInheritingDict
private
_typeInjectionsInheritingDict
private
_typeOptionsInheritingDict
private
fallbackRegistry
private
A backup registry for resolving registrations when no matches can be found.
registrationsInheritingDict
private
resolverResolver
private
An object that has a resolve
method that resolves a name.
© 2017 Yehuda Katz, Tom Dale and Ember.js contributors
Licensed under the MIT License.
https://emberjs.com/api/classes/Registry.html