Container

April 11, 2019 ยท View on GitHub

The container service allows you to manage dependency injection across the application, have easy access to object or services and lazy-load, singleton or cache the creation of new service instances.

API

set()

Register a new service

ParamTypeDescription
namestringName of the service
objectobject / callableService object or a resolvable callback function. For dependency injection pass a closure with your desired services as argument names.
singletonboolShould object instance be singleton?
cacheboolShould service instance be cached in memory?

Return Value

Returns container object instance

Example

container.set('myService', {
    'hello': 'world',
    'foo': 'bar'
}, true, false);

get()

Get registered service. Return service instance if already created (singleton) or initialize it if no instance is yet available.

ParamTypeDescription
namestringName of the service

Return Value

Requested service object instance or null if no object was found.

Example

let service = container.get('myService');

resolve()

This is where the dependency 'magic' happens. This method receives a callable variable, analyze argument names, makes registered service matching those name available inside the callable function and executes it

ParamTypeDescription
namecallableFunction needed to be resolved with requested services

Return Value

Callback resolved value.

Example

let result = container.resolve(function(window, myService) {
    window.alert(myService['hello']); // Alert: 'world'
});

path()

Get value by object path, or set value in object path if value param is passed

ParamTypeDescription
pathstringDot separated nested object path
valuemixedValue to be set in given path (optional)

Return Value

Value of the requested path or null if no path was found.

Example


// Get Path Value
let result = container.path('myService.foo'); // = 'bar'

// Set Path Value
container.path('myService.foo', 'new value');

let result = container.path('myService.foo'); // = 'new value'