Skip to content
Myr

Added res.partial() callback support

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 4874404 · Browse files · 3 files+60 added−5 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+60 added−5 removed

  • tests:+22 added−0 removed
  • other:+38 added−5 removed

3 files+60 added−5 removed

modified:examples/jade/app.js+9 added
modified:lib/view.js+29 added-5 removed
128 unmodified lines
129 }130};131132/**133 * Render `view` partial with the given `options`.133 * Render `view` partial with the given `options`. Optionally a 134 * callback `fn(err, str)` may be passed instead of writing to135 * the socket.136 *137 * Options:138 *139 * - `object` Single object with name derived from the view (unless `as` is present)
6 unmodified lines
146 * - `collection` Array of objects, the name is derived from the view name itself. 147 * For example _video.html_ will have a object _video_ available to it.148 *149 * @param {String} view148 * @param {Object|Array} options, collection, or object150 * @param {Object|Array|Function} options, collection, callback, or object151 * @param {Function} fn152 * @return {String}153 * @api public154 */155153res.partial = function(view, options){156res.partial = function(view, options, fn){157 var app = this.app158 , options = options || {}159 , parent = {};160161 // accept callback as second argument162 if ('function' == typeof options) {163 fn = options;164 options = {};165 }166167 // root "views" option168 parent.dirname = app.set('views') || process.cwd() + '/views';169170 // utilize "view engine" option171 if (app.set('view engine')) {172 parent.extension = '.' + app.set('view engine');173 }174166 var str = renderPartial(this, view, options, null, parent);167 this.send(str);175 // render the partial176 try {177 var str = renderPartial(this, view, options, null, parent);178 } catch (err) {179 if (fn) {180 fn(err);181 } else {182 throw err;183 }184 }185186 // callback or transfer187 if (fn) {188 fn(null, str);189 } else {190 this.send(str);191 }192};193194/**195 * Render `view` with the given `options` and optional callback `fn`.
170 unmodified lines
modified:test/view.test.js+22 added
576 unmodified lines
577 578 assert.response(app,579 { url: '/items' },580 { body: '<li>test foo</li><li>test bar</li>' });581 582 app.get('/stats/callback', function(req, res){583 res.partial('stats', { hits: 12, misses: 1 }, function(err, html){584 res.send('got: ' + html);585 });586 });587588 assert.response(app,589 { url: '/stats/callback' },590 { body: 'got: <p>Hits 12</p><p>Misses 1</p>' });591592 app.get('/stats/callback/2', function(req, res){593 res.local('hits', 12);594 res.local('misses', 1);595 res.partial('stats', function(err, html){596 res.send('got: ' + html);597 });598 });599600 assert.response(app,601 { url: '/stats/callback/2' },602 { body: 'got: <p>Hits 12</p><p>Misses 1</p>' });603 },604 605 'test #partial() with several calls': function(){606 var app = create();
419 unmodified lines