Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Asynchronous files loading with jquery deferred with callback
//Usage
var tplsToLoad = [
'tpl/menu-section',
'tpl/header',
'tpl/footer'
];
window.FilesLoader.load(tplsToLoad,
function () {
//Do what you want to do after all templates loaded
});
// Main `TemplateManager` object
// The Template Loader. Used to asynchronously load files
window.FilesLoader = {
files: {},
load: (function(views, callback) {
var deferreds = [], tplPath;
var defaults = {
path: opt.tpl.options.path,
ext: opt.tpl.options.ext
};
$.each(views, function(index, view) {
var self = this;
//Convert to string
view = "" + view;
//tpl file path
tplPath = "" + defaults.path + view + defaults.ext;
deferreds.push( $.ajax({
url: tplPath,
success: function(tplString) {
self.files[view] = {
tplString: tplString,
compiled: Handlebars.compile(tplString)
};
},
dataType: 'html',
cache: true,
}) );
});
$.when.apply($, deferreds).done(callback);
}).bind(this);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.