Tiny Javascript dependency injection framework, that uses the same syntax as AngularJS.
The api consists of three different methods.
{
noConflict: function () {},
register: {
singleton: function(name, dependencies) {},
factory: function (name, dependencies) {}
},
resolve: function (dependencies) {},
resolveSingleWith: function(dependency, substitutes),
resolveWith: function(substitutes, dependencies)
}
If a singleton is injected it will always be the same instance.
Here you register a factory method, that essentially will be resolved every time it is injected.
Similiar to a AngularJS controller except that it doesn't take a name.
try {
var singletonUnderTest = spry.resolveSingleDependencyWith("MySingleton", {
"MyFactory": {
setState: function () {
console.log("im setstate");
},
getState: function () {
console.log("im getState");
}
}
});
singletonUnderTest.setState("blag");
singletonUnderTest.getState();
} catch (e) {
console.log(e);
}
try {
spry.resolveWith({
"MyFactory": {
setState: function () {
console.log("im setstate");
},
getState: function () {
console.log("im getState");
}
}
}, ["MyFactory", "MySingleton", function Test2(myFactory, mySingleton) {
console.log("#3 factory test:");
myFactory.getState();
console.log("#3 singleton test: ", mySingleton.getState());
}]);
} catch (e) {
console.log(e);
}
Here is a complete example. Notice that the singleton is dependent on the factory, but it is declared before the factory, AND IT WORKS :-D
var spry = require("./spry.js");
spry.register.singleton("MySingleton", ["MyFactory", function MySingleton(myFactory) {
return myFactory;
}]);
spry.register.factory("MyFactory", [function MyFactory() {
var someStateVar = "state1";
return {
setState: function setState(state) {
someStateVar = state;
},
getState: function getState() {
return someStateVar;
}
};
}]);
spry.resolve(["MyFactory", "MySingleton", function Test1(myFactory, mySingleton) {
console.log("#1 factory test: ", myFactory.getState());
myFactory.setState("state2");
console.log("#1 singleton test: ", mySingleton.getState());
mySingleton.setState("state2");
}]);
spry.resolve(["MyFactory", "MySingleton", function Test2(myFactory, mySingleton) {
console.log("#2 factory test: ", myFactory.getState());
console.log("#2 singleton test: ", mySingleton.getState());
}]);
/**
Outputs:
#1 factory test: state1
#1 singleton test: state1
#2 factory test: state1
#2 singleton test: state2
*/
In the browser just add the module and it will expose it self globally on the window object. If there is a conflict use the .noConflict()
method, works like jQuery.
The module should work with AMD loaders too, but this needs to be tested.