From 4983c38298d1c6dad1a8a09241cf8beee66beb7e Mon Sep 17 00:00:00 2001 From: Roman Shtylman Date: Fri, 24 Jan 2014 19:49:45 -0500 Subject: [PATCH] change res.locals to a plain js object. Anyone who wants something fancier should use modules. - fixes annoyance with not being able to set 'name' property on locals --- History.md | 1 + lib/application.js | 3 +-- lib/middleware.js | 2 +- lib/utils.js | 19 ------------------- test/app.locals.js | 4 ++-- test/fixtures/name.jade | 1 + test/res.locals.js | 7 ++----- test/res.render.js | 15 +++++++++++++++ 8 files changed, 23 insertions(+), 29 deletions(-) create mode 100644 test/fixtures/name.jade diff --git a/History.md b/History.md index ad94194c..b64a6a6a 100644 --- a/History.md +++ b/History.md @@ -10,6 +10,7 @@ - `req.accepts*` -> `req.accepts*s` - i.e. `req.acceptsEncoding` -> `req.acceptsEncodings` - `req.params` is now an object instead of an array - `json spaces` no longer enabled by default in development + - `res.locals` is no longer a function. It is a plain js object. Treat it as such. * refactor: - `req.accepts*` with [accepts](https://github.com/expressjs/accepts) - `req.is` with [type-is](https://github.com/expressjs/type-is) diff --git a/lib/application.js b/lib/application.js index 6aa39110..b0c2de60 100644 --- a/lib/application.js +++ b/lib/application.js @@ -8,7 +8,6 @@ var connect = require('connect') , methods = require('methods') , middleware = require('./middleware') , debug = require('debug')('express:application') - , locals = require('./utils').locals , View = require('./view') , http = require('http'); @@ -72,7 +71,7 @@ app.defaultConfiguration = function(){ }); // setup locals - this.locals = locals(this); + this.locals = Object.create(null); // default locals this.locals.settings = this.settings; diff --git a/lib/middleware.js b/lib/middleware.js index e07dd4cd..625719d1 100644 --- a/lib/middleware.js +++ b/lib/middleware.js @@ -25,7 +25,7 @@ exports.init = function(app){ req.__proto__ = app.request; res.__proto__ = app.response; - res.locals = res.locals || utils.locals(res); + res.locals = res.locals || Object.create(null); next(); } diff --git a/lib/utils.js b/lib/utils.js index 6379b076..2de05b5d 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -24,25 +24,6 @@ exports.etag = function(body){ return '"' + crc32.signed(body) + '"'; }; -/** - * Make `locals()` bound to the given `obj`. - * - * This is used for `app.locals` and `res.locals`. - * - * @param {Object} obj - * @return {Function} - * @api private - */ - -exports.locals = function(){ - function locals(obj){ - for (var key in obj) locals[key] = obj[key]; - return obj; - }; - - return locals; -}; - /** * Check if `path` looks absolute. * diff --git a/test/app.locals.js b/test/app.locals.js index 58ccb854..9f5fc1f2 100644 --- a/test/app.locals.js +++ b/test/app.locals.js @@ -7,8 +7,8 @@ describe('app', 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); diff --git a/test/fixtures/name.jade b/test/fixtures/name.jade new file mode 100644 index 00000000..ede3527a --- /dev/null +++ b/test/fixtures/name.jade @@ -0,0 +1 @@ +p= name diff --git a/test/res.locals.js b/test/res.locals.js index 6cc4049f..8861bac7 100644 --- a/test/res.locals.js +++ b/test/res.locals.js @@ -3,15 +3,12 @@ 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(); }); diff --git a/test/res.render.js b/test/res.render.js index a865929f..11c01aa1 100644 --- a/test/res.render.js +++ b/test/res.render.js @@ -47,6 +47,21 @@ describe('res', function(){ .get('/') .expect('

tobi

', done); }) + + it('should expose app.locals with `name` property', function(done){ + var app = express(); + + app.set('views', __dirname + '/fixtures'); + app.locals.name = 'tobi'; + + app.use(function(req, res){ + res.render('name.jade'); + }); + + request(app) + .get('/') + .expect('

tobi

', done); + }) it('should support index.', function(done){ var app = express();