Home Reference Source Repository

handbag

A simple Dependency Injection container written in ES6.

Some ideas were taken from AngularJS' $injector service.

Injector API

Static methods

Usage

See the full API on documentation page


// 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 });