Skip to content
Browse files

refactor

  • Loading branch information...
1 parent c25ce8d commit 0bdef90314ba4230e71da33f01f9f97ef72c3def @aheckmann aheckmann committed Oct 2, 2012
Showing with 80 additions and 84 deletions.
  1. +80 −84 index.js
View
164 index.js
@@ -14,98 +14,33 @@ exports.version = '0.0.4';
* Module dependencies.
*/
-var res = require('http').ServerResponse.prototype
- , express = require('express')
+var express = require('express')
, Promise = require('mongoose').Promise
, Query = require('mongoose').Query
, slice = require('sliced')
/**
- * Wrap the original rendering methods with support
- * for Queries and Promises.
+ * Wrap response.render with support for mongoose
+ * Queries and Promises.
*/
-express.response.render = wrap(express.response.render);
-
-function wrap (method) {
- return function expressmongoose (view, options, callback) {
-
- if (!options || 'function' == typeof options) {
- return method.call(this, view, options, callback);
- }
-
- var self = this;
- return resolve(options, function (err, result) {
- if (err) {
- return 'function' == typeof callback
- ? callback(err)
- : self.req.next(err);
- }
-
- // must return here so partials always work
- return method.call(self, view, result, callback);
- });
+var render = express.response.render;
+express.response.render = function expressmongoose_render (view, options, callback) {
+ if (!options || 'function' == typeof options) {
+ return render.call(this, view, options, callback);
}
-}
-
-/**
- * Resolves any Queries and Promises within the passed options.
- */
-function resolve (options, callback, nested) {
- var keys = Object.keys(options)
- , i = keys.length
- , remaining = []
- , pending
- , item
- , key;
-
- while (i--) {
- key = keys[i];
- item = options[key];
- if (item instanceof Query || item instanceof Promise) {
- item.key = key;
- remaining.push(item);
- }
- }
-
- pending = remaining.length;
- if (options.locals) ++pending;
-
- if (!pending) {
- return callback(null, options);
- }
-
- function error (err) {
- if (error.ran) return;
- callback(error.ran = err);
- }
-
- remaining.forEach(function (item) {
- function handleResult (err, result) {
- if (err) return error(err);
- options[item.key] = result;
- --pending || callback(null, options);
+ var self = this;
+ return resolve(options, function (err, result) {
+ if (err) {
+ return 'function' == typeof callback
+ ? callback(err)
+ : self.req.next(err);
}
- if (item instanceof Query) {
- item.exec(handleResult);
- } else {
- item.addBack(handleResult);
- }
+ // must return here so partials always work
+ return render.call(self, view, result, callback);
});
-
- if (nested) return;
-
- // locals support
- if (options.locals) {
- return resolve(options.locals, function (err, resolved) {
- if (err) return error(err);
- options.locals = resolved;
- if (--pending) return;
- return callback(null, options);
- }, true);
- }
}
/**
@@ -152,14 +87,13 @@ express.response.send = function expressmongoose_send () {
* promise.complete(url [, status]);
*
* The promise may pass an optional status code as the
- * second argument.
+ * first argument.
*
- * promise.complete('/elsewhere', 301);
+ * promise.complete(301, '/elsewhere');
*/
-// TODO
var redirect = express.response.redirect;
-express.response.redirect = function () {
+express.response.redirect = function expressmongoose_redirect () {
var self = this;
var args = slice(arguments);
@@ -184,3 +118,65 @@ express.response.redirect = function () {
// TODO res.json
// TODO res.jsonp
+
+/**
+ * Resolves any Queries and Promises within the passed options.
+ * @api private
+ */
+
+function resolve (options, callback, nested) {
+ var keys = Object.keys(options)
+ , i = keys.length
+ , remaining = []
+ , pending
+ , item
+ , key;
+
+ while (i--) {
+ key = keys[i];
+ item = options[key];
+ if (item instanceof Query || item instanceof Promise) {
+ item.key = key;
+ remaining.push(item);
+ }
+ }
+
+ pending = remaining.length;
+ if (options.locals) ++pending;
+
+ if (!pending) {
+ return callback(null, options);
+ }
+
+ function error (err) {
+ if (error.ran) return;
+ callback(error.ran = err);
+ }
+
+ remaining.forEach(function (item) {
+ function handleResult (err, result) {
+ if (err) return error(err);
+ options[item.key] = result;
+ --pending || callback(null, options);
+ }
+
+ if (item instanceof Query) {
+ item.exec(handleResult);
+ } else {
+ item.addBack(handleResult);
+ }
+ });
+
+ if (nested) return;
+
+ // locals support
+ if (options.locals) {
+ return resolve(options.locals, function (err, resolved) {
+ if (err) return error(err);
+ options.locals = resolved;
+ if (--pending) return;
+ return callback(null, options);
+ }, true);
+ }
+}
+

0 comments on commit 0bdef90

Please sign in to comment.
Something went wrong with that request. Please try again.