2014-09-05 11:57:18 -04:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<meta charset="utf-8">
|
|
|
|
<title>JSDoc: Source: modules/ajax.js</title>
|
|
|
|
|
|
|
|
<script src="scripts/prettify/prettify.js"> </script>
|
|
|
|
<script src="scripts/prettify/lang-css.js"> </script>
|
|
|
|
<!--[if lt IE 9]>
|
|
|
|
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
|
|
|
|
<![endif]-->
|
|
|
|
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
|
|
|
|
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
|
|
|
|
</head>
|
|
|
|
|
|
|
|
<body>
|
|
|
|
|
|
|
|
<div id="main">
|
|
|
|
|
|
|
|
<h1 class="page-title">Source: modules/ajax.js</h1>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<section>
|
|
|
|
<article>
|
|
|
|
<pre class="prettyprint source linenums"><code>/**
|
|
|
|
* Ajax
|
|
|
|
*
|
|
|
|
* Module for making ajax requests
|
|
|
|
*/
|
|
|
|
(function (undefined){
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var ajax = {
|
2014-09-17 09:31:12 -04:00
|
|
|
_do: function (url, data, success_callback, error_callback, type)
|
2014-09-05 11:57:18 -04:00
|
|
|
{
|
|
|
|
var type,
|
|
|
|
request = new XMLHttpRequest();
|
|
|
|
|
|
|
|
if (success_callback === undefined)
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
success_callback = function (){};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type === "GET")
|
|
|
|
{
|
|
|
|
url += (url.match(/\?/))
|
|
|
|
? this._serialize(data)
|
|
|
|
: "?" + this._serialize(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
request.open(type, url);
|
|
|
|
|
|
|
|
request.onreadystatechange = function ()
|
|
|
|
{
|
|
|
|
if (request.readyState === 4)
|
|
|
|
{
|
|
|
|
if (request.status === 200)
|
|
|
|
{
|
|
|
|
success_callback.call(request.responseText, request.responseText);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (error_callback !== undefined)
|
|
|
|
{
|
|
|
|
error_callback.call(request.status, request.status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-09-17 09:31:12 -04:00
|
|
|
if (type !== "GET")
|
2014-09-05 11:57:18 -04:00
|
|
|
{
|
|
|
|
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
|
|
|
request.send(this._serialize(data));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
request.send(null);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Url encoding for non-get requests
|
|
|
|
*
|
|
|
|
* @param data
|
|
|
|
* @returns {string}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_serialize: function (data)
|
|
|
|
{
|
|
|
|
var name,
|
|
|
|
value,
|
|
|
|
pairs = [];
|
|
|
|
|
|
|
|
for (name in data)
|
|
|
|
{
|
|
|
|
if ( ! data.hasOwnProperty(name) || $_.type(data[name]) === "function")
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = data[name].toString();
|
|
|
|
|
|
|
|
name = encodeURIComponent(name);
|
|
|
|
value = encodeURIComponent(value);
|
|
|
|
|
|
|
|
pairs.push(name + "=" + value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pairs.join("&");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a GET type ajax request
|
|
|
|
*
|
2014-09-17 09:31:12 -04:00
|
|
|
* @function get
|
2014-09-05 11:57:18 -04:00
|
|
|
* @memberOf $_
|
|
|
|
* @param {string} url - The url to retrieve
|
|
|
|
* @param {Object} data - get parameters to send
|
|
|
|
* @param {function} success_callback - callback called on success
|
|
|
|
* @param {function} [error_callback] - callback called if there is an error
|
|
|
|
*/
|
|
|
|
$_.ext('get', function (url, data, success_callback, error_callback){
|
2014-09-17 09:31:12 -04:00
|
|
|
ajax._do(url, data, success_callback, error_callback, 'GET');
|
2014-09-05 11:57:18 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a POST type ajax request
|
|
|
|
*
|
2014-09-17 09:31:12 -04:00
|
|
|
* @function post
|
2014-09-05 11:57:18 -04:00
|
|
|
* @memberOf $_
|
|
|
|
* @param {string} url - The url to post to
|
|
|
|
* @param {Object} data - post parameters to send
|
|
|
|
* @param {function} success_callback - callback called on success
|
|
|
|
* @param {function} [error_callback] - callback called if there is an error
|
|
|
|
*/
|
|
|
|
$_.ext('post', function (url, data, success_callback, error_callback){
|
2014-09-17 09:31:12 -04:00
|
|
|
ajax._do(url, data, success_callback, error_callback, 'POST');
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a PUT type ajax request
|
|
|
|
*
|
|
|
|
* @function put
|
|
|
|
* @memberOf $_
|
|
|
|
* @param {string} url - The url to post to
|
|
|
|
* @param {Object} data - PUT parameters to send
|
|
|
|
* @param {function} success_callback - callback called on success
|
|
|
|
* @param {function} [error_callback] - callback called if there is an error
|
|
|
|
*/
|
|
|
|
$_.ext('put', function (url, data, success_callback, error_callback){
|
|
|
|
ajax._do(url, data, success_callback, error_callback, 'PUT');
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a DELETE type ajax request
|
|
|
|
*
|
|
|
|
* @function delete
|
|
|
|
* @memberOf $_
|
|
|
|
* @param {string} url - The url to post to
|
|
|
|
* @param {Object} data - delete parameters to send
|
|
|
|
* @param {function} success_callback - callback called on success
|
|
|
|
* @param {function} [error_callback] - callback called if there is an error
|
|
|
|
*/
|
|
|
|
$_.ext('delete', function (url, data, success_callback, error_callback){
|
|
|
|
ajax._do(url, data, success_callback, error_callback, 'DELETE');
|
2014-09-05 11:57:18 -04:00
|
|
|
});
|
|
|
|
}());
|
|
|
|
</code></pre>
|
|
|
|
</article>
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<nav>
|
2014-09-12 16:14:12 -04:00
|
|
|
<h2><a href="index.html">Index</a></h2><h3>Namespaces</h3><ul><li><a href="$_.html">$_</a></li><li><a href="$_.dom.html">dom</a></li><li><a href="$_.event.html">event</a></li><li><a href="$_.store.html">store</a></li></ul>
|
2014-09-05 11:57:18 -04:00
|
|
|
</nav>
|
|
|
|
|
|
|
|
<br clear="both">
|
|
|
|
|
|
|
|
<footer>
|
2014-09-17 09:31:12 -04:00
|
|
|
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-alpha9</a> on Tue Sep 16 2014 16:57:40 GMT-0400 (EDT)
|
2014-09-05 11:57:18 -04:00
|
|
|
</footer>
|
|
|
|
|
|
|
|
<script> prettyPrint(); </script>
|
|
|
|
<script src="scripts/linenumber.js"> </script>
|
|
|
|
</body>
|
|
|
|
</html>
|