mirror of
https://github.com/marko-js/marko.git
synced 2025-12-08 19:26:05 +00:00
Fixing usage of calling compile function with callback
This commit is contained in:
parent
15b3efa8a1
commit
adf0b5de41
@ -100,16 +100,14 @@ TemplateCompiler.prototype = {
|
||||
var filePath = this.path;
|
||||
var rootNode;
|
||||
var templateBuilder;
|
||||
function 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();
|
||||
|
||||
function returnError(err) {
|
||||
if (callback) {
|
||||
return callback.call(thisObj, err);
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
/*
|
||||
@ -121,7 +119,8 @@ TemplateCompiler.prototype = {
|
||||
//The templateBuilder object is need to manage the compiled JavaScript output
|
||||
this.transformTree(rootNode, templateBuilder);
|
||||
} 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 {
|
||||
@ -130,20 +129,26 @@ TemplateCompiler.prototype = {
|
||||
*/
|
||||
rootNode.generateCode(templateBuilder); //Generate the code and have all output be managed by the TemplateBuilder
|
||||
} 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 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();
|
||||
//Get the compiled output from the template builder
|
||||
//console.error('COMPILED TEMPLATE (' + filePath + ')\n', '\n' + output, '\n------------------');
|
||||
|
||||
if (callback) {
|
||||
callback.call(thisObj, output);
|
||||
callback.call(thisObj, null, output);
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
},
|
||||
isExpression: function (expression) {
|
||||
return expression instanceof Expression;
|
||||
|
||||
@ -54,8 +54,13 @@ extend(exports, {
|
||||
return new TemplateCompiler(path, options);
|
||||
},
|
||||
|
||||
compile: function (src, path, options) {
|
||||
return this.createCompiler(path, options).compile(src);
|
||||
compile: function (src, path, options, callback) {
|
||||
if (typeof options === 'function') {
|
||||
callback = options;
|
||||
options = undefined;
|
||||
}
|
||||
|
||||
return this.createCompiler(path, options).compile(src, callback);
|
||||
},
|
||||
|
||||
compileFile: function(path, options, callback) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user