A lightweight DI and IOC container to register service into service center
A lightweight DI and IOC container to register service into service center
inversion.js can be used in a browser or in a nodejs app. It can be installed via bower or npm:
> $ bower install inversionjs
> $ npm install inversionjs
// Module A
function Foo(name, age) {
this.name = name;
this.age = age;
}
Foo.prototype.say = function () {
console.log('My name:' + this.name + ', age: ' + this.age);
}
// Registry
var registry = new Registry();
// Register A via deps
registry.register({
name: 'moduleDeps',
module: Foo,
deps: ['zemin', 'infinity']
});
// Register B via factory
registry.register({
name: 'moduleFactory',
module: Foo,
deps: { name: 'zemin', age: 'infinity' }
factory: function (registry, Module, deps) {
return new Module(deps.name, deps.age);
}
});
registry.get('moduleDeps').say()
registry.get('moduleFactory').say()
// ==> `My name: zemin, age: infinity`
Test once
> $ npm run test
Or watch files changes
> $ npm run test:watch
Build umd
> $ npm run build:umd
Build umd min
> $ npm run build:umd:min
Build all
> $ npm run build
All built files will in /dist/*