Skip to content

Commit

Permalink
Add support to getUrl for a callback function to be called inremental…
Browse files Browse the repository at this point in the history
…ly while the data is loading

git-svn-id: https://svn.lojjic.net/zomby/trunk@836 8f8d9d7b-adf4-0310-9a85-ad1d9f542d00
  • Loading branch information
Jason Johnston committed Feb 21, 2009
1 parent baf1dca commit 28172e2
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/main/javascript/zomby/Util.js
Expand Up @@ -52,7 +52,7 @@ zomby.Util = {
id = el[exp] = "zomby_id_" + new Date().getTime() + Math.random();
}
return id;
}
};
})(),

/**
Expand All @@ -75,23 +75,35 @@ zomby.Util = {

/**
* Retrieve a remote file
* @param {String} url - The URL of the JSON document
* @param {Object} opts - Object holding options for the request. Recognized properties:
* - {String} method - either "GET" or "POST", defaults to GET
* - {String} url - required; the url of the request
* - {Boolean} async - if true then the request will be made asynchronously, defaults to false
* - {String} username - optional username for authentication
* - {String} password - optional password for authentication
* - {Function} partial - optional function to be called incrementally as the result data streams
* in. Passed one argument which is the XMLHttpRequest object, from which the current
* incomplete responseText can be retrieved.
* - {Function} success - a function to be called upon successful response. Passed one argument
* which is the XMLHttpRequest object.
* - {Function} error - a function to be called if the request errors out. If not supplied then an
* exception will be thrown instead. Passed one argument which is the XMLHttpRequest object.
*/
getUrl : function(opts) {
var xhr = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
var xhr = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest(),
done = false;

xhr.onreadystatechange = function() {
if(xhr.readyState == 4) {
if(opts.partial && xhr.readyState == 3) {
(function notify() {
if(!done) {
opts.partial(xhr);
setTimeout(notify, 500);
}
})();
}
else if(xhr.readyState == 4) {
done = true;
if(xhr.status == 200) {
opts.success(xhr);
} else {
Expand Down

0 comments on commit 28172e2

Please sign in to comment.