Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dot-build/handbag
Dependency Injector - based on AngularJS (without the config stage)
https://github.com/dot-build/handbag
Last synced: about 2 months ago
JSON representation
Dependency Injector - based on AngularJS (without the config stage)
- Host: GitHub
- URL: https://github.com/dot-build/handbag
- Owner: dot-build
- Created: 2014-11-04T12:45:09.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2016-04-07T15:37:18.000Z (almost 9 years ago)
- Last Synced: 2024-04-14T08:30:16.667Z (10 months ago)
- Language: JavaScript
- Homepage:
- Size: 44.9 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# handbag
A simple Dependency Injection container written in ES6.
Some ideas were taken from AngularJS' `$injector` service.
- Allow to nest injectors so you can separate containers by module, package, etc
- Accepts a `Symbol` as a provider or constant name.
- Allows only a provider (factory function or class) and constants
- Allows to register a "private" provider, i.e. a factory that will NOT be a singleton
- Allows to freeze the container (disables any further changes in the dependencies)## Injector API
- .constant(name, value)
- .provide(name, provider, isShared = true)
- .provideShared(name, provider);
- .provideNotShared(name, provider);
- .has(name)
- .get(name, locals = {})
- .freeze()## Static methods
- .createInjector()
## Usage
See the full API on [documentation page](https://doc.esdoc.org/github.com/dot-build/handbag/)
```js
// foo.service.js
const FOO = 123;
class FooService {
constructor(FOO) {
this.foo = FOO;
}
}export { FooService, FOO };
// foo.controller.js
export default class FooController {
constructor(FooService, data) {
this.service = FooService;
this.data = data
}
}// app.js
import handbag from 'handbag';
import FooController from 'foo.controller.js';
import {FooService, FOO} from 'foo.service.js';const di = handbag.createInjector();
di.constant('FOO', FOO);
di.provide('FooService', ['FOO', FooService]);
di.provide('FooController', FooController);// instantiates FooController injecting the service on constructor
const data = { bar: 1 };
const ctrl = di.get('FooController', { data: data });```