Skip to content

petomalina/ghost-inject

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

#Ghost inject npm versionBuild Status

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-inject

Usage

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 function

both call and inject can 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);

people argument will here be injected into constructor if service called people was previously defined

Resolving dependencies

func = function(firstdep, seconddep) {};

deps = injector.resolve(func);
// deps is now array containing arguments for the given function

Getting function arguments

func = function(a, b, c) {};

deps = injector.getArguments(func)
// deps = ['a', 'b', 'c']

About

Dependency injector in javascript

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published