Skip to content

amwmedia/infect.js

Repository files navigation

Infect.js - simple dependency injection

Infect.js is a simple way to add the magic of dependency injection to any web project, regardless of the framework on which you choose to write your application. It's infectiously simple!

buy me a cup of coffee?

Features

  • Extremely lightweight (under 1kb min+gz)
  • Has no dependencies of its own
  • Supports module loading systems (RequireJS/AMD, Node.js) with browser global fallback.
  • 4 ways to inject your code (function, class, object, and assignment)
  • Works in every browser I've tested so far

Browser Support (verified)

  • Internet Explorer 6+
  • Chome 14+
  • Firefox 3.6+
  • Android
  • iOS

Installation

Download: Source | Minified

NPM: npm install infect

Bower: bower install infect

Getting Started

Registering a dependency

infect.set(String, Object|Function|Array|etc...)

A simple call to infect.set() with the name you want to use, and the mutable object you'd like to register will do the trick. In the example below we are using a function, but you can register any type of mutable value (Functions, Arrays, Objects, etc).

infect.set('Logger', function (str) {
	// prepend a time to every log line
	console.log((new Date()).toLocaleTimeString() + ' ==> ' + str);
});

Function Injection

infect.func(Function, Object)

When you're writing a function that needs one or more dependency, simply pass it to infect.func() and reference the dependencies as parameters that are prefixed with a dollar sign ($). All dependencies should be added to the end of the parameter list, and they are not expected when you call the function. Optionally you may pass in an object that you want to be used as this inside the function. If no object is provided, a blank object will be created for this purpose (the global scope will not be used).

var foo = infect.func(function (name, age, $Logger) {
	$Logger(name + ' is ' + age);
}, this);

foo('Joe', 27);

// CONSOLE
// 11:50:37 PM ==> Joe is 27

view on jsFiddle


Class Injection

infect.func(Function)

The infect.func() method can also support javascript constructors (classes). Just like above, all dependencies should be added to the end of the parameter list, and they are not expected when you new the constructor.

function Cat(name, $Logger) {
	$Logger(name + ' is a Cat');
	this.name = name;
}
Cat = infect.func(Cat);

var c = new Cat('Mr. Buttons');
infect.get('Logger')('is c a Cat? ' + (c instanceof Cat));

// CONSOLE
// 11:50:37 PM ==> Mr. Buttons is a Cat
// 11:50:37 PM ==> is c a Cat? true

view on jsFiddle


Object Injection

infect.obj(Object, Array)

Sometimes function injection may not work for you, but you'd still like an easy way to pull multiple dependencies into a single place for reference within your code. Object injection suits this nicely and can be done by simply calling infect.obj() with an object and an array of dependency names you'd like to have injected. Any object can be injected except the global Window object (for obvious reasons). If you try to infect the global scope, infect will throw an error.

NOTE: You can reference the dependency with or without the dollar sign prefix ($). Regardless of how you reference it, the object will be injected with the prefixed version for consistency.

var INFECTED = infect.obj({}, ['$Logger']);
var ALSO_INFECTED = {};
infect.obj(ALSO_INFECTED, ['Logger']);

INFECTED.$Logger('foo!');
ALSO_INFECTED.$Logger('bar!');

// CONSOLE
// 11:50:37 PM ==> foo!
// 11:50:37 PM ==> bar!

view on jsFiddle


Assignment

infect.get(String)

As a simple way to reference your dependencies, you can pull them out and assign individual dependencies to variables. This is not recommended as it breaks the consistency of your code, but it is possible so I wanted to show an example of usage.

var log = infect.get('Logger');
log('foo bar!');

// CONSOLE
// 11:50:37 PM ==> foo bar!

view on jsFiddle


Minification and Obfuscation

Function.$infect = Array

When running your code through minification or obfuscation, the function parameters get renamed in order to save space. Obviously this wreaks havoc on Infect's ability to detect what values should be injected into the function at execution. To solve this you can simply add an array of dependency names to the $infect property of your function and infect will inject these values into the function.

var foo = infect.func(function (n, a, L) {
	L(n + ' is ' + a);
}, this);
foo.$infect = ['Logger'];

foo('Joe', 27);

// CONSOLE
// 11:50:37 PM ==> Joe is 27

view on jsFiddle

About

Infectiously simple dependency injection for any JavaScript project

Resources

License

Stars

Watchers

Forks

Packages

No packages published