Simple dependency injection container
JavaScript
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.editorconfig
.gitignore
.jshintrc
.travis.yml
LICENSE
README.md
bower.json
gulpfile.js
objar.js
objar.min.js
objarSpec.js
package.json

README.md

objar Build status

Simple and fast dependency injection container

Installation

Node

$ npm install objar --save
var objar = require('objar');
var jar = new objar();

Browser

$ bower install objar --save
<script type="text/javascript" src="bower_components/objar/objar.min.js"></script>
// Global
var jar = new objar();

// RequireJS
require(['objar'], function (objar) {
    var jar = new objar();
});

Usage

Defining and Resolving services

function Logger() {
    this.log = function (message) {
        console.log('Logged: ' + message);
    };
}

function ProductController(logger) {
    this.logger = logger;

    this.saveProduct = function () {
        this.logger.log('Saved the product!');
    };
}

// Defines the 'logger' service
jar.define('logger', function () {
   return new Logger();
});

// Defines the 'controller' service depending on the 'logger' service
jar.define('controller', function () {
    return new ProductController(jar.resolve('logger'));
});

// Resolving the product controller service
var controller = jar.resolve('controller');

// => Logged: Saved the product!
controller.saveProduct();

The define() method accepts two parameters:

  • the service name
  • a function returning the service (it can be any value except undefined)

Services are created lazily when calling the resolve() method and it always returns the same instance (singleton)

Circular dependencies

When two services depend on each other (in)directly, an Error will be thrown showing the cycle path in order to help you to fix it.

// 'hello' depends on 'world'
jar.define('hello', function () {
    return 'hello' + jar.resolve('world');
});

// 'world' depends on 'hello'
jar.define('world', function () {
    return 'world' + jar.resolve('hello');
});

// Error: Circular dependency detected: hello -> world -> hello
jar.resolve('hello');

Invoking functions

You can invoke annotated functions depending on services (AngularJS way).

jar
    .define('hello', function () {
        return 'hello';
    })
    .define('world', function () {
        return 'world';
    })
;

// Invoke the function with the services injected:
// - w: will be the 'hello' service
// - h: will be the 'world' service
jar.invoke(['hello', 'world', function (h, w) {
    // => hello world
    console.log(h + ' ' + w);
}]);

License

MIT