jsdi

DI - Dependency Injection , Inversion of Control - IoC , for JS.

DI

DI - Dependency Injection , Inversion of Control - IoC , for JS.

Install for node.js

npm install jsdi

Install for component

component install brighthas/jsdi

Example:

var DI = require("./index");
var Emitter = require("events").EventEmitter;
 
function User(){
    this.name = "brighthas";
}
 
User.prototype = {
    changeName:DI(function(name,my){
    
        var oldname = this.name;
        var newname = this.name = name;
        
        // publish change name event. 
        my.emit("change name",{oldname:oldname,newname:newname});
        
    })
}
 
 
var emitter = new Emitter;
emitter.on("change name",function(data){
    console.log("old name is "+data.oldname);
    console.log("new name is "+data.newname);
});
 
// inject 
User.prototype.my_ = emitter;
 
// test run  
var user = new User();
user.changeName("leo");

result is:

old name is brighthas
new name is leo