/*--------------------------------------------------------
* Copyright (c) 2011, The Dojo Foundation
* This software is distributed under the "Simplified BSD license",
* the text of which is available at http://www.winktoolkit.org/licence.txt
* or see the "license.txt" file for more details.
*--------------------------------------------------------*/
/**
* @fileOverview XHR utility
*
* @author Jerome GIRAUD
*/
define(['../../../_amd/core'], function()
{
var isSet = wink.isSet;
var isArray = wink.isArray;
/**
* @class The Xhr component can be used to build XmlHttpRequests and send HTTP requests. It supports both GET and POST methods.
* The Xhr constructor can optionaly take properties that will be stored (to be used within the callback methods for instance). To send a request, use the 'sendData' method
*
* @param {object} [properties] The parameters that will be stored within the request object and can be used in the callbacks methods
*
* @example
*
* var parameters =
* [
* {name: 'parameter1', value: 'test1'},
* {name: 'parameter2', value: 'test2'}
* ]
*
* xhr = new wink.Xhr();
* xhr.sendData('test_xhr.html', parameters, 'GET', {method: 'onsuccess'}, {method: 'onfailure'}, null);
*
* onsuccess = function(result)
* {
* ...
* }
*
* onfailure = function(result)
* {
* ...
* }
*
* @compatibility iOS2, iOS3, iOS4, iOS5, iOS6, Android 1.1, Android 1.5, Android 2.1, Android 2.2, Android 2.3, Android 3.0, Android 3.1, Android 4.0, Android 4.1.2, BlackBerry 6, BlackBerry 7, BB10, Bada 1.0, Windows Phone 7.5, Windows Phone 8
*
* @see Test page
* @see Online/Offline Resuming XHR
*/
wink.net.Xhr = function(properties)
{
/**
* Unique identifier
*
* @property
* @type integer
*/
this.uId = wink.getUId();
/**
* An object containing the actual XHR object and the parameters set at the instantiation time
*
* @property
* @type object
*/
this.request =
{
xhrObject: null,
params: properties
};
this._create();
};
wink.net.Xhr.prototype =
{
/**
* Send the datas
*
* @param {string} url The URL to call
* @param {array} [parameters] The parameters to add to the request URL
* @param {string} [method] Either GET or POST
* @param {object|function} [successCallback] The method to call in case of success. The 'callback' is a function or an object that must contain a 'method' and a 'scope'
* @param {object|function} [failureCallback] The method to call in case of success. The 'callback' is a function or an object that must contain a 'method' and a 'scope'
* @param {array} [headers] The HTTP headers to add to the request
* @param {integer} [timeout] The HTTP request timeout
*
* @returns {boolean} Returns true if the request was send, false otherwise
*/
sendData: function(url, parameters, method, successCallback, failureCallback, headers, timeout)
{
var r = this.request, xo = r.xhrObject, enc = encodeURIComponent, method = method.toUpperCase(), t = null;
if ( isSet(parameters) && !isArray(parameters) )
{
wink.log('[Xhr] parameters must be in an array of objects containing the parameter name and value');
return;
}
if ( isSet(timeout) )
{
t = wink.setTimeout(this, '_abort', timeout, failureCallback);
}
if (xo)
{
var p = null;
if ( isSet(parameters) )
{
var i, l = parameters.length;
for (i=0; i= 200 && status < 400) || status == 0))
{
if ( isSet(failureCallback) )
{
wink.call(failureCallback, r);
}
} else
{
if ( isSet(successCallback) )
{
wink.call(successCallback, r);
}
}
};
} else
{
return false;
}
return true;
},
/**
* Abort the current request
*
* @param {object|function} [failureCallback] The method to call in case of success. The 'callback' is a function or an object that must contain a 'method' and a 'scope'
*/
_abort: function(failureCallback)
{
this.request.xhrObject.onreadystatechange = null;
this.request.xhrObject.abort();
if ( isSet(failureCallback) )
{
wink.call(failureCallback, this.request);
}
},
/**
* Instantiate a new XMLHttpRequest
*/
_create: function()
{
var xhrInterface = window.XMLHttpRequest;
if (xhrInterface)
{
var xo;
try
{
xo = new xhrInterface();
} catch (e)
{
xo = false;
}
this.request.xhrObject = xo;
} else
{
wink.log('[Xhr] XHR not supported');
}
}
};
/**
* @class
* @see wink.net.Xhr
*/
wink.Xhr = wink.net.Xhr;
return wink.Xhr;
});