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

@ -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 {
@ -121,7 +119,8 @@ TemplateCompiler.prototype = {
//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()) { if (this.hasErrors()) {
handleErrors(); 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;
} }
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;
}, },
isExpression: function (expression) { isExpression: function (expression) {
return expression instanceof Expression; return expression instanceof Expression;

View File

@ -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) {