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
Provenance
- Commit
723c908- Tree
bf79007- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
4874404
3 files changed+60 added−5 removed
modified:examples/jade/app.js+9 added
43 unmodified lines444546474849505152535455565758596017 unmodified lines43 unmodified linesapp.get('/', function(req, res){ res.render('users', { users: users });});
app.get('/users/callback', function(req, res){ // a callback is also accepted res.partial('users/user', users, function(err, html){ if (err) throw err; console.log(html); res.send(html); });});
app.get('/users', function(req, res){ // we can use res.partial() as if // we were in a view, utilizing the same api // to render a fragment17 unmodified linesmodified:lib/view.js+29 added-5 removed
128 unmodified lines129 }130 };131 132 /**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 lines146 * - `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 */155 153-res.partial = function(view, options){156+res.partial = function(view, options, fn){157 var app = this.app158 , options = options || {}159 , parent = {};160 161+ // accept callback as second argument162+ if ('function' == typeof options) {163+ fn = options;164+ options = {};165+ }166+167 // root "views" option168 parent.dirname = app.set('views') || process.cwd() + '/views';169 170 // utilize "view engine" option171 if (app.set('view engine')) {172 parent.extension = '.' + app.set('view engine');173 }174 166- 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+ }185+186+ // callback or transfer187+ if (fn) {188+ fn(null, str);189+ } else {190+ this.send(str);191+ }192 };193 194 /**195 * Render `view` with the given `options` and optional callback `fn`.170 unmodified linesmodified:test/view.test.js+22 added
576 unmodified lines577 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+ });587+588+ assert.response(app,589+ { url: '/stats/callback' },590+ { body: 'got: <p>Hits 12</p><p>Misses 1</p>' });591+592+ 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+ });599+600+ 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