2011-10-24 12:26:00 -04:00
|
|
|
/**
|
|
|
|
* Ajax
|
|
|
|
*
|
2011-11-01 18:56:27 -04:00
|
|
|
* Module for making ajax requests
|
2011-10-24 12:26:00 -04:00
|
|
|
*/
|
|
|
|
(function (){
|
|
|
|
|
2011-11-01 20:14:38 -04:00
|
|
|
"use strict";
|
2012-03-29 11:48:11 -04:00
|
|
|
|
2011-11-02 19:12:58 -04:00
|
|
|
// Don't bother even defining the object if the XMLHttpRequest isn't available
|
|
|
|
if(typeof window.XMLHttpRequest === "undefined")
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2011-11-01 20:14:38 -04:00
|
|
|
|
2011-10-24 12:26:00 -04:00
|
|
|
var ajax = {
|
|
|
|
_do: function (url, data, callback, isPost)
|
|
|
|
{
|
2012-03-29 11:48:11 -04:00
|
|
|
var type,
|
2011-11-02 19:12:58 -04:00
|
|
|
request = new XMLHttpRequest();
|
2012-03-29 11:48:11 -04:00
|
|
|
|
2011-10-24 12:26:00 -04:00
|
|
|
if (typeof callback === "undefined")
|
|
|
|
{
|
2011-11-01 18:56:27 -04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2011-10-24 12:26:00 -04:00
|
|
|
callback = function (){};
|
|
|
|
}
|
|
|
|
|
2011-11-02 19:12:58 -04:00
|
|
|
type = (isPost) ? "POST" : "GET";
|
2011-10-24 12:26:00 -04:00
|
|
|
|
|
|
|
url += (type === "GET") ? "?"+this._serialize(data) : '';
|
2012-03-29 11:48:11 -04:00
|
|
|
|
2011-10-24 12:26:00 -04:00
|
|
|
request.open(type, url);
|
|
|
|
|
|
|
|
request.onreadystatechange = function ()
|
|
|
|
{
|
|
|
|
if (request.readyState === 4)
|
|
|
|
{
|
|
|
|
callback(request.responseText);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (type === "POST")
|
|
|
|
{
|
|
|
|
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
|
|
|
request.send(this._serialize(data));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
request.send(null);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_serialize: function (data)
|
|
|
|
{
|
2011-11-02 19:12:58 -04:00
|
|
|
var name,
|
|
|
|
value,
|
|
|
|
pairs = [];
|
2011-10-24 12:26:00 -04:00
|
|
|
|
2011-11-02 19:12:58 -04:00
|
|
|
for (name in data)
|
2011-10-24 12:26:00 -04:00
|
|
|
{
|
|
|
|
if (!data.hasOwnProperty(name))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (typeof data[name] === "function")
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-11-02 19:12:58 -04:00
|
|
|
value = data[name].toString();
|
2011-10-24 12:26:00 -04:00
|
|
|
|
|
|
|
name = encodeURIComponent(name);
|
|
|
|
value = encodeURIComponent(value);
|
|
|
|
|
|
|
|
pairs.push(name + "=" + value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pairs.join("&");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-11-01 18:56:27 -04:00
|
|
|
/**
|
|
|
|
* Sends a GET type ajax request
|
2012-03-29 11:48:11 -04:00
|
|
|
*
|
2011-11-01 18:56:27 -04:00
|
|
|
* @name get
|
|
|
|
* @function
|
|
|
|
* @memberOf $_
|
|
|
|
* @param string url
|
|
|
|
* @param object data
|
|
|
|
* @param function callback
|
|
|
|
*/
|
2011-10-24 12:26:00 -04:00
|
|
|
$_.ext('get', function (url, data, callback){
|
|
|
|
ajax._do(url, data, callback, false);
|
|
|
|
});
|
2012-03-29 11:48:11 -04:00
|
|
|
|
2011-11-01 18:56:27 -04:00
|
|
|
/**
|
|
|
|
* Sends a POST type ajax request
|
2012-03-29 11:48:11 -04:00
|
|
|
*
|
2011-11-01 18:56:27 -04:00
|
|
|
* @name post
|
|
|
|
* @function
|
|
|
|
* @memberOf $_
|
|
|
|
* @param string url
|
|
|
|
* @param object data
|
|
|
|
* @param function callback
|
|
|
|
*/
|
2011-10-24 12:26:00 -04:00
|
|
|
$_.ext('post', function (url, data, callback){
|
|
|
|
ajax._do(url, data, callback, true);
|
|
|
|
});
|
2012-03-29 11:48:11 -04:00
|
|
|
|
2012-04-03 14:39:26 -04:00
|
|
|
/**
|
|
|
|
* Watches for server-sent events, or falls back to ajax polling
|
|
|
|
* - defaults to 30 second intervals
|
|
|
|
*
|
|
|
|
* @name sse
|
|
|
|
* @function
|
|
|
|
* @memberOf $_
|
|
|
|
* @param string url
|
|
|
|
* @param function callback
|
|
|
|
* @param [int] 30000
|
|
|
|
*/
|
|
|
|
$_.ext('sse', function(url, callback, poll_rate){
|
|
|
|
|
|
|
|
var source;
|
|
|
|
|
|
|
|
//Check for server-sent event support
|
|
|
|
if (typeof EventSource !== 'undefined')
|
|
|
|
{
|
|
|
|
source = new EventSource(url);
|
|
|
|
|
|
|
|
// Apply the callback
|
2012-04-03 16:39:20 -04:00
|
|
|
source.onmessage = function(event){
|
|
|
|
callback(event.data);
|
|
|
|
};
|
2012-04-03 14:39:26 -04:00
|
|
|
}
|
|
|
|
else // Ajax polling fallback
|
|
|
|
{
|
|
|
|
poll_rate = poll_rate || 30000;
|
2012-04-03 16:39:20 -04:00
|
|
|
|
|
|
|
setInterval($_.get, poll_rate, url, {}, function(res){
|
2012-04-05 16:32:05 -04:00
|
|
|
res.trim().replace(/data:/gim, '');
|
2012-04-03 16:39:20 -04:00
|
|
|
res.replace(/^event|id|retry?:(.*)$/gim, '');
|
|
|
|
callback(res);
|
|
|
|
});
|
2012-04-03 14:39:26 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-10-24 12:26:00 -04:00
|
|
|
}());
|