Subscribe to our list to receive the latest updates on Node on Fire

Class: Injector

Injector

new Injector()

The injector module is used for AngularJS-style dependency injection.

Modules can either register dependencies via the Injector#register method or execute constructors which need dependencies injected via Injector#execute.

For example, to register a dependency called MyService:

function MyService() {
}

var service = new MyService();

injector.register('MyService', function() {
    return service;
});

and to inject this in a constructor:

function MyConstructor(MyService) {
    // MyService is the MyService instance previously registered.
}

var instance = injector.execute(MyConstructor);

A self dependency is available which replaces the var self = this; line. For example:

function MyTest(self) {
    // self === this
}

injector.execute(MyTest);

Methods

call(constructor, privateMap)

This invokes the constructor and returns it's return value. The constructor may be any type, and if a function is given, it's executed. If any other type is given, it's returned instead.

Parameters:
Name Type Description
constructor Constructor

The constructor to execute.

privateMap Dictionary

The private dependencies.

Returns:

The return value of the constructor.

Type
Any

construct(constructor, privateMap)

Executes a given constructor and returns the new instance. This injects dependencies based on the argument names of the constructor.

For example, this injects the MyService dependency when initiating MyConstructor: *

function MyConstructor(MyService) {
    // MyService is the MyService instance previously registered.
}

var instance = injector.execute(MyConstructor);

Private dependencies can be injected which need to be passed in via privateMap.

Parameters:
Name Type Description
constructor Constructor

The constructor to invoke.

privateMap Dictionary

The private dependency map.

Returns:

An instance created from constructor. Similar to calling new constructor().

Type
Instance

execute()

exists(name)

Checks whether a dependency exists within the injector.

Parameters:
Name Type Description
name String

The name of the dependency.

Returns:
Type
Boolean

prepare(constructor)

Prepares a constructor for dependency injection. This returns a constructor which takes privateMap as argument.

function Test(MyService) {
    //
}

var TestConstructor = injector.prepare(TestConstructor);
var instance = new TestConstructor({});
Parameters:
Name Type Description
constructor Constructor

The constructor to prepare for dependency injection.

Returns:

A new constructor prepared for dependency injection.

Type
Constructor

register(name, callback)

Registers a dependecy with a given name and the callback which should return the dependency.

For example, to register a dependency called MyService:

function MyService() {
}

var service = new MyService();

injector.register('MyService', function() {
    return service;
});

To execute a constructor and inject dependencies see Injector#execute.

Parameters:
Name Type Description
name String

The name of the dependency to register.

callback function

The callback to invoke when the dependency needs to resolve. The callback gets invoked with one argument which is the instance being created in Injector#execute.

unregister(name)

Removes an already registered dependency.

Parameters:
Name Type Description
name String

The name of the dependency to unregister.

Do you have any questions or comments on this page? Start a discussion below.

comments powered by Disqus
Subscribe to our list to receive the latest updates on Node on Fire