Permalink
Browse files

fixed; works with express 3

fixes #10
  • Loading branch information...
1 parent fae2e59 commit c25ce8dc0908931f276de73723f36dfe6627009c @aheckmann aheckmann committed Oct 3, 2012
Showing with 387 additions and 560 deletions.
  1. +34 −25 index.js
  2. +5 −4 package.json
  3. +4 −4 test/fixtures/both.jade
  4. +1 −1 test/fixtures/nested.jade
  5. +2 −2 test/fixtures/promise.jade
  6. +2 −2 test/fixtures/query.jade
  7. +339 −522 test/index.js
View
@@ -15,22 +15,23 @@ exports.version = '0.0.4';
*/
var res = require('http').ServerResponse.prototype
+ , 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.
*/
-res.render = wrap(res.render);
-res.partial = wrap(res.partial);
+express.response.render = wrap(express.response.render);
function wrap (method) {
- return function expressmongoose (view, options, callback, parent, sub) {
+ return function expressmongoose (view, options, callback) {
if (!options || 'function' == typeof options) {
- return method.call(this, view, options, callback, parent, sub);
+ return method.call(this, view, options, callback);
}
var self = this;
@@ -42,7 +43,7 @@ function wrap (method) {
}
// must return here so partials always work
- return method.call(self, view, result, callback, parent, sub);
+ return method.call(self, view, result, callback);
});
}
}
@@ -105,35 +106,36 @@ function resolve (options, callback, nested) {
return callback(null, options);
}, true);
}
-
}
/**
* Add Promise/Query support to res.send.
*/
-var send = res.send;
-res.send = function (body, headers, status) {
+var send = express.response.send;
+express.response.send = function expressmongoose_send () {
+ var args = slice(arguments);
var self = this;
function handleResult (err, result) {
if (err) return self.req.next(err);
- send.call(self, result, headers, status);
+ args[0] = result;
+ send.apply(self, args);
}
- if (body instanceof Promise) {
- return body.addBack(handleResult);
+ if (args[0] instanceof Promise) {
+ return args[0].addBack(handleResult);
}
- if (body instanceof Query) {
- return body.exec(handleResult);
+ if (args[0] instanceof Query) {
+ return args[0].exec(handleResult);
}
- if ('Object' == body.constructor.name) {
- return resolve(body, handleResult);
+ if ('Object' == args[0].constructor.name) {
+ return resolve(args[0], handleResult);
}
- send.call(this, body, headers, status);
+ send.apply(this, args);
};
/**
@@ -149,29 +151,36 @@ res.send = function (body, headers, status) {
* // later...
* promise.complete(url [, status]);
*
- * The promise can pass an optional status code as the
+ * The promise may pass an optional status code as the
* second argument.
*
* promise.complete('/elsewhere', 301);
*/
-var redirect = res.redirect;
-res.redirect = function (url, status) {
+// TODO
+var redirect = express.response.redirect;
+express.response.redirect = function () {
var self = this;
+ var args = slice(arguments);
- function handleResult (err, result, code) {
+ function handleResult (err, url, code) {
if (err) return self.req.next(err);
- if ('string' != typeof result) {
+ if ('string' != typeof url) {
return self.req.next(new Error('URL Expected'));
}
- redirect.call(self, result, code || status);
+ args[0] = url;
+ if (code) args[1] = code;
+ redirect.apply(self, args);
}
- if (url instanceof Promise) {
- return url.addBack(handleResult);
+ if (args[0] instanceof Promise) {
+ return args[0].addBack(handleResult);
}
- redirect.call(this, url, status);
+ redirect.apply(this, args);
}
+
+// TODO res.json
+// TODO res.jsonp
View
@@ -6,13 +6,14 @@
, "keywords": ["express", "mongoose", "mongo", "render", "promise", "query"]
, "homepage": "https://github.com/learnboost/express-mongoose"
, "dependencies": {
+ "sliced": "0.0.3"
}
, "devDependencies": {
- "should": "0.2.1"
- , "mongoose": ">= 1.0.0"
+ "mongoose": ">= 1.0.0"
, "express": ">= 2.0.0"
- , "jade": ">= 0.20.3"
- , "expresso": ">= 0.9.2"
+ , "jade": "0.27.5"
+ , "mocha": "1.6.0"
+ , "superagent": "0.9.5"
}
, "main": "index"
, "engines": { "node": ">= 0.2.0" }
View
@@ -1,7 +1,7 @@
ul
- - each drumset in query
- - if ('electronic' == drumset.type)
+ each drumset in query
+ if ('electronic' == drumset.type)
li= drumset.brand
- - each drumset in promise
- - if ('black' == drumset.color)
+ each drumset in promise
+ if ('black' == drumset.color)
li= drumset.brand
@@ -1,2 +1,2 @@
ul
- != partial('nested-partial')
+ include nested-partial
@@ -1,4 +1,4 @@
ul
- - each drumset in promise
- - if ('GMS' === drumset.brand)
+ each drumset in promise
+ if ('GMS' === drumset.brand)
li= drumset.color
View
@@ -1,4 +1,4 @@
ul
- - each drumset in query
- - if ('electronic' == drumset.type)
+ each drumset in query
+ if ('electronic' == drumset.type)
li= drumset.brand
Oops, something went wrong.

0 comments on commit c25ce8d

Please sign in to comment.