Class: request

Suit. request

new Suit.request()

Reference to the container of XmlHttpRequest features.

Classes

binary
blob
document
json

Methods

staticSuit.request.create(p_method, p_url, p_callback, p_response, p_data, p_headers){XmlHttpRequest}

Create and execute a XmlHttpRequest and invokes the callback with the needed feedback of the process.

Name Type Description
p_method String

Request method (GET, POST,...)

p_url String

URL

p_callback RequestCallback | String

Reference to the callback function to handle this request or notification string that all controllers will receive.

p_response String

Type of the response text,arraybuffer,blob,document,json - See More .

p_data ArrayBuffer | FormElement | String | Object | Blob nullable

Data to be sent.

p_headers Object nullable

Object containing custom headers.

Returns:
Type Description
XmlHttpRequest
  • Reference to the created XmlHttpRequest object.
Examples

//================ DOWNLOAD ================
//Will create a POST request expecting a json that don't send any data and don't define custom headers.
var xhr = Suit.request.create("POST","http://webservice.com/",function(p_data,p_progress,p_event){
if(p_progress>=1.0){ //100%
console.log(data); // { some: json: { data: "yay" } } }
}
else {
bar.style.width = (p_progress * 100.0)+"px"; //progress feedback on some element.
}
},"json",null,null);

var c = {
on: function (n) {
switch (n.path) {
case "some.load@progress": console.log(n.data.progress) break; //progress number
case "some.load@load": console.log(n.data.data); break; //Uint8Array
case "some.load@error": console.log(n.data.event); break; //error event
}
}
};

Suit.controller.add(c);

Suit.request.create("GET","http://webservice.com","some.load","arraybuffer"); //Starts the loading using notification string as callback.

//================ UPLOAD ================
Suit.request.create("POST","http://webservice.com/",function(p_data,p_progress,p_event) {
if(p_progress<0.0) { //Upload progress goes from -1.0 to 0.0
var p = p_progress+1.0; //convert to [0;1] range
bar.style.width = Math.floor(p*100.0)+"px";
}

if(p_progress>=0.0) { //Download progress goes from 0.0 to 1.0
var p = p_progress; //use as it is
bar.style.width = Math.floor(p*100.0)+"px";
}

if(p_progress>=1.0) { //Upload and Download finished.
console.log(p_data);
}

}, { data: "content", index: 10 }); //Object data will pass thru Json.stringify().

//Check the docs for the allowed data types that can be sent.

staticSuit.request.get(p_url, p_callback, p_data, p_headers){XmlHttpRequest}

Creates a GET request expecting 'text' response.

Name Type Description
p_url String

URL

p_callback RequestCallback

Reference to the callback function to handle this request.

p_data ArrayBuffer | FormElement | String | Object | Blob nullable

Data to be sent.

p_headers Object nullable

Object containing custom headers.

See:
Returns:
Type Description
XmlHttpRequest
  • Reference to the created XmlHttpRequest object.
Example

//Using 'create'
Suit.request.create("GET","http://webservice.com",function(d,p,e){...},"text");
//Using the shortcut
Suit.request.get("http://webservice.com",function(d,p,e){...});

staticSuit.request.post(p_url, p_callback, p_data, p_headers){XmlHttpRequest}

Creates a POST request expecting 'text' response.

Name Type Description
p_url String

URL

p_callback RequestCallback

Reference to the callback function to handle this request.

p_data ArrayBuffer | FormElement | String | Object | Blob nullable

Data to be sent.

p_headers Object nullable

Object containing custom headers.

See:
Returns:
Type Description
XmlHttpRequest
  • Reference to the created XmlHttpRequest object.
Example

//Using 'create'
Suit.request.create("POST","http://webservice.com",function(d,p,e){...},"text");
//Using the shortcut
Suit.request.post("http://webservice.com",function(d,p,e){...});

comments powered by Disqus