JAVASCRIPT

di4js

About

The di4js module is dependency injection implementation in JavaScript. Dependency injection is a software design pattern that allows the removal of hard-coded dependencies and makes it possible to change them. di4js is free software distributed under the terms of the MIT License (MIT) and can be used with web browsers or with node.js.


var Car = function (engine, year) {
  this.engine = engine;
  this.year = year;
};

Car.prototype.start = function () {
  this.engine.start();
};

var DieselEngine = function () {
  this.hp = 0;
};

DieselEngine.prototype.start = function () {
  console.log("Diesel engine with " + this.hp + " hp has been started...");
};

di
  .autowired(false)
  .register('dieselEngine')
    .as(DieselEngine)
    .withProperties()
      .prop('hp').val(42);
  .register('car')
    .as(Car)
    .withConstructor()
      .param().ref('dieselEngine')
      .param().val(1976);

var car = di.resolve('car');

car.start(); // Diesel engine with 42 hp has been started...
    

Platforms

di4js runs on all the following environments:

Web browsers

  • Chrome
  • Firefox
  • Internt Explorer (IE9+)

Server-side

  • Node.js

Installation

Node.js

To install di4js module for Node.js, this command should be used:

npm install di4js

Also di4js module's loading statement should be added to main module:


'use strict';
var di = require('di4js');
// Your code goes here...

Web Browsers

Theare are few options to install di4js to the web browser. Module can be downloaded or can be installed using Bower or NuGet package manager.


<!DOCTYPE HTML>
<html>
<body>
  <!-- Your code goes here...-->
  <script src="./scripts/di4js.min.js"></script>
</body>
</html>

Bower

To install di4js, run the following command.

bower install di4js

NuGet

In Visual Studio di4js module can be installed using NuGet extension. To install di4js, run the following command in the package manager console.

Install-Package di4js


License

This software is distributed under the terms of the MIT License (MIT).


What is Sourcery?

All of these components are Open Source.