Merge pull request #130 from kpdecker/defer-compile

Defer compile
This commit is contained in:
Peter Wagenet
2011-10-24 17:27:54 -07:00
+15 -4
View File
@@ -756,10 +756,21 @@ Handlebars.precompile = function(string, options) {
Handlebars.compile = function(string, options) {
options = options || {};
var ast = Handlebars.parse(string);
var environment = new Handlebars.Compiler().compile(ast, options);
var templateSpec = new Handlebars.JavaScriptCompiler().compile(environment, options, undefined, true);
return Handlebars.template(templateSpec);
var compiled;
function compile() {
var ast = Handlebars.parse(string);
var environment = new Handlebars.Compiler().compile(ast, options);
var templateSpec = new Handlebars.JavaScriptCompiler().compile(environment, options, undefined, true);
return Handlebars.template(templateSpec);
}
// Template is only compiled on first use and cached after that point.
return function(context, options) {
if (!compiled) {
compiled = compile();
}
return compiled.call(this, context, options);
};
};
// END(BROWSER)