Skip to content

Instantly share code, notes, and snippets.

@ststeiger
Created September 24, 2015 15:32
Show Gist options
  • Select an option

  • Save ststeiger/bb8c6565e75092e71d61 to your computer and use it in GitHub Desktop.

Select an option

Save ststeiger/bb8c6565e75092e71d61 to your computer and use it in GitHub Desktop.
EasyAjax TypeScript source
declare var REQ: any;
namespace Http
{
export abstract class RequestBase
{
public url: string;
public data: any;
private m_Async;
public Complete: boolean;
private m_SuccessCallbacks:(()=>any)[];
private m_FailureCallbacks:(()=>any)[];
private m_CompleteCallbacks:(()=>any)[];
constructor(pURL: string, pData: any) {
this.url = pURL;
this.data = pData;
this.m_Async = true;
this.m_SuccessCallbacks = [];
this.m_FailureCallbacks = [];
this.m_CompleteCallbacks = [];
}
protected failureDefault(r)
{
console.log("failure");
console.log(r);
var msg:string = "Error " + r.status + " (" + r.statusText + "): \n\n";
msg += "URL: \n" + r.responseURL +"\n\n";
msg += r.responseText;
alert(msg);
}
protected successCallback()
{
this.Complete = true;
for(var i:number = 0; i < this.m_SuccessCallbacks.length; ++i)
{
this.m_SuccessCallbacks[i].apply(this, arguments);
}
}
protected failureCallback()
{
this.Complete = true;
if(this.m_FailureCallbacks.length === 0)
this.failureDefault.apply(this, arguments);
for(var i:number = 0; i < this.m_FailureCallbacks.length; ++i)
{
this.m_FailureCallbacks[i].apply(this, arguments);
}
}
protected alwaysCallback()
{
this.Complete = true;
for(var i:number = 0; i < this.m_CompleteCallbacks.length; ++i)
{
this.m_CompleteCallbacks[i].apply(this, arguments);
}
}
public success(cb:()=> any)
{
this.m_SuccessCallbacks.push(cb);
return this;
}
public failure(cb:()=> any)
{
this.m_FailureCallbacks.push(cb);
return this;
}
public always(cb:()=> any)
{
this.m_CompleteCallbacks.push(cb);
return this;
}
public async(b:boolean)
{
this.m_Async = b;
return this;
}
public abstract send() : void;
} // End Class RequestBase
export class Get extends RequestBase {
public contentType:string="application/urlencode";
public method:string = "GET";
constructor(pURL: string, pData: any)
{
super(pURL, pData);
}
public send() : void
{
this.Complete = false;
REQ.sendRequest(this.url, this.successCallback.bind(this), this.failureCallback.bind(this), this.alwaysCallback.bind(this), this.data, this.contentType, this.method)
}
} // End Class Get
export class Json extends RequestBase {
public contentType:string="application/json; charset=UTF-8";
public method:string = "POST";
constructor(pURL: string, pData: any)
{
super(pURL, pData);
}
public send() : void
{
this.Complete = false;
REQ.sendRequest(this.url, this.successCallback.bind(this), this.failureCallback.bind(this), this.alwaysCallback.bind(this), this.data, this.contentType, this.method)
}
}
export class Post extends RequestBase {
public contentType:string="application/x-www-form-urlencoded";
public method:string = "POST";
constructor(pURL: string, pData: any)
{
super(pURL, pData);
}
public send() : void
{
this.Complete = false;
REQ.sendRequest(this.url, this.successCallback.bind(this), this.failureCallback.bind(this), this.alwaysCallback.bind(this), this.data, this.contentType, this.method)
}
} // End Class Post
export class RequestChain
{
Requests:Http.RequestBase[];
private m_OnComplete:(()=>any)[];
constructor(){
this.Requests = [];
this.m_OnComplete = [];
}
private internalComplete()
{
var allComplete:boolean = true;
for(var i:number = 0; i < this.Requests.length; ++i)
{
if(!this.Requests[i].Complete)
{
allComplete = false;
break;
}
}
if(allComplete)
{
// console.log("all complete");
for(var i:number = 0; i < this.m_OnComplete.length; ++i)
{
this.m_OnComplete[i]();
}
}
} // End Function internalComplete
public add(...args)
{
var me = this;
for(var i = 0; i < arguments.length; ++i)
{
arguments[i].always(function()
{
me.internalComplete();
}
);
this.Requests.push(arguments[i])
} // Next i
return this;
}
public addRange(req:Http.Get[])
{
var me = this;
for(var i = 0; i < req.length; ++i)
{
req[i].always(function()
{
me.internalComplete();
}
);
this.Requests.push(req[i])
} // Next i
return this;
}
public whenDone(cb:()=> any)
{
this.m_OnComplete.push(cb);
return this;
}
public process()
{
for(var i:number = 0; i < this.Requests.length; ++i)
{
this.Requests[i].send();
}
}
} // End Class RequestChain
} // End Namespace
// new Http.Json("foo", null).send()
// new Http.Get("foo", null).send()
// new Http.Post("foo", null).send()
// new Http.RequestChain().add(req1).add(req2)
// .whenDone(
// function ()
// {
// alert("foo!");
// console.log(this);
// }.bind(this)
// ).process();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment