IoC for Javascript & Node.js

Noder


new Noder()


Noder constructor.

The Noder instance is created when the first usage of require('noder.io'), then the same object (reference) is returned by require('noder.io') in the next usages.

Example
// file: a.js
var noder = require('noder.io');

// true
console.log(typeof noder === 'object');

// true
console.log(noder instanceof noder.Noder);

noder.$di.set('hello', '-> Hello from a.js file.');

// file: b.js
var noder = require('noder.io');

// -> Hello from a.js file.
console.log(noder.$di.get('hello'));

See also

Classes

new Noder()


Noder constructor. For creating a new instance of Noder.

Example
var api = new noder.Noder();

new Collection(values)


Collection constructor. For creating a new instance of Collection.

Parameters
Name Type Argument Description
values
object
optional 

(Optional) values to add in the collection.

var collection = new noder.Collection({'foo': 'bar'});

// display "bar"
console.log(collection.get('foo'));
Example
var collection = new noder.Collection();

collection.set('keyName', 'any value');

// any value
console.log(collection.get('keyName'));

See also

Properties

$di :Collection

IOC container.

See Collection API doc.


See also
Example
noser.$di.set('name', Nico);

var hello = noder.$di.apply(function() {
  return 'Hello ' + this.name;
});

Methods

createCollection(values)


Create a collection container.

Parameters
Name Type Argument Description
values
object
optional 

Optional values to add in the new collection.

var collection = noder.createCollection({'foo': 'bar'});

// display "bar"
console.log(collection.get('foo'));
Returns
Type Description
Collection

The new Collection instance

Example
var collection = noder.createCollection();

// true
console.log(collection instanceof noder.Collection);

collection.set('keyName', 'any value');

// any value
console.log(collection.get('keyName'));

See also

createNoder()


Create a new Noder instance.

Returns
Type Description
Noder

The new Noder instance

Example
var api = noder.createNoder();

// true
console.log(api instanceof noder.Noder);

use(noderPlugin, arguments)


Use a noder plugin. A plugin is initialized by the method __noder().

See also plugins in the guide.

Parameters
Name Type Argument Description
noderPlugin
string
object
function

A noder plugin. If noderPlugin is:

  • string: the plugin is loaded with require and called by passing the current instance of noder like

    require(noderPlugin).__noder(noder [, optional argument, ...]);
  • object or function: the method __noder() is called by passing the current instance of noder like

    noderPlugin.__noder(noder [, optional argument, ...]);
arguments
mixed
optional  repeatable 

Zero, one or several arguments passed to plugin.

Throws
Type Description
TypeError

If the plugin has not the method __noder().

Returns
Type Description
Noder

The current Noder instance.

Example
// example-plugin.js *
module.exports.__noder = examplePlugin(noder, arg1, arg2) {

  noder.$di.set('foo', arg1 + ' and ' + arg2);

  return noder;
};

// app.js
var noder         = require('noder.io').createNoder();
var examplePlugin = require('./example-plugin');

noder.use(examplePlugin, 'any value 1', 'any value 2');

// displays: 'any value 1 and any value 2'
console.log(noder.$di.get('foo'));

$invoke(deps, fn)


Shortcut of noder.$di.invoke().

Call a function with dependencies injection.

Unlike noder.$invoke(), the returned value of each dependency is passed to fn.

Parameters
Name Type Argument Description
deps
string
array
function

See Collection.invoke().

fn
function
optional 

See Collection.invoke().

Returns
Type Description
mixed

The result of fn.


See also

$inject(deps, fn)


Shortcut of noder.$di.inject().

Call a function with dependencies injection.

Unlike noder.$invoke(), the raw value of each dependency is passed to fn.

Parameters
Name Type Argument Description
deps
string
array
function

See Collection.inject().

fn
function
optional 

See Collection.inject().

Returns
Type Description
mixed

The result of fn.


See also

$provider(key, deps, fn)


Shortcut of noder.$di.provider().

Create a provider that supports dependencies injection. When the item key is called, it calls the function fn by passing dependencies deps.

This method defines a function that returns the result of noder.$di.inject(deps, fn).

Parameters
Name Type Argument Description
key
string

The key (provider identifier).

deps
string
array
function

See Noder.$inject().

fn
function
optional 

See Noder.$inject().

Returns
Type Description
Noder

The current Noder instance.


See also

$factory(key, deps, fn)


Shortcut of noder.$di.factory().

Create a factory that supports dependencies injection. When the item key is called, it calls the function fn by passing dependencies deps.

This method defines a function that returns the result of noder.$di.invoke(deps, fn).

Parameters
Name Type Argument Description
key
string

The key (factory identifier).

deps
string
array
function

See Noder.$invoke().

fn
function
optional 

See Noder.$invoke().

Returns
Type Description
Noder

The current Noder instance.


See also

$singleton(key, fn)


Shortcut of noder.$di.singleton().

Create a singleton (function shared).

Parameters
Name Type Description
key
string

The key (function identifier).

fn
function

The function, executed once, after the value is returned when is again called.

Returns
Type Description
Noder

The current Noder instance.


See also

$apply(bindable, arguments)


Shortcut of noder.$di.apply().

Calls a given function by binding the scope (this) to the $di container (noder.$di._container).

Parameters
Name Type Argument Description
bindable
function
object

See Collection.apply().

arguments
mixed
optional  repeatable 

Zero, one or more arguments passed to bindable.

Returns
Type Description
mixed

The value returned by bindable.

Example
noder.$di.set('name', 'Nico');

// returns 'Nico'
noder.$apply(function() {
  return this.name;
});

See also

$wrap(value)


Shortcut of noder.$di.wrap().

Wrap a value. Useful to avoid calling a function in the implementation of a provider or a factory.

Parameters
Name Type Description
value
mixed

The value to wrap.

Returns
Type Description
function

value wrapped by a function.


See also

$require(property, required, obj)


Lazy require(), register a new lazy loadable property whose the value will be assigned on the fly with require() only when it is used.

The property configuration is :

  • enumerable: true,
  • configurable: false,
  • writable: false

Parameters
Name Type Argument Description
property
string
function

The property name or required value if the required argument is not provided (shortcut).

required
string
function
optional 

The module name or the JS file path required to set the property value. Or a custom loader handler via a given function, the scope (this) bind to noder.$di._container like noder.$apply().

Note:

  • The required argument is passed to the function require().
  • The required item is only loaded the first time (singleton).
obj
object
optional 

The object where the property is created. If is not provided, it's the current instance of Noder.

Returns
Type Description
Noder

The current Noder instance.

Examples
// Register `markdown` property
// Note: `marked` module is not loaded
noder.$require('markdown', 'marked');

// Now load the `marked` module in the `markdown` property
noder.markdown;
// Register a sub-property.
noder.models = {};

noder.$require('User', './models/user', noder.models);

// Load the `./models/user` module in `noder.models.User` property.
noder.models.User.someMethod();

$require.isLoaded(property, obj)


Check if a given module is loaded.

Parameters
Name Type Argument Description
property
string

The property name.

obj
object
optional 

Object to check the property. If is not provided, it's the current instance of Noder.

Returns
Type Description
bool

true if the given module is loaded, false otherwise.

Example
noder.$require('express');

// false
console.log(noder.$require.isLoaded('express'));

var express = noder.express;

// true
console.log(noder.$require.isLoaded('express'));

See also