+/*globals elgg, $*/\r
elgg.provide('elgg.ajax');\r
\r
/**\r
* @param {Object} options Optional. {@see jQuery#ajax}\r
* @return {XmlHttpRequest}\r
*/\r
-elgg.ajax = function(url, options) {\r
+elgg.ajax = function (url, options) {\r
options = elgg.ajax.handleOptions(url, options);\r
\r
options.url = elgg.normalize_url(options.url);\r
* @return {Object}\r
* @private\r
*/\r
-elgg.ajax.handleOptions = function(url, options) {\r
+elgg.ajax.handleOptions = function (url, options) {\r
+ var data_only = true,\r
+ data,\r
+ member;\r
+ \r
//elgg.ajax('example/file.php', {...});\r
- if(typeof url == 'string') {\r
+ if (typeof url === 'string') {\r
options = options || {};\r
\r
//elgg.ajax({...});\r
url = options.url;\r
}\r
\r
- var data_only = true;\r
-\r
//elgg.ajax('example/file.php', function() {...});\r
- if (typeof options == 'function') {\r
+ if (typeof options === 'function') {\r
data_only = false;\r
options = {success: options};\r
}\r
\r
//elgg.ajax('example/file.php', {data:{...}});\r
- if(options.data) {\r
+ if (options.data) {\r
data_only = false;\r
} else {\r
- for (var member in options) {\r
+ for (member in options) {\r
//elgg.ajax('example/file.php', {callback:function(){...}});\r
- if(typeof options[member] == 'function') {\r
+ if (typeof options[member] === 'function') {\r
data_only = false;\r
}\r
}\r
\r
//elgg.ajax('example/file.php', {notdata:notfunc});\r
if (data_only) {\r
- var data = options;\r
+ data = options;\r
options = {data: data};\r
}\r
\r
* @param {Object} options {@see jQuery#ajax}\r
* @return {XmlHttpRequest}\r
*/\r
-elgg.get = function(url, options) {\r
+elgg.get = function (url, options) {\r
options = elgg.ajax.handleOptions(url, options);\r
\r
options.type = 'get';\r
* @param {Object} options {@see jQuery#ajax}\r
* @return {XmlHttpRequest}\r
*/\r
-elgg.getJSON = function(url, options) {\r
+elgg.getJSON = function (url, options) {\r
options = elgg.ajax.handleOptions(url, options);\r
\r
options.dataType = 'json';\r
* @param {Object} options {@see jQuery#ajax}\r
* @return {XmlHttpRequest}\r
*/\r
-elgg.post = function(url, options) {\r
+elgg.post = function (url, options) {\r
options = elgg.ajax.handleOptions(url, options);\r
\r
options.type = 'post';\r
* @param {Object} options {@see jQuery#ajax}\r
* @return {XMLHttpRequest}\r
*/\r
-elgg.action = function(action, options) {\r
- if(!action) {\r
- throw new TypeError("action must be specified");\r
- } else if (typeof action != 'string') {\r
- throw new TypeError("action must be a string");\r
- }\r
+elgg.action = function (action, options) {\r
+ elgg.assertTypeOf('string', action);\r
\r
options = elgg.ajax.handleOptions('action/' + action, options);\r
\r
options.dataType = 'json';\r
\r
//Always display system messages after actions\r
- var custom_success = options.success || function(){};\r
- options.success = function(json, two, three, four) {\r
+ var custom_success = options.success || function () {};\r
+ options.success = function (json, two, three, four) {\r
if (json.system_messages) {\r
elgg.register_error(json.system_messages.errors);\r
elgg.system_message(json.system_messages.messages);\r
}\r
+ \r
custom_success(json, two, three, four);\r
};\r
\r
* @param {Object} options {@see jQuery#ajax}\r
* @return {XmlHttpRequest}\r
*/\r
-elgg.api = function(method, options) {\r
- if (!method) {\r
- throw new TypeError("method must be specified");\r
- } else if (typeof method != 'string') {\r
- throw new TypeError("method must be a string");\r
- }\r
+elgg.api = function (method, options) {\r
+ elgg.assertTypeOf('string', method);\r
\r
var defaults = {\r
dataType: 'json',\r
options.data.method = method;\r
\r
return elgg.ajax(options);\r
-};\r
-\r
-/**\r
- * @param {string} selector a jQuery selector\r
- * @param {Function} complete A function to execute when the refresh is done\r
- * @return {XMLHttpRequest}\r
- */\r
-elgg.refresh = function(selector, complete) {\r
- $(selector).html('<div align="center" class="ajax_loader"></div>');\r
- return $(selector).load(location.href + ' ' + selector + ' > *', complete);\r
-};\r
-\r
-/**\r
- * @param {string} selector a jQuery selector (usually an #id)\r
- * @param {number} interval The refresh interval in seconds\r
- * @param {Function} complete A function to execute when the refresh is done\r
- * @return {number} The interval identifier\r
- */\r
-elgg.feed = function(selector, interval, complete) {\r
- return setInterval(function() {\r
- elgg.refresh(selector, complete);\r
- }, interval);\r
};
\ No newline at end of file