Last active
December 14, 2015 15:59
-
-
Save JasonOffutt/5112094 to your computer and use it in GitHub Desktop.
CoffeeScript implementation of TemplateManager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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