aboutsummaryrefslogtreecommitdiff
path: root/js/lib/ajax.js
diff options
context:
space:
mode:
authorbrettp <brettp@36083f99-b078-4883-b0ff-0f9b5a30f544>2011-02-19 19:29:18 +0000
committerbrettp <brettp@36083f99-b078-4883-b0ff-0f9b5a30f544>2011-02-19 19:29:18 +0000
commit7dfe81901f078340a9d7fb0bd9edf70d66c0b723 (patch)
tree40e2d7ba4d4426b96baac1233b5cd513bc5ee25f /js/lib/ajax.js
parent0cc638aed896135a3b2954bc990c2dcd6621b26e (diff)
downloadelgg-7dfe81901f078340a9d7fb0bd9edf70d66c0b723.tar.gz
elgg-7dfe81901f078340a9d7fb0bd9edf70d66c0b723.tar.bz2
elgg.action() now supports full URLs.
git-svn-id: http://code.elgg.org/elgg/trunk@8342 36083f99-b078-4883-b0ff-0f9b5a30f544
Diffstat (limited to 'js/lib/ajax.js')
-rw-r--r--js/lib/ajax.js83
1 files changed, 45 insertions, 38 deletions
diff --git a/js/lib/ajax.js b/js/lib/ajax.js
index 3ea4377a4..36f76b2c3 100644
--- a/js/lib/ajax.js
+++ b/js/lib/ajax.js
@@ -9,16 +9,16 @@ elgg.provide('elgg.ajax');
/**
* Wrapper function for jQuery.ajax which ensures that the url being called
* is relative to the elgg site root.
- *
+ *
* You would most likely use elgg.get or elgg.post, rather than this function
- *
+ *
* @param {string} url Optionally specify the url as the first argument
* @param {Object} options Optional. {@see jQuery#ajax}
* @return {XmlHttpRequest}
*/
elgg.ajax = function(url, options) {
options = elgg.ajax.handleOptions(url, options);
-
+
options.url = elgg.normalize_url(options.url);
return $.ajax(options);
};
@@ -34,7 +34,7 @@ elgg.ajax.ERROR = -1;
/**
* Handle optional arguments and return the resulting options object
- *
+ *
* @param url
* @param options
* @return {Object}
@@ -44,23 +44,23 @@ elgg.ajax.handleOptions = function(url, options) {
var data_only = true,
data,
member;
-
+
//elgg.ajax('example/file.php', {...});
if (elgg.isString(url)) {
options = options || {};
-
+
//elgg.ajax({...});
} else {
options = url || {};
url = options.url;
}
-
+
//elgg.ajax('example/file.php', function() {...});
if (elgg.isFunction(options)) {
data_only = false;
options = {success: options};
}
-
+
//elgg.ajax('example/file.php', {data:{...}});
if (options.data) {
data_only = false;
@@ -78,75 +78,75 @@ elgg.ajax.handleOptions = function(url, options) {
data = options;
options = {data: data};
}
-
+
if (url) {
options.url = url;
}
-
+
return options;
};
/**
* Wrapper function for elgg.ajax which forces the request type to 'get.'
- *
+ *
* @param {string} url Optionally specify the url as the first argument
* @param {Object} options {@see jQuery#ajax}
* @return {XmlHttpRequest}
*/
elgg.get = function(url, options) {
options = elgg.ajax.handleOptions(url, options);
-
+
options.type = 'get';
return elgg.ajax(options);
};
/**
* Wrapper function for elgg.get which forces the dataType to 'json.'
- *
+ *
* @param {string} url Optionally specify the url as the first argument
* @param {Object} options {@see jQuery#ajax}
* @return {XmlHttpRequest}
*/
elgg.getJSON = function(url, options) {
options = elgg.ajax.handleOptions(url, options);
-
+
options.dataType = 'json';
return elgg.get(options);
};
/**
* Wrapper function for elgg.ajax which forces the request type to 'post.'
- *
+ *
* @param {string} url Optionally specify the url as the first argument
* @param {Object} options {@see jQuery#ajax}
* @return {XmlHttpRequest}
*/
elgg.post = function(url, options) {
options = elgg.ajax.handleOptions(url, options);
-
+
options.type = 'post';
return elgg.ajax(options);
};
/**
* Perform an action via ajax
- *
+ *
* @example Usage 1:
* At its simplest, only the action name is required (and anything more than the
* action name will be invalid).
* <pre>
* elgg.action('name/of/action');
* </pre>
- * Note that it will *not* love you if you specify the full url as the action
- * (i.e. elgg.yoursite.com/action/name/of/action), but why would you want to do
- * that anyway, when you can just specify the action name?
- *
+ *
+ * The action can be relative to the current site ('name/of/action') or
+ * the full URL of the action ('http://elgg.org/action/name/of/action').
+ *
* @example Usage 2:
* If you want to pass some data along with it, use the second parameter
* <pre>
* elgg.action('friend/add', { friend: some_guid });
* </pre>
- *
+ *
* @example Usage 3:
* Of course, you will have no control over what happens when the request
* completes if you do it like that, so there's also the most verbose method
@@ -161,28 +161,35 @@ elgg.post = function(url, options) {
* }
* </pre>
* You can pass any of your favorite $.ajax arguments into this second parameter.
- *
+ *
* @note If you intend to use the second field in the "verbose" way, you must
* specify a callback method or the data parameter. If you do not, elgg.action
* will think you mean to send the second parameter as data.
- *
+ *
* @note You do not have to add security tokens to this request. Elgg does that
* for you automatically.
- *
+ *
* @see jQuery.ajax
- *
+ *
* @param {String} action The action to call.
* @param {Object} options
* @return {XMLHttpRequest}
*/
elgg.action = function(action, options) {
elgg.assertTypeOf('string', action);
-
- options = elgg.ajax.handleOptions('action/' + action, options);
-
+
+ // support shortcut and full URLs
+ // this will mangle URLs that aren't elgg actions.
+ // Use post, get, or ajax for those.
+ if (action.indexOf('action/') < 0) {
+ action = 'action/' + action;
+ }
+
+ options = elgg.ajax.handleOptions(action, options);
+
options.data = elgg.security.addToken(options.data);
options.dataType = 'json';
-
+
//Always display system messages after actions
var custom_success = options.success || elgg.nullFunction;
options.success = function(json, two, three, four) {
@@ -190,16 +197,16 @@ elgg.action = function(action, options) {
elgg.register_error(json.system_messages.errors);
elgg.system_message(json.system_messages.success);
}
-
+
custom_success(json, two, three, four);
};
-
+
return elgg.post(options);
};
/**
* Make an API call
- *
+ *
* @example Usage:
* <pre>
* elgg.api('system.api.list', {
@@ -208,24 +215,24 @@ elgg.action = function(action, options) {
* }
* });
* </pre>
- *
+ *
* @param {String} method The API method to be called
* @param {Object} options {@see jQuery#ajax}
* @return {XmlHttpRequest}
*/
elgg.api = function (method, options) {
elgg.assertTypeOf('string', method);
-
+
var defaults = {
dataType: 'json',
data: {}
};
-
+
options = elgg.ajax.handleOptions(method, options);
options = $.extend(defaults, options);
-
+
options.url = 'services/api/rest/' + options.dataType + '/';
options.data.method = method;
-
+
return elgg.ajax(options);
};