1/******** Async Functionality ********/ 2 3/// Send Async Request Function 4/// 5/// Description: This is the function that is actually called by a developer's 6/// code. It accepts a target URL, some content to send to that URL via POST, 7/// and a function to call when the response has been recieved. The callback 8/// function should be setup to accept two parameters: the response text and 9/// the HTTP response status code. 10/// 11function saxf_sendAsync(targetURL, xmlRequest, callback) 12{ 13 newRequest = new saxf_Request(targetURL, xmlRequest, callback); 14 saxf_AddRequest(newRequest); 15 newRequest.Run(); 16} 17 18 19/// Request Object Definition 20/// 21/// Description: This object represents a request. It stores 22/// both the XMLHTTP object and a series of state 23/// properties for the request. 24/// 25function saxf_Request(targetURL, xmlRequest, callback) 26{ 27 this.xmlHTTP = new ActiveXObject('Microsoft.XMLHTTP'); 28 this.xmlRequest = xmlRequest; 29 this.callback = callback; 30 this.targetURL = targetURL; 31 this.status = 0; 32 this.callbackStatus = 0; 33 34 function Run() 35 { 36 this.xmlHTTP.open('POST',this.targetURL,true); 37 this.xmlHTTP.onreadystatechange = saxf_CatchReturn; 38 this.xmlHTTP.send(this.xmlRequest); 39 } 40 this.Run = Run; 41 42} 43 44 45/// Framework Callback Function 46/// 47/// Description: This is the function that all returning requests 48/// use as their callback method. It is basically a bit of a hack, because 49/// the XMLHTTP readystatechange event does not provide any context for its 50/// call into the onreadystatechange handler function. To get around this, 51/// I implemented a bit of a "let-us-see-what-request-is-complete" 52/// logic approach. The key to this is combining the readyState property 53/// of the XMLHTTP object with the custom saxf_Request.callbackStatus 54/// property. 55/// 56function saxf_CatchReturn() 57{ 58 // We need to loop through the request list to see if there is a request 59 // that is pending the callback processing. We use both the xmlHTTP's 60 // readyState and the custom callbackStatus properties. 61 for (request in requestList) 62 { 63 // readyState == 4 means the request has returned completely 64 // callbackStatus == 0 means that the callback has not yet been processed 65 if (requestList[request].xmlHTTP.readyState == 4 && 66 requestList[request].callbackStatus == 0) 67 { 68 // Mark this request as having been processed 69 requestList[request].callbackStatus = 1; 70 71 // Set the request status and response text 72 requestList[request].status = requestList[request].xmlHTTP.status; 73 74 // Call the requesting component's handler, passing the responseText 75 // and status of the call 76 requestList[request].callback(requestList[request].xmlHTTP.ResponseText, 77 requestList[request].status); 78 79 // Cleanup the request 80 saxf_RemoveRequest(request); 81 82 } 83 } 84} 85 86 87/// Request Array and Request Management Functions 88 89// This array stores all currently ongoing requests 90requestList = new Array(); 91 92// adds a new request object to the list of ongoing requests 93function saxf_AddRequest(RequestToAdd) 94{ 95 requestList[requestList.length] = RequestToAdd; 96} 97 98// removes a request from the list of ongoing requests 99function saxf_RemoveRequest(LocationToRemove) 100{ 101 requestList.splice(LocationToRemove, 1); 102} 103