Created
September 24, 2014 09:22
-
-
Save ScottMaclure/e57a6eb116b774ad5731 to your computer and use it in GitHub Desktop.
jQuery loadBlazeTemplates
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
/** | |
* 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