From 964c65db3703738b60b28fc6b496691ddd360eae Mon Sep 17 00:00:00 2001 From: Dexter Miguel Date: Sat, 6 Feb 2016 22:25:52 -0500 Subject: [PATCH] Full support for nested objects --- README.md | 19 +++++++++++++++++++ nwire.js | 12 ++++++++++-- package.json | 2 +- spec/nwire.js | 19 +++++++++++++++++++ 4 files changed, 49 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f0f78aa..86ff305 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/nwire.js b/nwire.js index 965cc8d..ec64ba0 100644 --- a/nwire.js +++ b/nwire.js @@ -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]; @@ -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); @@ -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); } }); }); diff --git a/package.json b/package.json index 3dc0ac7..d159d4f 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/spec/nwire.js b/spec/nwire.js index f0053b1..a51392b 100644 --- a/spec/nwire.js +++ b/spec/nwire.js @@ -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': {