ghost-inject v0.1.1
Ghost is simple dependency injection manager. It is easy to use and avaliable in node.js and browser. No dependencies are needed.
Installation
$ npm install ghost-injectUsage
Initialization
Ghost = require("ghost-inject")
injector = new Ghost();Adding services
anyService = {
a: 10
};
injector.addService("any", anyService);Adding factories
Factories are lazy loaded services. Once the service is created by the factory, it is saved and returned each time.
databaseFactory = function() {
return {
connected: true
};
};
injector.addFactory("database", databaseFactory);Calling functions with dependencies
func = function(database) {
// database is injected here if service or factory was provided before
};
injector.call(func)Injecting functions
func = function(database) {
// database is injected here if service or factory was provided before
};
// injector will return function with bound arguments
bound = injector.inject(func);
bound(); // call injected functionboth
callandinjectcan be provided with this parameter as a second parameter. Also additional dependency list can be provided for local dependencies.if collision occurs within specified list and service in the injector, service from the list will be used
Creating instances
car = function(people) {
this.people = people;
this.handbreak = true;
};
myCar = injector.create(car);
peopleargument will here be injected into constructor if service calledpeoplewas previously defined
Resolving dependencies
func = function(firstdep, seconddep) {};
deps = injector.resolve(func);
// deps is now array containing arguments for the given functionGetting function arguments
func = function(a, b, c) {};
deps = injector.getArguments(func)
// deps = ['a', 'b', 'c']