0.2.2 • Published 10 years ago

dicontainer v0.2.2

Weekly downloads
2
License
MIT
Repository
github
Last release
10 years ago

#dicontainer

Tiny dependency container

  • Handles resolution of dependencies
  • Lazy instantiation (services arent created until they are needed)
  • Understands Angular.js providers/services/factories/globals
  • Provides simple React.js compatible Mixin for attaching services
  • Tests for circular dependencies
  • less than 1kb (gzipped)

###Basic Usage

####Setup

var Container = require('dicontainer');

var appContainer = new Container(logFunction /* optional */);

####Registration

container.register(name, factory /* or service/provider */);

function MathService(other) {}
MathService.prototype.add = function (a,b) { return a+b; };
MathService.$inject = ['OtherService']; 

appContainer.register('MathService', MathService); // Dependencies are read from $inject property

// --- or ----

function MathService(other) {}
MathService.prototype.add = function (a,b) { return a+b; };

appContainer.register('MathService', MathService, ['OtherService']); // Dependencies are specified as array

// --- or ----

function MathService(other) {}
MathService.prototype.add = function (a,b) { return a+b; };

appContainer.register('MathService', ['OtherService', MathService]); // Dependencies and service are specified as array

####Resolution

var instance = container.resolve(name);

var MathService = appContainer.resolve('MathService');
MathService.add(1,2);

####Mixin

Service instances are attached to this.services

var Calculator = React.createClass({
	
	mixins: [appContainer.Mixin('MathService', 'OtherService')]

	render: function () {
		this.services.MathService.add()
	}
});

####Building & testing

npm run build // lint, test & compile dist
npm test // test in terminal
npm run test-local // run test in browser
0.2.2

10 years ago

0.2.1

10 years ago

0.2.0

10 years ago

0.1.0

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago