Ankh

Ankh

function
Ankh()

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
    })
}

factory

method
Ankh.prototype.factory()

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
}

ctor

method
Ankh.prototype.ctor()

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
}

value

method
Ankh.prototype.value()

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
}

instance

method
Ankh.prototype.instance()

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
}

decorate

method
Ankh.prototype.decorate()

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
}

deferrable

method
Ankh.prototype.deferrable()

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
}

resolve

method
Ankh.prototype.resolve()

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 inject values found on the service implementation (impl). Be very careful here since if your service is a singleton and you pass in
dynamic values, those values will persist for all future resolutions of the dependency.

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)
}

start

method
Ankh.prototype.start()

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)
}

validate

method
Ankh.prototype.validate()

Validates registrations; eg for acyclic dependencies

Ankh.prototype.validate = function(){
    return this.kernel.validate()
}

create

method
Ankh.create()

Factory method for creating an container.

Ankh.create = function() {
    return new Ankh()
}