Last active
August 29, 2015 14:01
-
-
Save geraintluff/30f838e68ad1d3e4a591 to your computer and use it in GitHub Desktop.
A little tv4 extension that can pre-fetch all required schemas.
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
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