Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
16 changes: 16 additions & 0 deletions lib/loaders/literal.js
Original file line number Diff line number Diff line change
@@ -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;
};

};
85 changes: 42 additions & 43 deletions test/container.literal.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
});


});