aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--js/lib/ajax.js72
1 files changed, 23 insertions, 49 deletions
diff --git a/js/lib/ajax.js b/js/lib/ajax.js
index 184fd0da3..95ec7948e 100644
--- a/js/lib/ajax.js
+++ b/js/lib/ajax.js
@@ -1,3 +1,4 @@
+/*globals elgg, $*/
elgg.provide('elgg.ajax');
/**
@@ -15,7 +16,7 @@ elgg.provide('elgg.ajax');
* @param {Object} options Optional. {@see jQuery#ajax}
* @return {XmlHttpRequest}
*/
-elgg.ajax = function(url, options) {
+elgg.ajax = function (url, options) {
options = elgg.ajax.handleOptions(url, options);
options.url = elgg.normalize_url(options.url);
@@ -39,9 +40,13 @@ elgg.ajax.ERROR = -1;
* @return {Object}
* @private
*/
-elgg.ajax.handleOptions = function(url, options) {
+elgg.ajax.handleOptions = function (url, options) {
+ var data_only = true,
+ data,
+ member;
+
//elgg.ajax('example/file.php', {...});
- if(typeof url == 'string') {
+ if (typeof url === 'string') {
options = options || {};
//elgg.ajax({...});
@@ -50,21 +55,19 @@ elgg.ajax.handleOptions = function(url, options) {
url = options.url;
}
- var data_only = true;
-
//elgg.ajax('example/file.php', function() {...});
- if (typeof options == 'function') {
+ if (typeof options === 'function') {
data_only = false;
options = {success: options};
}
//elgg.ajax('example/file.php', {data:{...}});
- if(options.data) {
+ if (options.data) {
data_only = false;
} else {
- for (var member in options) {
+ for (member in options) {
//elgg.ajax('example/file.php', {callback:function(){...}});
- if(typeof options[member] == 'function') {
+ if (typeof options[member] === 'function') {
data_only = false;
}
}
@@ -72,7 +75,7 @@ elgg.ajax.handleOptions = function(url, options) {
//elgg.ajax('example/file.php', {notdata:notfunc});
if (data_only) {
- var data = options;
+ data = options;
options = {data: data};
}
@@ -90,7 +93,7 @@ elgg.ajax.handleOptions = function(url, options) {
* @param {Object} options {@see jQuery#ajax}
* @return {XmlHttpRequest}
*/
-elgg.get = function(url, options) {
+elgg.get = function (url, options) {
options = elgg.ajax.handleOptions(url, options);
options.type = 'get';
@@ -104,7 +107,7 @@ elgg.get = function(url, options) {
* @param {Object} options {@see jQuery#ajax}
* @return {XmlHttpRequest}
*/
-elgg.getJSON = function(url, options) {
+elgg.getJSON = function (url, options) {
options = elgg.ajax.handleOptions(url, options);
options.dataType = 'json';
@@ -118,7 +121,7 @@ elgg.getJSON = function(url, options) {
* @param {Object} options {@see jQuery#ajax}
* @return {XmlHttpRequest}
*/
-elgg.post = function(url, options) {
+elgg.post = function (url, options) {
options = elgg.ajax.handleOptions(url, options);
options.type = 'post';
@@ -167,12 +170,8 @@ elgg.post = function(url, options) {
* @param {Object} options {@see jQuery#ajax}
* @return {XMLHttpRequest}
*/
-elgg.action = function(action, options) {
- if(!action) {
- throw new TypeError("action must be specified");
- } else if (typeof action != 'string') {
- throw new TypeError("action must be a string");
- }
+elgg.action = function (action, options) {
+ elgg.assertTypeOf('string', action);
options = elgg.ajax.handleOptions('action/' + action, options);
@@ -180,12 +179,13 @@ elgg.action = function(action, options) {
options.dataType = 'json';
//Always display system messages after actions
- var custom_success = options.success || function(){};
- options.success = function(json, two, three, four) {
+ var custom_success = options.success || function () {};
+ options.success = function (json, two, three, four) {
if (json.system_messages) {
elgg.register_error(json.system_messages.errors);
elgg.system_message(json.system_messages.messages);
}
+
custom_success(json, two, three, four);
};
@@ -208,12 +208,8 @@ elgg.action = function(action, options) {
* @param {Object} options {@see jQuery#ajax}
* @return {XmlHttpRequest}
*/
-elgg.api = function(method, options) {
- if (!method) {
- throw new TypeError("method must be specified");
- } else if (typeof method != 'string') {
- throw new TypeError("method must be a string");
- }
+elgg.api = function (method, options) {
+ elgg.assertTypeOf('string', method);
var defaults = {
dataType: 'json',
@@ -227,26 +223,4 @@ elgg.api = function(method, options) {
options.data.method = method;
return elgg.ajax(options);
-};
-
-/**
- * @param {string} selector a jQuery selector
- * @param {Function} complete A function to execute when the refresh is done
- * @return {XMLHttpRequest}
- */
-elgg.refresh = function(selector, complete) {
- $(selector).html('<div align="center" class="ajax_loader"></div>');
- return $(selector).load(location.href + ' ' + selector + ' > *', complete);
-};
-
-/**
- * @param {string} selector a jQuery selector (usually an #id)
- * @param {number} interval The refresh interval in seconds
- * @param {Function} complete A function to execute when the refresh is done
- * @return {number} The interval identifier
- */
-elgg.feed = function(selector, interval, complete) {
- return setInterval(function() {
- elgg.refresh(selector, complete);
- }, interval);
}; \ No newline at end of file