Git Product home page Git Product logo

objar's Introduction

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

objar's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

gwash3189

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.