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
This commit is contained in:
Roman Shtylman 2014-01-24 19:49:45 -05:00
parent 337ab24899
commit 4983c38298
8 changed files with 23 additions and 29 deletions

View File

@ -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)

View File

@ -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;

View File

@ -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();
}

View File

@ -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.
*

View File

@ -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);

1
test/fixtures/name.jade vendored Normal file
View File

@ -0,0 +1 @@
p= name

View File

@ -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();
});

View File

@ -48,6 +48,21 @@ describe('res', function(){
.expect('<p>tobi</p>', 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('<p>tobi</p>', done);
})
it('should support index.<engine>', function(done){
var app = express();