The primary object for interacting with the container.
function Ankh(kernel) {
Object.defineProperty(this,'kernel',{
value: (kernel || new Kernel())
,configurable: false
,writable: false
,enumerable: true
})
}
Option name | Type | Description |
---|---|---|
key | String | The service key; will overwrite existing keys |
impl | Any | The implementation to resolve |
[cfg] | Object | The configuration for the {ComponentModel} |
return | Ankh | this instance |
Registers a factory on the kernel. By default, this will be registered as a transient
instance. Use the cfg.lifestyle
configuration to
declare otherwise; eg { lifestyle: 'singleton'}
Ankh.prototype.factory = function(key, impl, cfg) {
cfg = (cfg || {})
var model = new ComponentModel(key, impl, cfg)
model.activator(cfg.activator || '@factory')
model.resolver(cfg.resolver || (cfg.lifestyle === 'singleton' ? '@cacheable' : '@transient'))
this.kernel.register(model)
return this
}
Option name | Type | Description |
---|---|---|
key | String | The service key; will overwrite existing keys |
impl | Any | The implementation to resolve |
[cfg] | Object | The configuration for the {ComponentModel} |
return | Ankh | this instance |
Registers a constructor/prototype on the kernel. By default, this will be registered as a transient
instance. Use the cfg.lifestyle
configuration to
declare otherwise; eg { lifestyle: 'singleton'}
Ankh.prototype.ctor = function(key, impl, cfg) {
cfg = (cfg || {})
var model = new ComponentModel(key, impl, cfg)
;
model.activator(cfg.activator || '@constructor')
model.resolver(cfg.resolver || (cfg.lifestyle === 'singleton' ? '@cacheable' : '@transient'))
this.kernel.register(model)
return this
}
Option name | Type | Description |
---|---|---|
key | String | The service key; will overwrite existing keys |
impl | Any | The implementation to resolve |
[cfg] | Object | The configuration for the {ComponentModel} |
return | Ankh | this instance |
Registers a value on the kernel. This will deep clone the passed in impl
and resolve that value each time. Lifestyle is irrelevant here.
Ankh.prototype.value = function(key, impl,cfg) {
cfg = (cfg || {})
var model = new ComponentModel(key, clone(impl), cfg)
model.activator(cfg.activator || '@impl')
model.resolver(cfg.resolver || '@impl')
this.kernel.register(model)
return this
}
Option name | Type | Description |
---|---|---|
key | String | The service key; will overwrite existing keys |
impl | Any | The implementation to resolve |
[cfg] | Object | The configuration for the {ComponentModel} |
return | Ankh | this instance |
Registers an instance (reference) on the kernel. This will simply resolve the impl
and resolve that aach time. Lifestyle is irrelevant here.
This is handy for putting third-party libs into the container.
Ankh.prototype.instance = function(key, impl, cfg) {
cfg = (cfg || {})
var model = new ComponentModel(key, impl, cfg)
model.activator(cfg.activator || '@impl')
model.resolver(cfg.resolver || '@impl')
this.kernel.register(model)
return this
}
Option name | Type | Description |
---|---|---|
targetKey | String | the service to decorate |
decoratorKey | String | the service to use for decorating |
return | Ankh | this instance |
Sets up a service to decorate another service's instance.
So given service 'A', you can declare service 'B' to decorate all instances of the resolved 'A' services.
To receive the decorated instance into your decorator, you must use the special key '@impl' in your inject
array.
Ankh.prototype.decorate = function(targetKey, decoratorKey) {
this.kernel.decorate(targetKey, decoratorKey)
return this
}
Option name | Type | Description |
---|---|---|
key | String | the service to expose as deferrable |
[deferrableKey] | String | the key to use for resolve calls. Default is {key}Deferred |
return | Ankh | this instance |
Registers a service to expose {serviceKey}Deferred
as an
on-demand resolution.
Ankh.prototype.deferrable = function(key, deferrableKey,cfg) {
var self = this
deferrableKey = (deferrableKey || (key + 'Deferred'))
var impl = this.resolve.bind(this,key)
var model = new ComponentModel(deferrableKey,impl,cfg)
model.activator('@impl')
model.resolver('@impl')
this.kernel.register(model)
return this
}
Option name | Type | Description |
---|---|---|
key | String | The service key |
deps | Object | Key/value map of dynamic values to use during resolution. These keys map to the |
return | Promise | resolving the requested service |
Resolves a service by key
while injecting dynamically the dependencies found in deps
Ankh.prototype.resolve = function(key,deps) {
return this.kernel.resolve(key,deps)
}
Starts the container, validating the registrations and executing startable
components
Ankh.prototype.start = function(){
return this.kernel.start()
.then(function(){
return this
}.bind(this))
}
Ankh.prototype.isRegistered = function(key) {
return this.kernel.isRegistered(key)
}
Validates registrations; eg for acyclic dependencies
Ankh.prototype.validate = function(){
return this.kernel.validate()
}
Factory method for creating an container.
Ankh.create = function() {
return new Ankh()
}