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
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,34 @@ Note that now the `mysql` module is injected by Electrolyte, rather than
explicitly `require()`'d. This makes it easy to write tests for this component
while mocking out network access entirely.

## Quick-API

```javascript
IoC.loader(***loader***);
````

Registers a Component to the IoC Container. ***loader*** is the result, given by on of the following component loaders.

```javascript
IoC.node_modules(***moduleName***);
````

Creates a loader for a previously installed NPM package. Returns the loader, which is a function.

```javascript
IoC.node(***searchPath***);
````

Creates a loader for a any script below the given directory. Let's say you directory is named ***app/***, and it contains subdirectories with a component named ***app/services/hello.js***, you will be able to @require "services/hello" later. Returns the loader, which is a function.

```javascript
IoC.value(***componentName***, ***component***);
````

Creates a loader for a given value/component. Pretty useful when you are generating objects during runtime (configuration files etc).



## Examples

- __[Express](https://github.com/jaredhanson/electrolyte/tree/master/examples/express)__
Expand Down
18 changes: 18 additions & 0 deletions lib/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,24 @@ Container.prototype.create = function(id, parent) {
return comp.create(this);
}

/**
*/
Container.prototype.inject = function(fn) {
var dependencies = fn['@require'].map(function(id) {
var result;

if (!this._o[id]) {
this._loadModule(id);
}

result = this._o[id] ? this._o[id].obj : this._o[id];

return result;
}, this);

return fn.apply(fn, dependencies);
};

Container.prototype.factory = function(id, dependencies, fn, sid) {
if (typeof dependencies == 'function') {
fn = dependencies;
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ exports.Container = Container;

exports.node = require('./loaders/node');
exports.node_modules = require('./loaders/node_modules');
exports.value = require('./loaders/value');
5 changes: 5 additions & 0 deletions lib/loaders/value.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = function(key, value) {
return function(id) {
return id === key ? value : undefined;
};
}