Skip to content

Instantly share code, notes, and snippets.

@tjsingleton
Forked from plukevdh/lazy_mustachio.coffee
Created April 25, 2013 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjsingleton/5460226 to your computer and use it in GitHub Desktop.
Save tjsingleton/5460226 to your computer and use it in GitHub Desktop.
function lazyCompileFactory(id, html) {
crunch.global.templates[id] = function(context) {
var compiled = Handlebars.compile(html);
crunch.global.templates[id] = compiled;
return compiled(context);
}
}
$('script[type="text/x-handlebars-template"]').each(function() {
var text = this.textContent;
if (!text) { text = this.innerHTML; } // IE 8
lazyCompileFactory(this.id, text);
});
class Mustachio extends Backbone.View
render: (template_id, context) ->
@templates[template_id].call @, context
Mustachio.prepare = ->
templates = {}
raw_templates = $('script[type="text/x-handlebars-template"]')
raw_templates.each (i, template) ->
$template = $(template)
templates[$template.attr('id')] = Handlebars.compile($template.html())
Mustachio::templates = templates
window.Mustachio = Mustachio
$ ->
Mustachio.prepare()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment