shhac Expert Cheater
Reputation: 0
Joined: 30 Oct 2007 Posts: 108
|
Posted: Mon May 18, 2009 12:06 pm Post subject: [JS/Ajax] What functions should be in an ajax framework? |
|
|
I'm in the process of writing a simple ajax framework, and wonder what functions should be provided by the framework for it to be 'complete'.
You can either read the code below and tell me what I've missed or just make a list of what it should have.
| Code: | /*
Basic Ajax Framework by Shhac, 18/05/2009.
Example usage:
loadData('http://google.com', function(idx){alert(XHRArray[idx].responseText);});
OR
loadData('http://google.com', function(idx){alert(XHRArray[idx].responseText);},'searchstring=blahblah');
*/
var XHRArray = [], // This will hold the actual XMLHttpObjs.
XHRState = [], // This will hold 0 = null, 1 = in use, 2 = finished.
XHRActions = []; // This will hold functions to be called when finished.
function getXMLHttpObj(){
if(window.XMLHttpRequest){
return new XMLHttpRequest();
}else if(window.ActiveXObject){
return new ActiveXObject('Microsoft.XMLHTTP');
}else{
alert('Your browser does not support XMLhttpObjects.');
return null;
}
}
function flushXHRs(){
for(i=0;i<XHRState.length;i++){
XHRState[i] = null;
}
for(i=0;i<XHRArray.length;i++){
XHRArray[i] = null;
}
for(i=0;i<XHRActions.length;i++){
XHRActions[i] = null;
}
XHRState = [];
XHRArray = [];
XHRActions = [];
}
function clearXHR(idx){
XHRState[idx] = 0;
XHRArray[idx] = null;
XHRActions[idx] = null;
}
function clearFinishedXHRs(){
if((XHRArray.length == XHRState.length) && (XHRState.length == XHRActions.length)){
for(i=0;i<XHRState.length;i++){
if(XHRState[i]==2){
XHRState[i] = 0;
XHRArray[i] = null;
XHRActions[i] = null;
}
}
}else{
alert('(XHRArray.length == XHRState.length) &&\n(XHRState.length == XHRActions.length)\nis false');
}
}
function newXHR(){
if((XHRArray.length == XHRState.length) && (XHRState.length == XHRActions.length)){
for(i=0;i<XHRState.length;i++){
if(XHRState[i]===0){
XHRState[i] = 1;
XHRArray[i] = getXMLHttpObj();
XHRActions[i] = null;
return i;
}
}
var idx = XHRState.length;
XHRState[idx] = 1;
XHRArray[idx] = getXMLHttpObj();
XHRActions[idx] = null;
return idx;
}else{
alert('(XHRArray.length == XHRState.length) &&\n(XHRState.length == XHRActions.length)\nis false');
}
}
function XHRStateChange(idx) {
if(XHRArray[idx].readyState == 4){
XHRState[idx] = 2;
if(XHRArray[idx].status == 200){
XHRActions[idx](idx);
clearXHR(idx);
return true;
}else{
alert("Error - Status Code: " + XHRArray[idx].status);
clearXHR(idx);
return false;
}
}
}
function getUrl(url){
var idx = newXHR();
XHRArray[idx].onreadystatechange = function(){XHRStateChange(idx);};
XHRArray[idx].open('GET',url,true);
XHRArray[idx].send(null);
return idx;
}
function postUrl(url, postdata){
var idx = newXHR();
XHRArray[idx].onreadystatechange = function(){XHRStateChange(idx);};
XHRArray[idx].open('POST',url,true);
XHRArray[idx].setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
XHRArray[idx].setRequestHeader('Content-length', postdata.length);
XHRArray[idx].send(postdata);
return idx;
}
function loadData(url, onCompleteFunction, postData){
if(typeof(onCompleteFunction)=='function' && onCompleteFunction.length == 1){
if(typeof(postData)=='undefined'){
XHRActions[getUrl(url)] = onCompleteFunction;
}else{
XHRActions[postUrl(url, postData)] = onCompleteFunction;
}
}else{
alert('onCompleteFunction should have one arguement, namely idx.\nIt should refer to the XMLhttpObj as XHRArray[idx].');
}
} |
|
|