Separate function after figuring out args

This commit is contained in:
Patrick Steele-Idem 2014-06-18 21:10:58 -06:00
parent 94e7a7a65e
commit adc58f21cc

View File

@ -43,6 +43,17 @@ if (streamPath) {
} }
function renderWithCallback(template, data, context, callback) {
context
.on('end', function() {
callback(null, context.getOutput());
})
.on('error', callback);
template._(data, context); //Invoke the template rendering function with the required arguments
context.end();
}
function Template(renderFunc) { function Template(renderFunc) {
this._ = renderFunc; this._ = renderFunc;
@ -57,40 +68,19 @@ Template.prototype = {
// callback is last argument if provided // callback is last argument if provided
callback = arguments[arguments.length - 1]; callback = arguments[arguments.length - 1];
if (typeof callback === 'function') { if (typeof callback === 'function') {
if (arguments.length === 2) { // data, context, callback
// found a callback function callback = context;
if (arguments.length < 3) {
// context could not have been created so create
context = undefined;
if (arguments.length < 2) {
// data could not have been provided
data = undefined;
}
}
context = context || new Context();
context
.on('end', function() {
callback(null, context.getOutput());
})
.on('error', callback);
this._(data, context); //Invoke the template rendering function with the required arguments
context.end();
} else {
var shouldEnd = false;
if (!context) {
context = new Context(); context = new Context();
shouldEnd = true;
} }
// A context object was provided instead of a callback renderWithCallback(this, data, context, callback);
this._(data, context); //Invoke the template rendering function with the required arguments } else {
if (context.attributes) {
if (shouldEnd) { this._(data, context);
context.end(); } else {
// Assume the "context" is really a stream
context = new Context(context);
this._(data, context);
context.end(); // End the context and the underlying stream
} }
} }