0.2.0 • Published 8 years ago

js-di v0.2.0

Weekly downloads
1
License
ISC
Repository
-
Last release
8 years ago

js-di

A simple Dependency-Injection system which supports multiple containers and container inheritance

getting the code

install from npm: npm install js-di

use in your project: var injector = require('js-di');

running the tests

Install the dependencies using npm install

Run the tests using npm test

Transpile to ES5 using npm run build

Build the docs using npm run doc

Generate the docs for the readme with npm run build-readme

API Reference

Injector

injector.register(modules, container) ⇒ Container

Registers the modules in the specified container. If no container is specified, uses the root container.

Kind: instance method of injector
Returns: Container - the container

ParamTypeDescription
modulesObjecta key-value pair of modules to register
containerContaineroptional, a container instance in which the modules should be registered.

injector.invoke(module, container) ⇒ *

Invoke a function through the DI system. Dependencies will automatically be applied as arguments to the function. Will attempt to resolve dependencies from the specified container (if provided) and will recursively look through parent containers if the dependencies cannot be met

Kind: instance method of injector
Returns: * - The return value of the invoked function

ParamTypeDescription
modulefunctiona function to invoke.
containerContaineroptional, a container instance from which the dependencies should be injected

injector.getInstance(key, container) ⇒ *

Manually get an registered instance from the specified container (if provided) or from the root container.

Kind: instance method of injector
Returns: * - the registered module

ParamTypeDescription
keyStringthe key of the registered module to get
containerContaineroptional, a container instance in which to look. If unspecified, will use the root container.

injector.getContainer(id) ⇒ Container

if an id is passed, returns the matching container, or null if no id is passed, returns the root container

Kind: instance method of injector
Returns: Container - the specified container

ParamTypeDescription
idStringoptional the id of the container to get

injector.createContainer(id, parent) ⇒ Container

Creates a new Container with the specified ID. If parent is a string, it will use the container corresponding to that id. If the id does not match any containers, it will throw an exception. If parent is not a string, it checks to make sure the specified argument has an id property and that a container with this id already exists. Otherwise, it will throw an exception. If parent is unspecified, the root container will be set as the parent

Kind: instance method of injector
Returns: Container - the new container

ParamTypeDescription
idStringthe id of the container to create
parentContainer | Stringthe container to set as the new container's parent, or an id corresponding to the container to set as the parent

Container

Kind: global class

container.register(modules) ⇒ Container

Registers one or more modules within the current container. Modules can be specified in any order. If a module being registered in a child container requires a module that does not exist in the current container, it will recursively look through parent containers until it finds it UNLESS a new instance of that dependency is being registered as part of this call to .register()

Circular dependencies are caught and an exception is thrown.

Kind: instance method of Container
Returns: Container - the container (for chaining)

ParamTypeDescription
modulesObjecta map of modules to register, where the key is the id with which to register the module, and the value is the invocable function which will return the instance to store

container.get(key)

Get a module from this container with the specified key. If it does not exist in the container, recursively search through parent containers. If it has not been registered in any ancestor, return null

Kind: instance method of Container

ParamTypeDescription
keyStringthe key corresponding to the dependency to get

container.invoke(module) ⇒ *

Invoke a function through the DI system. Dependencies will automatically be applied as arguments to the function. Will attempt to resolve dependencies from the current container (if provided) and recursively look through parent containers if the dependencies cannot be met

Kind: instance method of Container
Returns: * - The return value of the invoked function

ParamTypeDescription
modulefunctiona function to invoke.

Container.id

the id of the container

Kind: static property of Container
Properties

NameDescription
idString