Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implement dependency injection variout of routes boot phase.

  • Loading branch information...
commit 0bd9f84674437a4e3340b33e3d0dc16b798599c8 1 parent 480b34a
Jared Hanson authored
Showing with 50 additions and 0 deletions.
  1. +45 −0 lib/boot/di/routes.js
  2. +2 −0  lib/index.js
  3. +3 −0  test/package.test.js
45 lib/boot/di/routes.js
View
@@ -0,0 +1,45 @@
+/**
+ * Module dependencies.
+ */
+var scripts = require('scripts')
+ , path = require('path')
+ , fs = require('fs')
+ , existsSync = fs.existsSync || path.existsSync // node <=0.6
+ , debug = require('debug')('locomotive:di');
+
+
+module.exports = function(options, container) {
+ if (!container) {
+ try {
+ container = require('electrolyte');
+ } catch (_) {
+ // workaround when `npm link`'ed for development
+ var prequire = require('parent-require');
+ container = prequire('electrolyte');
+ }
+ }
+
+ if ('string' == typeof options) {
+ options = { filename: options };
+ }
+ options = options || {};
+ var filename = options.filename || 'config/routes'
+ , extensions = options.extensions;
+
+ return function routes() {
+ var script = scripts.resolve(path.resolve(filename), extensions);
+ if (!existsSync(script)) { return; }
+
+ var mod = require(script);
+ var deps = mod['@require'] || []
+ , args = [];
+
+ for (var i = 0, len = deps.length; i < len; ++i) {
+ debug('create %s', deps[i]);
+ var inst = container.create(deps[i], this);
+ args.push(inst);
+ }
+
+ mod.apply(this.__router, args);
+ };
+};
2  lib/index.js
View
@@ -32,6 +32,8 @@ exports.boot.views = require('./boot/views');
exports.boot.routes = require('./boot/routes');
exports.boot.httpServer = require('./boot/httpserver');
exports.boot.httpServerCluster = require('./boot/httpservercluster');
+exports.boot.di = {};
+exports.boot.di.routes = require('./boot/di/routes');
/**
* Export CLI.
3  test/package.test.js
View
@@ -26,6 +26,9 @@ describe('locomotive', function() {
expect(locomotive.boot.routes).to.be.a('function');
expect(locomotive.boot.httpServer).to.be.a('function');
expect(locomotive.boot.httpServerCluster).to.be.a('function');
+
+ expect(locomotive.boot.di).to.be.an('object');
+ expect(locomotive.boot.di.routes).to.be.a('function');
});
});
Please sign in to comment.
Something went wrong with that request. Please try again.