Skip to content

Instantly share code, notes, and snippets.

@Vinze
Last active August 29, 2015 14:25
Show Gist options
  • Save Vinze/22215ebff035a7a6f15c to your computer and use it in GitHub Desktop.
Save Vinze/22215ebff035a7a6f15c to your computer and use it in GitHub Desktop.
Resource loader ($.when wrapper)
function fetch(urls, callback) {
var requests = [];
var data = [];
for (var i = 0; i < urls.length; i++) {
requests.push($.get(urls[i]));
}
$.when.apply($, requests).then(function() {
if (typeof arguments[0] === 'object') {
for (var i = 0; i < arguments.length; i++) {
data.push(arguments[i][0]);
}
} else {
data.push(arguments[0]);
}
callback.apply(this, data);
});
}
// Example:
fetch(['templates/userlist.html', 'api/users.json'], function(template, users) {
console.log(template);
console.log(users);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment