Skip to content

Instantly share code, notes, and snippets.

@ScottMaclure
Created September 24, 2014 09:22
Show Gist options
  • Save ScottMaclure/e57a6eb116b774ad5731 to your computer and use it in GitHub Desktop.
Save ScottMaclure/e57a6eb116b774ad5731 to your computer and use it in GitHub Desktop.
jQuery loadBlazeTemplates
/**
* Load remote template code via XHR
* Then create Blaze Template from it.
* Will result in Template[templateName] being defined.
*/
function loadBlazeTemplates(options) {
var d = $.Deferred();
var loadTotal = Object.keys(options).length;
var loadCount = 0;
$.each(options, function (templateName, relativePath) {
// FIXME global variable. Too magic. Was to get this working in test harness.
var path = connectServerBaseUrl ? connectServerBaseUrl + relativePath : relativePath;
var xhr = $.get(path);
xhr.done(function (data) {
loadCount++;
// Code lifted from blaze.devel.js!
// Evil eval!
/*jshint ignore:start*/
var renderFuncCode = Spacebars.compile(data);
eval("Template.__define__(" + JSON.stringify(templateName) + ", " + renderFuncCode + ");");
/*jshint ignore:end*/
// If we've loaded all Templates, resolve promise to allow caller to proceed.
if (loadCount === loadTotal) {
d.resolve();
}
});
xhr.fail(function () {
console.error('Failed to load relativePath:', relativePath);
});
});
return d.promise();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment