dinject is a light weight javascript dependency injection framework that allows javascript developers removing hard-coded dependencies and making it possible to change them, whether at run-time or compile-time.
Document ####Like the Spring framework in java,you can use DI in javascript with a simple config as follows
var di_config = {
beans: [
{
name: 'Speak',
from: 'humanBehavior'
}
,{
name: 'Walk'
}
,{
name: 'Person',
inject: [{
need: 'Speak',
prop: 'speak'
}, {
need: 'Walk',
prop: 'walk'
}]
}],
libs:[
{
name:'humanBehavior',
path:'human.js'
}
]
}
Then you can use it like this:
di.create(People);
dinject will fill the speak and walk property with new instance or the instance from pool then return a Person instance to you.
Also dinject provide a simple way to load js at runtime.
di.using('humanBehavior',function(){
//do something when the js file loaded.
});
#Document ###Getting Started
Insert the script in any place but below the dinject_config in the html page.
<script type="text/javascript" src="dinject.js"></script>
And then setup the dinject_config for dependency injection maping and libs settings as follow:
var di_config = {
beans: [
{
name: 'Speak',
from: 'humanBehavior'
}
,{
name: 'Walk'
}
,{
name: 'Person',
inject: [{
need: 'Speak',
prop: 'speak'
}, {
need: 'Walk',
prop: 'walk'
}]
}],
libs:[
{
name:'humanBehavior',
path:'human.js'
}
]
}
That's it ,now you can use the following api:
di.create(class or className)
To get an class instance,it could be from the pool or just a new one.and the dependency injection in this instance is just follow your config.
di.using(libName,callback)
To get a script at runtime , the script is config in the dinject config libs.the callback will call when the script loaded.
di.clean()
To manually clean the instances pool.
di.update(config)
To reset the configuration of beans and libs.