mirror of
https://github.com/zebrajr/express.git
synced 2025-12-06 00:19:48 +01:00
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:
parent
337ab24899
commit
4983c38298
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
|
|
|
|||
19
lib/utils.js
19
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.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -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
1
test/fixtures/name.jade
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
p= name
|
||||
|
|
@ -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();
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user