Simply Container for using Dependency Injection pattern in JavaScript
Container.js is lightweight library (<2kb when minified), designed to facilitate how you can implement Dependency Injection pattern in your JavaScript applications. It works both versions of ECMAScript - 2015 (ES6) and 5 (ES5).
In order to use this package, you need to install it in your project:
via Bower (by default will be used ES5 version)
bowerinstallContainer.js--savevia NPM (by default will be used ES2015 version)
npminstallnode-container.js--saveor download it manually:
<scriptsrc="/path/to/Container.min.js"></script><scriptsrc="/path/to/Es2015-Container.js"></script>- Removed callback from
Container.getmethod - Gulp instead of Grunt
- Support for ES2015
voidContainer - Constructor - arguments: [Object<string, function> elements]
Argument
elementsis optional. It allows to create default bindings for existing classes.
Example:
varappContainer=newContainer({"Date": Date});booleanContainer.prototype.has - arguments: [string name]
Return true if Container contains element with given name.
Example:
console.log(appContainer.has('Date'));// trueconsole.log(appContainer.has('MyObject'));// falsevoidContainer.prototype.bind - arguments: [string name, function instance, string[]|function[] parameters]
Binds given instance with given name. Each value in
parametersarray should be name of element in Container or function which return value of parameter.
Example:
appContainer.bind('Window',Window,['Date']);appContainer.bind('Door',Door,[function(){returnMath.random();}]);voidContainer.prototype.singleton - arguments: [string name, function instance, string[]|function[] parameters]
Binds given object as singleton in container. Parameters are the same like
Container.bindmethod.
Example:
appContainer.singleton('Date',newDate());appContainer.get('Date').setFullYear(2014);appContainer.get('Date').getFullYear();// 2014mixedContainer.prototype.get - arguments: [string name]
Returns instance of earlier bound instance.
Example:
appContainer.get('Window');// an Window instancevoidContainer.prototype.remove - arguments: [string name]
Removes link between Container and given name.
Example:
appContainer.remove('Date');appContainer.has('Date');// falseFirstly need to install dev dependencies:
npminstallFor running unit tests:
npmrungulptestFor build new dists version:
npmrungulp