Merge pull request #1908 from visionmedia/locals-object
Before adoption: this commit came in with the history the project adopted, so no landing records it.
change res.locals to a plain js object.
authored by Roman Shtylman · committed by Roman Shtylman · parents 337ab24, 4983c38 · Browse files · 8 files·+23 added−29 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
8 files+23 added−29 removed
- tests:+20 added−7 removed
- other:+3 added−22 removed
Provenance
- Commit
991c2a9- Tree
aa9bccb- Author
Roman Shtylman <shtylman@gmail.com>- Committer
Roman Shtylman <shtylman@gmail.com>- Parent 1
337ab24- Parent 2
4983c38
8 files changed+23 added−29 removed
modified:lib/application.js+1 added-2 removed
6 unmodified lines7 , Router = require('./router')8 , methods = require('methods')9 , middleware = require('./middleware')10 , debug = require('debug')('express:application')11- , locals = require('./utils').locals11 , View = require('./view')12 , http = require('http');13 14 /**55 unmodified lines70 return this._router.middleware;71 });72 73 // setup locals75- this.locals = locals(this);74+ this.locals = Object.create(null);75 76 // default locals77 this.locals.settings = this.settings;78 397 unmodified linesmodified:lib/middleware.js+1 added-1 removed
23 unmodified lines2425262728282930313223 unmodified lines
req.__proto__ = app.request; res.__proto__ = app.response;
res.locals = res.locals || utils.locals(res); res.locals = res.locals || Object.create(null);
next(); }};modified:lib/utils.js-19 removed
23 unmodified lines24 return '"' + crc32.signed(body) + '"';25 };26 27 /**28- * Make `locals()` bound to the given `obj`.29- *30- * This is used for `app.locals` and `res.locals`.31- *32- * @param {Object} obj33- * @return {Function}34- * @api private35- */36-37-exports.locals = function(){38- function locals(obj){39- for (var key in obj) locals[key] = obj[key];40- return obj;41- };42-43- return locals;44-};45-46-/**28 * Check if `path` looks absolute.29 *30 * @param {String} path31 * @return {Boolean}127 unmodified linesadded:test/fixtures/name.jade+1 added
1+p= namemodified:test/app.locals.js+2 added-2 removed
5 unmodified lines6789101110111213141512 unmodified lines5 unmodified lines describe('.locals(obj)', function(){ it('should merge locals', function(){ var app = express(); Object.keys(app.locals).should.eql(['settings']); app.locals({ user: 'tobi', age: 1 }); app.locals({ age: 2 }); app.locals.user = 'tobi'; app.locals.age = 2; Object.keys(app.locals).should.eql(['settings', 'user', 'age']); app.locals.user.should.equal('tobi'); app.locals.age.should.equal(2); })12 unmodified linesmodified:test/res.locals.js+2 added-5 removed
1234567678910111213141213141526 unmodified lines
var express = require('../') , request = require('./support/http');
describe('res', function(){ describe('.locals(obj)', function(){ it('should merge locals', function(done){ describe('.locals', function(){ it('should be empty by default', function(done){ var app = express();
app.use(function(req, res){ Object.keys(res.locals).should.eql([]); res.locals({ user: 'tobi', age: 1 }); res.locals.user.should.equal('tobi'); res.locals.age.should.equal(1); res.end(); });
request(app)26 unmodified linesmodified:test/res.render.js+15 added
45 unmodified lines46 request(app)47 .get('/')48 .expect('<p>tobi</p>', done);49 })50+51+ it('should expose app.locals with `name` property', function(done){52+ var app = express();53+54+ app.set('views', __dirname + '/fixtures');55+ app.locals.name = 'tobi';56+57+ app.use(function(req, res){58+ res.render('name.jade');59+ });60+61+ request(app)62+ .get('/')63+ .expect('<p>tobi</p>', done);64+ })65 66 it('should support index.<engine>', function(done){67 var app = express();68 204 unmodified linesmodified:History.md+1 added
8 unmodified lines9 * change:10 - `req.accepts*` -> `req.accepts*s` - i.e. `req.acceptsEncoding` -> `req.acceptsEncodings`11 - `req.params` is now an object instead of an array12 - `json spaces` no longer enabled by default in development13+ - `res.locals` is no longer a function. It is a plain js object. Treat it as such.14 * refactor:15 - `req.accepts*` with [accepts](https://github.com/expressjs/accepts)16 - `req.is` with [type-is](https://github.com/expressjs/type-is)17 1252 unmodified lines