Last active
August 29, 2015 14:25
-
-
Save Vinze/22215ebff035a7a6f15c to your computer and use it in GitHub Desktop.
Resource loader ($.when wrapper)
This file contains hidden or 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
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