Skip to content

Commit

Permalink
Full support for nested objects
Browse files Browse the repository at this point in the history
  • Loading branch information
divmgl committed Feb 7, 2016
1 parent 6b347ed commit 964c65d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,25 @@ module.exports.fn = function($) {
```
If the `fn` and `needs` properties are not provided, `nwire` will not perform any dependency injection.
## Nested packages
`nwire` will recursively look for packages that implement `fn` and `needs` which will allow you to perform dependency injection on nested objects. It will also inject packages from the parent scope.
```
// components/header.js
module.exports.needs = ['Vue'];
module.exports.fn = function($) { return $.Vue.component({}); }
```
```
// config.js
module.exports = {
Vue: require('vue'),
components: { // Vue is injected even though the object is nested
header: require('./components/header')
}
}
```
## Running the test suite
```
Expand Down
12 changes: 10 additions & 2 deletions nwire.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ module.exports = function(config, callback) {
this.parent = parent;
}

Declaration.prototype.traverseParentFor = function(name) {
if (this.parent)
return this.parent.root[name] || this.parent.traverseParentFor(name);
return undefined;
}

Declaration.prototype.resolve = function(name) {
var self = this;
var decl = self.root[name];
Expand All @@ -18,7 +24,7 @@ module.exports = function(config, callback) {

if (!decl.fn || !decl.needs || typeof decl.fn !== "function"
|| !(decl.needs instanceof Array))
{
{
if (typeof decl === 'object' && !(decl instanceof Array))
for (var member in decl) {
var declaration = new Declaration(decl, self);
Expand All @@ -34,7 +40,9 @@ module.exports = function(config, callback) {
Object.defineProperty(needs, need, {
get: function() {
var parent = self.parent || { root: {} };
return self.root[need] || parent.root[need];
var obj = self.root[need];

return self.root[need] || self.traverseParentFor(need);
}
});
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nwire",
"version": "0.2.2",
"version": "0.2.3",
"description": "Simplified dependency injection in Node.js",
"main": "nwire.js",
"scripts": {
Expand Down
19 changes: 19 additions & 0 deletions spec/nwire.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,25 @@ describe('nwire', function() {
});
});

it('should resolve parent dependencies', function(done) {
wire({
'prov': { value: 123 },
'nested': {
'nested': {
'nested': {
'cons': {
needs: ['prov'],
fn: function($) { return { consumedValue: $.prov.value }; }
}
}
}
}
}, function(err, app) {
expect(app.nested.nested.nested.cons.consumedValue).to.equal(123);
done();
})
});

it('should not crash on circular dependency', function(done) {
wire({
'provc': {
Expand Down

0 comments on commit 964c65d

Please sign in to comment.