Added res.locals.use(). Closes #1120
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by TJ Holowaychuk · committed by TJ Holowaychuk · parent f3a32f2 · Browse files · 5 files·+150 added−25 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+150 added−25 removed
- tests:+104 added−0 removed
- other:+46 added−25 removed
Provenance
- Commit
dd33ef2- Tree
8405deb- Author
TJ Holowaychuk <tj@vision-media.ca>- Committer
TJ Holowaychuk <tj@vision-media.ca>- Parent
f3a32f2
5 files changed+150 added−25 removed
modified:lib/application.js+4 added-19 removed
12 unmodified lines13 , Router = require('./router')14 , methods = Router.methods.concat('del', 'all')15 , middleware = require('./middleware')16 , debug = require('debug')('express:application')17+ , locals = require('./utils').locals18 , View = require('./view')19 , url = require('url')20 , utils = connect.utils21 , path = require('path')42 unmodified lines64 // implicit middleware65 this.use(connect.query());66 this.use(middleware.init(this));67 67- // app locals68- this.locals = function(obj){69- for (var key in obj) self.locals[key] = obj[key];70- return self;71- };72-73- // response locals74- this.locals.use = function(fn){75- if (3 == fn.length) {76- self.viewCallbacks.push(fn);77- } else {78- self.viewCallbacks.push(function(req, res, done){79- fn(req, res);80- done();81- });82- }83- return this;84- };85-68 // inherit view callbacks69 this.on('mount', function(parent){70 this.request.__proto__ = parent.request;71 this.response.__proto__ = parent.response;10 unmodified lines82 this._router.strict = this.enabled('strict routing');83 return this._router.middleware;84 });85 86+ // setup locals87+ this.locals = locals(this);88+89 // default locals90 this.locals.settings = this.settings;91 92 // default configuration448 unmodified linesmodified:lib/middleware.js+8 added-5 removed
123445678910111213141516171812 unmodified lines31323334293031323536373839
/*! * Express - middleware * Copyright(c) 2010-2011 TJ Holowaychuk <tj@vision-media.ca> * Copyright(c) 2010-2012 TJ Holowaychuk <tj@vision-media.ca> * MIT Licensed */
/** * Module dependencies. */
var utils = require('./utils');
/** * Initialization middleware, exposing the * request and response to eachother, as well * as defaulting the X-Powered-By header field. *12 unmodified lines
req.__proto__ = app.request; res.__proto__ = app.response;
res.locals = function(obj){ for (var key in obj) res.locals[key] = obj[key]; return res; }; res.locals = utils.locals(res);
next(); }};modified:lib/response.js+1 added-1 removed
625 unmodified lines626 app.render(view, options, fn);627 }628 629 // invoke view callbacks630- var callbacks = app.viewCallbacks630+ var callbacks = app.viewCallbacks.concat(self.viewCallbacks)631 , pending = callbacks.length632 , len = pending633 , done;634 18 unmodified linesmodified:lib/utils.js+33 added
10 unmodified lines11 12 var mime = require('mime');13 14 /**15+ * Make `locals()` bound to the given `obj`.16+ * 17+ * This is used for `app.locals` and `res.locals`. 18+ *19+ * @param {Object} obj20+ * @return {Function}21+ * @api private22+ */23+24+exports.locals = function(obj){25+ obj.viewCallbacks = obj.viewCallbacks || [];26+27+ function locals(obj){28+ for (var key in obj) locals[key] = obj[key];29+ return obj;30+ };31+32+ locals.use = function(fn){33+ if (3 == fn.length) {34+ obj.viewCallbacks.push(fn);35+ } else {36+ obj.viewCallbacks.push(function(req, res, done){37+ fn(req, res);38+ done();39+ });40+ }41+ return obj;42+ };43+44+ return locals;45+};46+47+/**48 * Check if `path` looks absolute.49 *50 * @param {String} path51 * @return {Boolean}235 unmodified linesadded:test/res.locals.use.js+104 added
1+2+var express = require('../')3+ , request = require('./support/http');4+5+describe('res', function(){6+ describe('.locals.use(fn)', function(){7+ it('should run in parallel on res.render()', function(done){8+ var app = express();9+ var calls = [];10+ app.set('views', __dirname + '/fixtures');11+12+ app.locals.first = 'tobi';13+14+ app.use(function(req, res, next){15+ res.locals.use(function(req, res, done){16+ process.nextTick(function(){17+ calls.push('one');18+ res.locals.last = 'holowaychuk';19+ done();20+ });21+ });22+ next();23+ });24+25+ app.use(function(req, res, next){26+ res.locals.use(function(req, res, done){27+ process.nextTick(function(){28+ calls.push('two');29+ res.locals.species = 'ferret';30+ done();31+ });32+ });33+ next();34+ });35+36+ app.use(function(req, res){37+ calls.push('render');38+ res.render('pet.jade');39+ });40+ 41+ request(app)42+ .get('/')43+ .end(function(res){44+ calls.should.eql(['render', 'one', 'two']);45+ res.body.should.equal('<p>tobi holowaychuk is a ferret</p>');46+ done();47+ })48+ })49+ 50+ describe('with arity < 3', function(){51+ it('should done() for you', function(done){52+ var app = express();53+54+ app.set('views', __dirname + '/fixtures');55+ app.locals.first = 'tobi';56+57+ app.use(function(req, res, next){58+ app.locals.use(function(req, res){59+ res.locals.last = 'holowaychuk';60+ res.locals.species = 'ferret';61+ });62+ next();63+ });64+65+ app.use(function(req, res){66+ res.render('pet.jade');67+ });68+69+ request(app)70+ .get('/')71+ .end(function(res){72+ res.body.should.equal('<p>tobi holowaychuk is a ferret</p>');73+ done();74+ })75+ })76+ })77+78+ it('should not override res.render() locals', function(done){79+ var app = express();80+81+ app.set('views', __dirname + '/fixtures');82+ app.locals.first = 'tobi';83+84+ app.use(function(req, res, next){85+ app.locals.use(function(req, res){86+ res.locals.last = 'holowaychuk';87+ res.locals.species = 'ferret';88+ });89+ next();90+ });91+92+ app.use(function(req, res){93+ res.render('pet.jade', { last: 'ibot' });94+ });95+96+ request(app)97+ .get('/')98+ .end(function(res){99+ res.body.should.equal('<p>tobi ibot is a ferret</p>');100+ done();101+ })102+ })103+ })104+})