infector v1.0.7
Infector
Dependency injection for Node.js and the browser. Inspired by the dependency injection approaches in AngularJS and node-di. No relation to Infector++.
Usage
Grab Infector
In the browser, Lo-Dash is a dependency and Infector is exposed as a global. So do something like:
<script src="lodash.js"></script>
<script src="infector.js"></script>Using Bower:
$ bower install infector --saveIn Node.js:
$ npm install infector --saveSpecify dependencies
Use a constructor property:
function Foo(bar) {}
Foo.infect = ['bar'];
// or Foo.inject = ['bar'];Register modules
There are two return options.
type: an instance oftypewill be returned vianew:
function Foo() {}
infector.register({ foo: { type: Foo } });If you want the module to be returned as a singleton:
infector.register({ foo: { type: Foo, isSingleton: true } });value: the registeredvaluewill be returned:
infector.register({ foo: { value: 'Foo' } });Get modules
Just do:
infector.get('foo');Examples
Browser
A working example is available in the examples/browser dir. To view:
$ open examples/browser/index.htmlNode.js
A working example is available in the examples/node dir. To run:
$ node examples/nodeTesting
In the browser:
$ npm run test-browserIn Node.js:
$ npm run test-specTo generate a coverage report:
$ npm run test-covCredits
- This great article by Chad Perry: DIY-DI
- @vojtajina, whose injector recipe in node-di gave me ideas for module registration
- AngularJS, where the idea of how to specify dependencies to be injected came from
- Infector++, where the name for this component came from ;)
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
