Skip to content

Commit

Permalink
Added res.partial() callback support
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Mar 17, 2011
1 parent 4874404 commit 723c908
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
9 changes: 9 additions & 0 deletions examples/jade/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ app.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
Expand Down
34 changes: 29 additions & 5 deletions lib/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ function renderPartial(res, view, options, parentLocals, parent){
};

/**
* Render `view` partial with the given `options`.
* Render `view` partial with the given `options`. Optionally a
* callback `fn(err, str)` may be passed instead of writing to
* the socket.
*
* Options:
*
Expand All @@ -145,16 +147,23 @@ function renderPartial(res, view, options, parentLocals, parent){
* For example _video.html_ will have a object _video_ available to it.
*
* @param {String} view
* @param {Object|Array} options, collection, or object
* @param {Object|Array|Function} options, collection, callback, or object
* @param {Function} fn
* @return {String}
* @api public
*/

res.partial = function(view, options){
res.partial = function(view, options, fn){
var app = this.app
, options = options || {}
, parent = {};

// accept callback as second argument
if ('function' == typeof options) {
fn = options;
options = {};
}

// root "views" option
parent.dirname = app.set('views') || process.cwd() + '/views';

Expand All @@ -163,8 +172,23 @@ res.partial = function(view, options){
parent.extension = '.' + app.set('view engine');
}

var str = renderPartial(this, view, options, null, parent);
this.send(str);
// render the partial
try {
var str = renderPartial(this, view, options, null, parent);
} catch (err) {
if (fn) {
fn(err);
} else {
throw err;
}
}

// callback or transfer
if (fn) {
fn(null, str);
} else {
this.send(str);
}
};

/**
Expand Down
22 changes: 22 additions & 0 deletions test/view.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,28 @@ module.exports = {
assert.response(app,
{ url: '/items' },
{ body: '<li>test foo</li><li>test bar</li>' });

app.get('/stats/callback', function(req, res){
res.partial('stats', { hits: 12, misses: 1 }, function(err, html){
res.send('got: ' + html);
});
});

assert.response(app,
{ url: '/stats/callback' },
{ body: 'got: <p>Hits 12</p><p>Misses 1</p>' });

app.get('/stats/callback/2', function(req, res){
res.local('hits', 12);
res.local('misses', 1);
res.partial('stats', function(err, html){
res.send('got: ' + html);
});
});

assert.response(app,
{ url: '/stats/callback/2' },
{ body: 'got: <p>Hits 12</p><p>Misses 1</p>' });
},

'test #partial() with several calls': function(){
Expand Down

0 comments on commit 723c908

Please sign in to comment.