Skip to content

Instantly share code, notes, and snippets.

@jido
Created April 23, 2016 16:07
Show Gist options
  • Save jido/1aa424dceb434da4e38973d0a2b7482c to your computer and use it in GitHub Desktop.
Save jido/1aa424dceb434da4e38973d0a2b7482c to your computer and use it in GitHub Desktop.
The function that uses the callback
var handleAllIncludes = function handleAllIncludes(includes, config, rendered, size, finish) {
if (includes.work.length === 0)
{
if (finish !== undefined) finish(rendered);
return;
}
if (finish === undefined)
{
console.warn("Mistigri needs a callback - ignoring all includes");
return;
}
var open_brace = getOption('openBrace', DEFAULT_OPEN_BRACE, config);
var reader = getOption('reader', DEFAULT_READER, config);
var include = includes.work.shift();
var position = include.at + rendered.length - size; // position from end
reader(include.path, function(content) {
var template = content.split(open_brace);
includes.cache[include.path] = template;
var new_includes = {work: [], offset: position, cache: includes.cache};
if (include.render)
{
var inserted = render(template, include.model, config, new_includes);
rendered = rendered.substr(0, position) + inserted + rendered.substr(position);
}
handleAllIncludes(new_includes, config, rendered, rendered.length, function(new_rendered) {
handleAllIncludes(includes, config, new_rendered, size, finish);
});
},
function(error) {
console.error(error);
handleAllIncludes(includes, config, rendered, size, finish);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment