Last active
December 18, 2015 01:09
-
-
Save jakebellacera/5702009 to your computer and use it in GitHub Desktop.
BikeShop - simple AJAX template manager (with caching!) for Handlebars.js. http://jsfiddle.net/jakebellacera/89XER/
This file contains 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
// BikeShop | |
// | |
// Simple asynchronous template management for Handlebars.js. | |
// | |
// By Jake Bellacera (http://jakebellacera.com) | |
// | |
// Usage: | |
// 1. Include Handlebars in your project. | |
// 2. Include BikeShop in you project. | |
// 4. Initialize a new BikeShop instance with the path to the templates dir as | |
// an argument (example below). | |
// | |
// Example: | |
// templates/hello_world.handlebars | |
// -------------------------------- | |
// Hello, {{ name }}! | |
// | |
// script.js | |
// --------- | |
// var shop = new BikeShop('/path/to/templates/dir'), // no trailing slash | |
// element = document.getElementById('foo'); | |
// shop.render('hello_world', function (template) { | |
// element.innerHTML = template({ name: 'World' }); | |
// }); | |
// | |
// index.html | |
// ---------- | |
// <div id="foo">Hello, World!</div> | |
var BikeShop = function(directory) { | |
this.templatesDirectory = directory; | |
return { | |
cache: {}, | |
// Renders a template. If the template is not found within the cache, | |
// fetch it asynchronously and render the callback when it has loaded. | |
// | |
// Arguments: | |
// name - (string) the name of the template. | |
// callback - (function) a callback function that will be fired after the | |
// template has been successfully fetched via AJAX. The | |
// callback function will be passed the raw Handlebars template | |
// as an argument. | |
// | |
// Returns nothing. | |
render: function(name, callback) { | |
if (this.isCached(name)) { | |
callback(this.cache[name]); | |
} else { | |
this.fetch(name, callback); | |
} | |
}, | |
// Fetches a template from a URL via GET, store the template in the cache | |
// and then fires the callback function. If the template was previously | |
// saved in the cache, replace the template with the new one. | |
// | |
// Arguments: | |
// name - (string) the name of the template. | |
// callback - (function) a callback function that will be fired after the | |
// template has been successfully fetched via AJAX. The | |
// callback function will be passed the raw Handlebars template | |
// as an argument. | |
// | |
// Returns nothing. | |
fetch: function(name, callback) { | |
var shop = this, | |
request = new XMLHttpRequest; | |
request.open('GET', shop.pathFor(name), true); | |
request.onload = function () { | |
if (request.status >= 200 && request.status < 400) { | |
shop.store(name, request.responseText); | |
if (typeof callback === 'function') { | |
callback(shop.cache[name]); | |
} | |
} | |
}; | |
}, | |
// Checks if a template exists in the cache. | |
// | |
// Arguments: | |
// name - (string) the name of the template in the cache | |
// | |
// Returns true if it exists, false if it does not exist. | |
isCached: function(name) { | |
return this.cache[name] != null; | |
}, | |
// Stores a raw Handlebars template into the cache. This can be useful if | |
// you need to store a template manually. | |
// | |
// Arguments: | |
// name - (string) the name of the template. | |
// raw_template - (string) the string handlebars template. | |
// | |
// Returns the raw Handlebars template. | |
store: function(name, raw_template) { | |
return this.cache[name] = Handlebars.compile(raw_template); | |
}, | |
// Fetches a path for the handlebars template. | |
// | |
// Arguments: | |
// name - (string) name of the template in the cache. | |
// | |
// Returns the url for the template file. | |
pathFor: function(name) { | |
return this.templatesDirectory + "/" + name + ".handlebars"; | |
} | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
But I ride my bike with no handlebars..