From adc58f21cc7fa54be74cfe2c0e56877f29da0b86 Mon Sep 17 00:00:00 2001 From: Patrick Steele-Idem Date: Wed, 18 Jun 2014 21:10:58 -0600 Subject: [PATCH] Separate function after figuring out args --- runtime/raptor-templates-runtime.js | 54 ++++++++++++----------------- 1 file changed, 22 insertions(+), 32 deletions(-) diff --git a/runtime/raptor-templates-runtime.js b/runtime/raptor-templates-runtime.js index 4973f2e4a..01b634e18 100644 --- a/runtime/raptor-templates-runtime.js +++ b/runtime/raptor-templates-runtime.js @@ -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) { this._ = renderFunc; @@ -57,40 +68,19 @@ Template.prototype = { // callback is last argument if provided callback = arguments[arguments.length - 1]; if (typeof callback === 'function') { - - // found a callback function - 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) { + if (arguments.length === 2) { // data, context, callback + callback = context; context = new Context(); - shouldEnd = true; } - // A context object was provided instead of a callback - this._(data, context); //Invoke the template rendering function with the required arguments - - if (shouldEnd) { - context.end(); + renderWithCallback(this, data, context, callback); + } else { + if (context.attributes) { + this._(data, context); + } else { + // Assume the "context" is really a stream + context = new Context(context); + this._(data, context); + context.end(); // End the context and the underlying stream } }