Fixing usage of calling compile function with callback

This commit is contained in:
Phil Gates-Idem 2014-07-28 22:32:28 -04:00
parent 15b3efa8a1
commit adf0b5de41
2 changed files with 39 additions and 29 deletions

View File

@ -71,8 +71,8 @@ TemplateCompiler.prototype = {
} }
/* /*
* Now process the child nodes by looping over the child nodes * Now process the child nodes by looping over the child nodes
* and transforming the subtree recursively * and transforming the subtree recursively
* *
* NOTE: The length of the childNodes array might change as the tree is being performed. * NOTE: The length of the childNodes array might change as the tree is being performed.
* The checks to prevent transformers from being applied multiple times makes * The checks to prevent transformers from being applied multiple times makes
* sure that this is not a problem. * sure that this is not a problem.
@ -85,14 +85,14 @@ TemplateCompiler.prototype = {
}); });
} }
/* /*
* The tree is continuously transformed until we go through an entire pass where * The tree is continuously transformed until we go through an entire pass where
* there were no new nodes that needed to be transformed. This loop makes sure that * there were no new nodes that needed to be transformed. This loop makes sure that
* nodes added by transformers are also transformed. * nodes added by transformers are also transformed.
*/ */
do { do {
this._transformerApplied = false; this._transformerApplied = false;
//Reset the flag to indicate that no transforms were yet applied to any of the nodes for this pass //Reset the flag to indicate that no transforms were yet applied to any of the nodes for this pass
transformTreeHelper(rootNode); //Run the transforms on the tree transformTreeHelper(rootNode); //Run the transforms on the tree
} while (this._transformerApplied); } while (this._transformerApplied);
}, },
compile: function (src, callback, thisObj) { compile: function (src, callback, thisObj) {
@ -100,15 +100,13 @@ TemplateCompiler.prototype = {
var filePath = this.path; var filePath = this.path;
var rootNode; var rootNode;
var templateBuilder; var templateBuilder;
function handleErrors() {
var message = 'An error occurred while trying to compile template at path "' + filePath + '". Error(s) in template:\n'; function returnError(err) {
var errors = _this.getErrors(); if (callback) {
for (var i = 0, len = errors.length; i < len; i++) { return callback.call(thisObj, err);
message += (i + 1) + ') ' + (errors[i].pos ? '[' + errors[i].pos + '] ' : '') + errors[i].message + '\n'; } else {
throw error;
} }
var error = new Error(message);
error.errors = _this.getErrors();
throw error;
} }
try { try {
@ -118,10 +116,11 @@ TemplateCompiler.prototype = {
rootNode = parser.parse(src, filePath, this.taglibs); rootNode = parser.parse(src, filePath, this.taglibs);
//Build a parse tree from the input XML //Build a parse tree from the input XML
templateBuilder = new TemplateBuilder(this, filePath, rootNode); templateBuilder = new TemplateBuilder(this, filePath, rootNode);
//The templateBuilder object is need to manage the compiled JavaScript output //The templateBuilder object is need to manage the compiled JavaScript output
this.transformTree(rootNode, templateBuilder); this.transformTree(rootNode, templateBuilder);
} catch (e) { } catch (e) {
throw createError(new Error('An error occurred while trying to compile template at path "' + filePath + '". Exception: ' + (e.stack || e)), e); var err = createError(new Error('An error occurred while trying to compile template at path "' + filePath + '". Exception: ' + (e.stack || e)), e);
return returnError(err);
} }
try { try {
@ -130,20 +129,26 @@ TemplateCompiler.prototype = {
*/ */
rootNode.generateCode(templateBuilder); //Generate the code and have all output be managed by the TemplateBuilder rootNode.generateCode(templateBuilder); //Generate the code and have all output be managed by the TemplateBuilder
} catch (e) { } catch (e) {
throw createError(new Error('An error occurred while trying to compile template at path "' + filePath + '". Exception: ' + e), e); var err = createError(new Error('An error occurred while trying to compile template at path "' + filePath + '". Exception: ' + e), e);
} return returnError(err);
if (this.hasErrors()) {
handleErrors();
}
var output = templateBuilder.getOutput();
//Get the compiled output from the template builder
//console.error('COMPILED TEMPLATE (' + filePath + ')\n', '\n' + output, '\n------------------');
if (callback) {
callback.call(thisObj, output);
} }
return output; if (this.hasErrors()) {
var message = 'An error occurred while trying to compile template at path "' + filePath + '". Error(s) in template:\n';
var errors = _this.getErrors();
for (var i = 0, len = errors.length; i < len; i++) {
message += (i + 1) + ') ' + (errors[i].pos ? '[' + errors[i].pos + '] ' : '') + errors[i].message + '\n';
}
var error = new Error(message);
error.errors = _this.getErrors();
return returnError(error);
} else {
var output = templateBuilder.getOutput();
if (callback) {
callback.call(thisObj, null, output);
}
return output;
}
}, },
isExpression: function (expression) { isExpression: function (expression) {
return expression instanceof Expression; return expression instanceof Expression;

View File

@ -40,7 +40,7 @@ var defaultOptions = {
extend(exports, { extend(exports, {
createCompiler: function (path, options) { createCompiler: function (path, options) {
var TemplateCompiler = require('./TemplateCompiler'); var TemplateCompiler = require('./TemplateCompiler');
//Get a reference to the TemplateCompiler class //Get a reference to the TemplateCompiler class
if (options) { if (options) {
/* /*
* If options were provided then they should override the default options. * If options were provided then they should override the default options.
@ -54,8 +54,13 @@ extend(exports, {
return new TemplateCompiler(path, options); return new TemplateCompiler(path, options);
}, },
compile: function (src, path, options) { compile: function (src, path, options, callback) {
return this.createCompiler(path, options).compile(src); if (typeof options === 'function') {
callback = options;
options = undefined;
}
return this.createCompiler(path, options).compile(src, callback);
}, },
compileFile: function(path, options, callback) { compileFile: function(path, options, callback) {