Visualizzazione post con etichetta JS IoC. Mostra tutti i post
Visualizzazione post con etichetta JS IoC. Mostra tutti i post

martedì, gennaio 22, 2008

[jsn]A simple JS IoC example code

Inspired by Ninject documentation I decided to port the c# example code to my JS IoC.
Here are the code:

var
shuriken = {
hit:function(who){alert("pierced the "+who+" armor");}
},

sword = {
hit:function(who){alert("Chopped the "+who+" in half");}
};

function Samurai(weapon){
return {
Attack:function(who){
weapon.hit(who);
}
}
}
container.register("samurai",Samurai);
var warrior1 = container.byConstructor("samurai",shuriken),
warrior2 = container.byConstructor("samurai",sword);

warrior1.Attack("The evildoers");
warrior2.Attack("The evildoers");


So simple, So clear!

[jsn]JS IoC on GoogleCode!

Today I moved my JS IoC source on GoogleCode,
and for the occasion i made a little change in the code, so now JS IoC
is perfectly compatible with MooTools ( I tested the beta, but it should work on the current also ) with the Class Module selected.
So now you have 2 choice, or use the old (and perfectly working) code and use
Andrea Giammarchi extend script (I put that also on Google Code) or if you
already use MooTools you can simply take the 1.0MT version and you're ready to go!