Skip to content
Myr

Replaced `res.local[s]()` with `res.locals` function. Closes #757

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent 04ce039 · Browse files · 4 files+32 added−51 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+32 added−51 removed

  • tests:+15 added−15 removed
  • other:+17 added−36 removed

4 files+32 added−51 removed

modified:lib/http.js+15 added
86 unmodified lines
87 var query = url.parse(req.url).query;88 req.query = qs.parse(query);89 }9091 /**92 * Assign several locals with the given `obj`,93 * or return the locals.94 *95 * @param {Object} obj96 * @return {Object|ServerResponse}97 * @api public98 */99100 res.locals = function(obj){101 for (var key in obj) {102 res.locals[key] = obj[key];103 }104 };105106 next();107 });108109 // apply middleware
448 unmodified lines
modified:lib/response.js-34 removed
417 unmodified lines
418 this.statusCode = status;419 this.header('Location', url);420 this.end(body);421};422423/**424 * Assign the view local variable `name` to `val` or return the425 * local previously assigned to `name`.426 *427 * @param {String} name428 * @param {Mixed} val429 * @return {ServerResponse} for chaining430 * @api public431 */432433res.local = function(name, val){434 this._locals = this._locals || {};435 if (1 == arguments.length) return this._locals[name];436 this._locals[name] = val;437 return this;438};439440/**441 * Assign several locals with the given `obj`,442 * or return the locals.443 *444 * @param {Object} obj445 * @return {Object|ServerResponse}446 * @api public447 */448449res.locals = function(obj){450 if (0 == arguments.length) return this._locals;451 for (var key in obj) {452 this.local(key, obj[key]);453 }454 return this;455};
modified:lib/view.js+2 added-2 removed
342 unmodified lines
343 var cid = app.enabled('view cache')344 ? view + (parent ? ':' + parent.path : '')345 : false;346347 // merge res._locals348 if (this._locals) merge(options, this._locals);347 // merge res.locals348 merge(options, this.locals);349350 // merge render() options351 if (opts) merge(options, opts);352
103 unmodified lines
modified:test/view.test.js+15 added-15 removed
615 unmodified lines
616 { url: '/stats/callback' },617 { body: 'got: <p>Hits 12</p><p>Misses 1</p>' });618619 app.get('/stats/callback/2', function(req, res){620 res.locals({ hits: 12, misses: 1 });620 res.locals.hits = 12;621 res.locals({ misses: 1 });622 res.partial('stats', function(err, html){623 res.send('got: ' + html);624 });625 });
343 unmodified lines
969 { url: '/video' },970 { body: '<h1>Wahoo</h1>' });971 },972 972 'test res.local()': function(){973 'test res.locals': function(){974 var app = create();975 976 app.get('/video', function(req, res, next){976 res.local('open', '<?');977 res.local('close', '?>');978 res.local('title', 'Wahoo');977 res.locals.open = '<?';978 res.locals.close = '?>';979 res.locals.title = 'Wahoo';980 res.render('video.ejs', { layout: false });981 });982 983 assert.response(app,984 { url: '/video' },985 { body: '<h1>Wahoo</h1>' });986 },987 987 'test res.local() render() precedence': function(){988 'test res.locals render() precedence': function(){989 var app = create();990 991 app.get('/video', function(req, res, next){991 res.local('open', '<?');992 res.local('close', '?>');993 res.local('title', 'Wahoo');992 res.locals.open = '<?';993 res.locals.close = '?>';994 res.locals.title = 'Wahoo';995 res.render('video.ejs', { layout: false, title: 'keyboard cat' });996 });997 998 assert.response(app,999 { url: '/video' },1000 { body: '<h1>keyboard cat</h1>' });1001 },1002 1002 'test res.local() partials': function(){1003 'test res.locals partials': function(){1004 var app = create();10051006 app.local('site', 'My Cool Pets');1007 1008 app.get('/pets', function(req, res, next){1008 res.local('pets', ['Tobi']);1009 res.locals.pets = ['Tobi'];1010 next();1011 });1012 1013 app.get('/pets', function(req, res, next){1013 var pets;1014 if (pets = res.local('pets')) {1015 pets.push('Jane', 'Bandit');1014 if (pets = res.locals.pets) {1015 res.locals.pets.push('Jane', 'Bandit');1016 }1017 next();1018 });1019
54 unmodified lines
1074 res.render('hello.jade');1075 });10761077 app.get('/local', function(req, res){1078 res.local('charset', 'ISO-8859-1');1078 res.locals.charse = 'ISO-8859-1';1079 res.render('hello.jade');1080 });10811082 app.get('/render', function(req, res){
29 unmodified lines