Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[JS/Ajax] What functions should be in an ajax framework?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
shhac
Expert Cheater
Reputation: 0

Joined: 30 Oct 2007
Posts: 108

PostPosted: Mon May 18, 2009 12:06 pm    Post subject: [JS/Ajax] What functions should be in an ajax framework? Reply with quote

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].');
  }
}
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites