Skip to content

Instantly share code, notes, and snippets.

@JasonOffutt
Last active December 14, 2015 15:59
Show Gist options
  • Save JasonOffutt/5112094 to your computer and use it in GitHub Desktop.
Save JasonOffutt/5112094 to your computer and use it in GitHub Desktop.
CoffeeScript implementation of TemplateManager
TemplateManager =
partials: [] # e.g. - 'loginForm'
templates: {}
get: (id, callback) ->
# If the template is already in the cache, just return it.
if @templates[id] then return callback @templates[id]
# Otherwise, use Traffic Cop to load up the template.
url = "/templates/#{id}.html"
promise = $.trafficCop url
promise.done (template) =>
# Once loading is complete, compile and cache the template for later use.
tmp = Handlebars.compile template
@templates[id] = tmp
callback tmp
registerPartials: (callback) ->
_.each @partials, (partial, index) =>
templateUrl = "/templates/_#{partial}.html"
@get templateUrl, (tmp) =>
Handlebars.registerPartial partial, tmp
if index + 1 is @partials.length then callback()
registerHelpers: (callback) ->
# Handlebars.registerHelper 'escape', (str) ->
# return escape str.toString()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment