From 77adfbf7b8e98280116bc5cabcba56e5837733e1 Mon Sep 17 00:00:00 2001 From: rolux Date: Sun, 17 Jun 2012 15:36:55 +0200 Subject: [PATCH] Requests.js: handle errors, various other improvements --- source/Ox/js/Request.js | 239 ++++++++++++++++++++++------------------ 1 file changed, 133 insertions(+), 106 deletions(-) diff --git a/source/Ox/js/Request.js b/source/Ox/js/Request.js index d335b5a1..0351f8cf 100644 --- a/source/Ox/js/Request.js +++ b/source/Ox/js/Request.js @@ -2,93 +2,78 @@ /*@ Ox.get Get a remote file - # fixme: remote? same-origin-policy? jsonp? (url, callback) -> undefined url Remote URL callback Callback function data The contents of the remote resource + error Error, or null + code Status code + text Status text @*/ Ox.get = function(url, callback) { - var req = new XMLHttpRequest(); - req.open('GET', url, true); - req.onreadystatechange = function() { - if (req.readyState == 4) { - if (req.status == 200) { - callback(req.responseText); + var request = new XMLHttpRequest(); + request.open('GET', url, true); + request.onreadystatechange = function() { + if (request.readyState == 4) { + if (request.status == 200) { + callback(request.responseText, null); } else { - throw new Error( - 'Cannot get URL "' + url + '" (Status: ' + req.status + ')' - ); + callback(null, { + code: request.status, + text: request.statusText + }); } } }; - req.send(); + request.send(); }; /*@ -Ox.getJSON Get and parse one or more remote JSON files - # fixme: remote? same-origin-policy? - (url, callback) -> undefined - url One or more remote URLs +Ox.getAsync Runs an asynchonous loader for an array of URLs + (urls, map, callback) -> undefined + urls URL or array of either URLs or arrays of URLs + Multiple URLs in the same array will be processed simultaneously, but + multiple arrays of URLs will be processed in that order. callback Callback function - data The parsed contents of the remote resource(s) - For multiple URLs, keys are file names, values are contents + results Results + Keys are file names, values are results + errors Errors, or null + Keys are file names, values are error objects @*/ -Ox.getJSON = function(url, callback) { - var urls = Ox.makeArray(url); - Ox.loadAsync(urls, function(url, callback) { - Ox.get(url, function(data) { - var result = {}; - result[url] = JSON.parse(data); - callback(result); +Ox.getAsync = function(urls, map, callback) { + urls = Ox.clone(Ox.makeArray(urls)); + var errors = {}, i = 0, n = urls.length, results = {}; + function done() { + callback( + n == 1 ? results[urls[0]] : results, + n == 1 ? errors[urls[0]] : Ox.some(errors, function(error) { + return error !== null; + }) ? errors : null + ); + } + function iterate() { + var url = urls.shift(); + Ox.getAsync(url, function(result, error) { + Ox.extend(results, url, result); + Ox.extend(errors, url, error); + urls.length ? iterate() : done(); }); - }, function(results) { - callback(urls.length == 1 ? results[url] : results); - }); -} - -/*@ -Ox.getJSONP Get and parse one or more remote JSONP files - (url, callback) -> undefined - url One or more remote URLs, - {callback} gets replaced with jsonp callback function name - callback Callback function - data The parsed contents of the remote resource(s) - For multiple URLs, keys are file names, values are contents -@*/ -Ox.getJSONP = function(url, callback) { - var urls = Ox.makeArray(url); - Ox.loadAsync(urls, function(url, callback) { - var id = 'callback' + Ox.uid(); - Ox.getJSONP[id] = function(data) { - delete Ox.getJSONP[id]; - callback(data); - } - Ox.$('body').append(Ox.$('