Skip to content

Instantly share code, notes, and snippets.

@geraintluff
Last active August 29, 2015 14:01
Show Gist options
  • Save geraintluff/30f838e68ad1d3e4a591 to your computer and use it in GitHub Desktop.
Save geraintluff/30f838e68ad1d3e4a591 to your computer and use it in GitHub Desktop.
A little tv4 extension that can pre-fetch all required schemas.
tv4.asyncFetchSchemas = function (urls, callback) {
var activeRequests = 0;
var attemptedRequest = {};
if (!Array.isArray(urls)) urls = [urls];
function requestFinished(url) {
activeRequests--;
var missing = tv4.getMissingUris();
for (var i = 0; i < missing; i++) {
if (!attemptedRequest[missing[i]]) {
fetchSchema(missing[i]);
}
}
if (activeRequests <= 0) {
callback();
}
}
function fetchSchema(url) {
activeRequests++;
attemptedRequest[url] = true;
$.getJSON(url).success(function (schema) {
tv4.addSchema(url, schema);
requestFinished(url);
}).error(function () {
console.log("Error fetching schema: " + url);
tv4.addSchema(url, {});
requestFinished(url);
});
}
urls.forEach(fetchSchema);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment