Skip to content
This repository was archived by the owner on Mar 20, 2022. It is now read-only.
/container.jsPublic archive

Simply Container for using Dependency Injection pattern in JavaScript

Notifications You must be signed in to change notification settings

pomek/container.js

Repository files navigation

Container.jsCode ClimateBuild Status

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

How to use

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--save

via NPM (by default will be used ES2015 version)

npminstallnode-container.js--save

or download it manually:

<scriptsrc="/path/to/Container.min.js"></script><scriptsrc="/path/to/Es2015-Container.js"></script>

Changes

2.0.0

  • Removed callback from Container.get method
  • Gulp instead of Grunt
  • Support for ES2015

API Reference

voidContainer - Constructor - arguments: [Object<string, function> elements]

Argument elements is 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'));// false

voidContainer.prototype.bind - arguments: [string name, function instance, string[]|function[] parameters]

Binds given instance with given name. Each value in parameters array 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.bind method.

Example:

appContainer.singleton('Date',newDate());appContainer.get('Date').setFullYear(2014);appContainer.get('Date').getFullYear();// 2014

mixedContainer.prototype.get - arguments: [string name]

Returns instance of earlier bound instance.

Example:

appContainer.get('Window');// an Window instance

voidContainer.prototype.remove - arguments: [string name]

Removes link between Container and given name.

Example:

appContainer.remove('Date');appContainer.has('Date');// false

Development

Firstly need to install dev dependencies:

npminstall

For running unit tests:

npmrungulptest

For build new dists version:

npmrungulp

About

Simply Container for using Dependency Injection pattern in JavaScript

Resources

Stars

Watchers

Forks

Packages

No packages published