https://github.com/endel/injectd
Lean dependency injection for JavaScript (ES2016) and TypeScript
https://github.com/endel/injectd
Last synced: about 1 year ago
JSON representation
Lean dependency injection for JavaScript (ES2016) and TypeScript
- Host: GitHub
- URL: https://github.com/endel/injectd
- Owner: endel
- License: mit
- Created: 2016-06-25T17:00:37.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2016-07-02T12:36:38.000Z (almost 10 years ago)
- Last Synced: 2025-03-12T15:06:39.405Z (over 1 year ago)
- Language: JavaScript
- Size: 20.5 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
injectd 0.1 
===
This library is the leanest dependency injection implementation you can
possibly have for JavaScript (ES2016) and TypeScript. Just **~1kb filesize
(uncompressed)**
The API consists in three methods: `inject`, `register` and `resolve`.
- `register(idOrInstance: string | any, instance?: any): void`: Register instance with given identifier. The instance registered will be available for `inject` and `resolve` methods.
- `inject(id: any)`: A decorator to inject a previously registered instance into target class property. ([see usage below](#usage))
- `resolve(id: any): T`: Get previously registered instance by given name or type. (used under the hood by `inject` method)
Configuration
---
Your compiler/transpiler need to understand the [decorator
syntax](https://github.com/wycats/javascript-decorators/blob/master/README.md)
in order to be able to use `injectd`. Here's how you enable it using:
- [Babel](https://babeljs.io/docs/plugins/syntax-decorators/)
- [TypeScript](https://www.typescriptlang.org/docs/handbook/decorators.html)
Usage
---
```typescript
// application.ts
import { register } from "injectd"
export class Application {
constructor () {
register(this);
}
}
// screen.ts
import { Application } from "./application"
import { inject } from "injectd"
export class Screen {
@inject(Application)
app: Application;
}
// main.ts
let myApp = new Application();
// anywhere.ts
import { Screen } from "./screen"
let screen = new Screen();
screen.app // your "Application" instance is here!
```
License
---
MIT