Skip to content

Commit

Permalink
Merge pull request expressjs#1908 from visionmedia/locals-object
Browse files Browse the repository at this point in the history
change res.locals to a plain js object.
  • Loading branch information
defunctzombie committed Jan 28, 2014
2 parents 337ab24 + 4983c38 commit 991c2a9
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 29 deletions.
1 change: 1 addition & 0 deletions History.md
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions lib/application.js
Expand Up @@ -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');

Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion lib/middleware.js
Expand Up @@ -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();
}
Expand Down
19 changes: 0 additions & 19 deletions lib/utils.js
Expand Up @@ -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.
*
Expand Down
4 changes: 2 additions & 2 deletions test/app.locals.js
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/name.jade
@@ -0,0 +1 @@
p= name
7 changes: 2 additions & 5 deletions test/res.locals.js
Expand Up @@ -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();
});

Expand Down
15 changes: 15 additions & 0 deletions test/res.render.js
Expand Up @@ -47,6 +47,21 @@ describe('res', function(){
.get('/')
.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();
Expand Down

0 comments on commit 991c2a9

Please sign in to comment.