diff --git a/README.md b/README.md index 557e71c..5584677 100644 --- a/README.md +++ b/README.md @@ -131,6 +131,20 @@ location where an application's components are found: IoC.loader(IoC.node('app/components')); ``` +#### Injecting a literal component + +Manually created components may be directly injected into the container and used as +module dependencies. This is particularly useful in circumstances where a +dependency must be created asynchronously. Manually created components are injected +using the container's `use()` method and the `literal()` loader: + +```javascript +var myComponent = new MyComponent('a', 'b'); + +// makes myComponent an injectable dependency with id "myComp" +IoC.use(IoC.literal('myComp', myComponent)); +``` + #### @require vs require() Loading components is similar in many regards to `require`ing a module, with diff --git a/lib/index.js b/lib/index.js index 26acdaa..4ab31e4 100644 --- a/lib/index.js +++ b/lib/index.js @@ -8,3 +8,4 @@ exports.Container = Container; exports.fs = exports.node = require('./loaders/node'); exports.node_modules = require('./loaders/node_modules'); +exports.literal = require('./loaders/literal'); diff --git a/lib/loaders/literal.js b/lib/loaders/literal.js new file mode 100644 index 0000000..caace31 --- /dev/null +++ b/lib/loaders/literal.js @@ -0,0 +1,16 @@ +/** + * Returns a component definition which will, when queried with the + * provided componentId, return the provided literal component. + * + * @param componentId The identifier for the provided component + * @param component The literal component to be added + * @returns {Function} To return the provided component when the appropriate + * component id is specified. + */ +module.exports = function (componentId, component) { + + return function (id) { + return (componentId == id) ? component : null; + }; + +}; diff --git a/test/container.literal.test.js b/test/container.literal.test.js index e93d932..e4ee27c 100644 --- a/test/container.literal.test.js +++ b/test/container.literal.test.js @@ -1,57 +1,56 @@ /* global describe, it, expect */ var Container = require('../lib/container'); - +var Literal = require('../lib/loaders/literal'); // Bacteria function Bacteria(food) { - this.food = food; + this.food = food; } -Bacteria.prototype.eat = function() { - return this.food; -} +Bacteria.prototype.eat = function () { + return this.food; +}; +describe.skip('Container#literal', function () { -describe.skip('Container#literal', function() { - - describe('creating an object', function() { - var container = new Container(); - var lit = new Bacteria('starch'); - //container.literal('bacteria', lit); - - //var obj = container.create('bacteria'); - - it('should create an object', function() { - expect(obj).to.be.an('object'); - expect(obj).to.be.an.instanceOf(Bacteria); - expect(obj).to.equal(lit); - }); - - it('should not create unique instances', function() { - var obj2 = container.create('bacteria'); - expect(obj).to.be.equal(obj2); - }); - }); - - describe('creating an object from object literal', function() { - var container = new Container(); - var lit = { foo: 'bar' }; - //container.literal('foo', lit); - - //var obj = container.create('foo'); - - it('should create an object', function() { - expect(obj).to.be.an('object'); - expect(obj).to.be.an.instanceOf(Object); - expect(obj).to.equal(lit); + describe('creating an object', function () { + var container = new Container(); + var lit = new Bacteria('starch'); + container.use(Literal('bacteria', lit)); + + var obj = container.create('bacteria'); + + it('should create an object', function () { + expect(obj).to.be.an('object'); + expect(obj).to.be.an.instanceOf(Bacteria); + expect(obj).to.equal(lit); + }); + + it('should not create unique instances', function () { + var obj2 = container.create('bacteria'); + expect(obj).to.be.equal(obj2); + }); }); - - it('should not create unique instances', function() { - var obj2 = container.create('foo'); - expect(obj).to.be.equal(obj2); + + describe('creating an object from object literal', function () { + var container = new Container(); + var lit = {foo: 'bar'}; + container.use(Literal('foo', lit)); + + var obj = container.create('foo'); + + it('should create an object', function () { + expect(obj).to.be.an('object'); + expect(obj).to.be.an.instanceOf(Object); + expect(obj).to.equal(lit); + }); + + it('should not create unique instances', function () { + var obj2 = container.create('foo'); + expect(obj).to.be.equal(obj2); + }); }); - }); - + });