Dead simple dependency container for node.js
npm install independableIndependable
============
Dead simple dependency container for node.js and the browser.
Function.toString()` to identify the dependencies.
Things will go wrong when you try to minify such files, and it simply isn't a "clean" solution.
Installation
`
npm install independable
`
or
`
bower install independable
`
Usage
You can retreive the container as
`javascript
// Node & commonjs
var independable = require('independable');
var container = independable();
// AMD
define(['independable'], function(independable) {
var container = independable();
});
// Global
var independable = window.independable;
var container = independable();
`
depending on your environment.
Subsequently there are two ways to specify your dependencies. You could either _register_ them, or _define_ them.
$3
The `container.register()` method expects you to specify a dependency directly. For example
`javascript
// Registering a string dependency
container.register('some-string', 'a string value');
// Registering an object as dependency
var object = {};
container.register('some-object', object);
`
or equivalent
`javascript
container.register({
"some-string": "a string value",
"some-object": {}
});
`
to register multiple dependencies at once.
$3
If you want to only create objects on-the-fly as they are needed, you may provide a factory function using the `container.define()`method.
For example
`javascript
var Constructor = function() {
this.foo = 'bar';
};
container.define('on-the-fly', function() {
return new Constructor();
});
`
As such, `new Constructor()` is only called when requested by `container.get('on-the-fly')`.
Note that if you use `container.get('on-the-fly');` the constructor is not called again, but the object that was created earlier is returned.
Also note that
`javascript
container.register('dep', 'value');
`
and
`javascript
container.define('dep', function() {
return 'value';
});
`
are equivalent.
#### Relying on other dependencies
If a certain dependency depends on another dependency, there are two ways to handle this. Either
`javascript
container.register('one', 1);
container.register('two', 2);
container.define('three', {
deps: ['one', 'two'],
get: function(one, two) {
return one + two;
}
});
`
or
`javascript
container.register('one', 1);
container.register('two', 2);
container.define('three', function() {
var one = this.get('one'),
two = this.get('two');
return one + two;
});
`
are equivalent. As can be seen, `this` in the factory function refers to the dependency container itself.
As such, you can also pass it to functions depending on the container as
`javascript
var needsTheContainer = function(container) {
// Do something with the container
};
container.define('dependent', function() {
return needsTheContainer(this);
});
`
$3
`javascript
var dep = container.get('dependency-id');
`
Since version 1.1.0 you can also directly access the dependencies which makes them suitable for object destructuring as well
`javascript
var dep = container['dependency-id'];
var { dep } = container;
`
$3
Deleting dependencies can be useful to clear up some memory:
`javascript
container.delete('dependency-id');
``