diconjs
v0.1.0
Published
Symfony2 inspired dipendency injection container in pure JavaScript
Downloads
4
Readme
dicon.js
Symfony2 inspired dipendency injection container in pure JavaScript.
License: MIT
Example Usage
// define a dummy hello world service
var Acme = function (name) {
if (name) {
this.sayHello(name);
}
};
Acme.prototype.sayHello = function (name) {
console.log('Hello ' + name + '!');
};
// define some initial parameters to start with
var someParameters = {
username: "sweikenb"
};
// create the dependency container (if you don't have initial parameters just skipp the arument)
var container = new Dicon(someParameters);
// add a new parameter "on-the-fly"
container.setParameter('repository', "https://github.com/sweikenb/dicon.js");
// register the acme service and add the "username"-parameter as dependency
container.register('acme', Acme, ['%username%']);
// define a service "on-the-fly" and add the "acme"-service and the container itself as dependency
container.register('helloWorld', function (acme, container) {
acme.sayHello('World, please have a look at: '+container.getParameter('repository'));
}, ['acme', '&']);
// HINT: if you like to inject the container itself as an dependency use the special '&' service-id.
// request the services
var greeter = container.get('acme');
container.get('helloWorld');
// call a service method
greeter.sayHello('Octocat');
This will return:
Hello sweikenb!
Hello World, please have a look at: https://github.com/sweikenb/dicon.js!
Hello Octocat!