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
@ -71,8 +71,8 @@ TemplateCompiler.prototype = {
|
||||
}
|
||||
/*
|
||||
* 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.
|
||||
* The checks to prevent transformers from being applied multiple times makes
|
||||
* 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
|
||||
* nodes added by transformers are also transformed.
|
||||
*/
|
||||
do {
|
||||
this._transformerApplied = false;
|
||||
//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);
|
||||
},
|
||||
compile: function (src, callback, thisObj) {
|
||||
@ -100,15 +100,13 @@ 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';
|
||||
|
||||
function returnError(err) {
|
||||
if (callback) {
|
||||
return callback.call(thisObj, err);
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
var error = new Error(message);
|
||||
error.errors = _this.getErrors();
|
||||
throw error;
|
||||
}
|
||||
|
||||
try {
|
||||
@ -118,10 +116,11 @@ TemplateCompiler.prototype = {
|
||||
rootNode = parser.parse(src, filePath, this.taglibs);
|
||||
//Build a parse tree from the input XML
|
||||
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);
|
||||
} 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);
|
||||
}
|
||||
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);
|
||||
var err = createError(new Error('An error occurred while trying to compile template at path "' + filePath + '". Exception: ' + e), e);
|
||||
return returnError(err);
|
||||
}
|
||||
|
||||
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) {
|
||||
return expression instanceof Expression;
|
||||
|
||||
@ -40,7 +40,7 @@ var defaultOptions = {
|
||||
extend(exports, {
|
||||
createCompiler: function (path, options) {
|
||||
var TemplateCompiler = require('./TemplateCompiler');
|
||||
//Get a reference to the TemplateCompiler class
|
||||
//Get a reference to the TemplateCompiler class
|
||||
if (options) {
|
||||
/*
|
||||
* If options were provided then they should override the default options.
|
||||
@ -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