Skip to content

Instantly share code, notes, and snippets.

@tylergaw
Created October 5, 2011 21:18
Show Gist options
  • Save tylergaw/1265771 to your computer and use it in GitHub Desktop.
Save tylergaw/1265771 to your computer and use it in GitHub Desktop.
Retrieve a template file via an Ajax request and add it to ICanHazJS's template store.
// Make a request to retrieve an html template.
// @param OBJECT options
mxAdmin.tmpl = function (templateName, data, callback, options) {
var defaults = {
// Providing a filename will allow for naming the ich template
// one name and then requesting a different file from the server
filename: templateName,
extension: 'html',
dataType: 'html',
templateLoc: './templates/',
error: function (jqxhr) {
console.log('There was an error retrieving the template', jqxhr);
}
},
settings = null
template = null,
url = null,
tmplReady = function () {
template = ich[templateName](data);
if ($.isFunction(callback)) {
callback(template);
}
};
settings = $.extend(defaults, options || {});
// If we've used the template before, it will be in the ich
// cache and we can just pull it from there.
if (ich[templateName]) {
tmplReady();
// If not, we'll make an ajax request to grab the template html
} else {
url = (settings.templateLoc + settings.filename);
url += '.' + settings.extension;
$.ajax({
url: url,
type: 'GET',
dataType: settings.dataType,
success: function (res) {
ich.addTemplate(templateName, res);
tmplReady();
},
error: settings.error
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment