Added res.local() for progressive view locals
Before adoption: this commit came in with the history the project adopted, so no landing records it.
for middleware etc. res.local("foo", "bar") => "bar" res.local("foo") => "bar"
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent 2bfdfe3 · Browse files · 3 files·+112 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+112 added−10 removed
- tests:+87 added−8 removed
- other:+25 added−2 removed
Provenance
- Commit
6b0d82f- Tree
a83d171- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
2bfdfe3
3 files changed+112 added−10 removed
modified:lib/express/response.js+18 added
371 unmodified lines372 373 // Respond374 this.send(body, { Location: url }, status);375 };376+377+/**378+ * Assign the view local variable `name` to `val` or return the379+ * local previously assigned to `name`.380+ *381+ * @param {String} name382+ * @param {Mixed} val383+ * @return {Mixed} val384+ * @api public385+ */386+387+http.ServerResponse.prototype.local = function(name, val){388+ this.locals = this.locals || {};389+ return undefined === val390+ ? this.locals[name]391+ : this.locals[name] = val;392+};393+modified:lib/express/view.js+7 added-2 removed
81 unmodified lines82 }83 84 // Inherit locals from parent85 merge(options, locals);86-86+ 87 // Merge locals88 if (options.locals) {89 merge(options, options.locals);90 }74 unmodified lines165 , dynamicHelpers = app.dynamicViewHelpers166 , viewOptions = app.settings['view options']167 , cacheTemplates = app.settings['cache views'];168 169- // Mixin "view options"169+ // Merge "view options"170 if (viewOptions) options = merge(clone(viewOptions), options);171 172 // Defaults173 var self = this37 unmodified lines211 ? new Partial(orig.indexPath, options)212 : new View(orig.indexPath, options);213 }214 215+ // Merge res.locals216+ if (this.locals) {217+ options = merge(this.locals, options);218+ }219+ 220 // Dynamic helper support221 if (false !== options.dynamicHelpers) {222 // cache223 if (!this.__dynamicHelpers) {55 unmodified linesmodified:test/view.test.js+87 added-8 removed
768 unmodified lines769 assert.response(app,770 { url: '/' },771 { body: '<p>This is actually jade :)</p>' });772 },773-774- 'test global-variable-leak `str`': function() {773+ 774+ 'test res.local()': function(){775 var app = create();776- app.set('view engine', 'jade');776 778- app.get('/', function(req, res){779- res.render('index', { layout: 'cool-layout' });780- assert.equal(typeof global.str, 'undefined');777+ app.get('/video', function(req, res, next){778+ res.local('open', '<?');779+ res.local('close', '?>');780+ res.local('title', 'Wahoo');781+ res.render('video.ejs', { layout: false });782 });783+ 784+ assert.response(app,785+ { url: '/video' },786+ { body: '<h1>Wahoo</h1>' });787+ },788+ 789+ 'test res.local() render() precedence': function(){790+ var app = create();791+ 792+ app.get('/video', function(req, res, next){793+ res.local('open', '<?');794+ res.local('close', '?>');795+ res.local('title', 'Wahoo');796+ res.render('video.ejs', { layout: false, title: 'keyboard cat' });797+ });798+ 799+ assert.response(app,800+ { url: '/video' },801+ { body: '<h1>keyboard cat</h1>' });802+ },803+ 804+ 'test res.local() "view options" precedence': function(){805+ var app = create();806+ 807+ app.set('view options', {808+ layout: false809+ , open: '<?'810+ , title: 'Should not show :)'811+ });812+ 813+ app.get('/video', function(req, res, next){814+ res.local('close', '?>')815+ res.local('title', 'Wahoo');816+ res.render('video.ejs', { layout: false, title: 'keyboard cat' });817+ });818+ 819+ assert.response(app,820+ { url: '/video' },821+ { body: '<h1>keyboard cat</h1>' });822+ },823+ 824+ 'test res.local() partials': function(){825+ var app = create();826+ 827+ app.set('view options', {828+ site: 'My Cool Pets'829+ });830+ 831+ app.get('/pets', function(req, res, next){832+ res.local('pets', ['Tobi']);833+ next();834+ });835+ 836+ app.get('/pets', function(req, res, next){837+ var pets;838+ if (pets = res.local('pets')) {839+ pets.push('Jane', 'Bandit');840+ }841+ next();842+ });843 844+ app.get('/pets', function(req, res, next){845+ res.render('pets.jade');846+ });847+848+ var html = [849+ '<html>',850+ '<body>',851+ '<h1>My Cool Pets</h1>',852+ '<p>We have 3 cool pets\n</p>',853+ '<ul>',854+ '<li>Tobi is the coolest of My Cool Pets</li>',855+ '<li>Jane</li>',856+ '<li>Bandit</li>',857+ '</ul>',858+ '</body>',859+ '</html>'860+ ].join('');861+862 assert.response(app,784- { url: '/' },785- { body: '<cool><p>Welcome</p></cool>' });863+ { url: '/pets' },864+ { body: html });865 }866 };