Elgg  Version 2.3
ajax.js
Go to the documentation of this file.
1 /*globals elgg, $*/
2 elgg.provide('elgg.ajax');
3 
19 elgg.ajax = function(url, options) {
20  options = elgg.ajax.handleOptions(url, options);
21 
22  options.url = elgg.normalize_url(options.url);
23  return $.ajax(options);
24 };
28 elgg.ajax.SUCCESS = 0;
29 
33 elgg.ajax.ERROR = -1;
34 
43 elgg.ajax.handleOptions = function(url, options) {
44  var data_only = true,
45  data,
46  member;
47 
48  //elgg.ajax('example/file.php', {...});
49  if (elgg.isString(url)) {
50  options = options || {};
51 
52  //elgg.ajax({...});
53  } else {
54  options = url || {};
55  url = options.url;
56  }
57 
58  //elgg.ajax('example/file.php', function() {...});
59  if (elgg.isFunction(options)) {
60  data_only = false;
61  options = {success: options};
62  }
63 
64  //elgg.ajax('example/file.php', {data:{...}});
65  if (options.data) {
66  data_only = false;
67  } else {
68  for (member in options) {
69  //elgg.ajax('example/file.php', {callback:function(){...}});
70  if (elgg.isFunction(options[member])) {
71  data_only = false;
72  }
73  }
74  }
75 
76  //elgg.ajax('example/file.php', {notdata:notfunc});
77  if (data_only) {
78  data = options;
79  options = {data: data};
80  }
81 
82  if (!elgg.isFunction(options.error)) {
83  // add a generic error handler
84  options.error = elgg.ajax.handleAjaxError;
85  }
86 
87  if (url) {
88  options.url = url;
89  }
90 
91  return options;
92 };
93 
102 elgg.ajax.handleAjaxError = function(xhr, status, error) {
103  if (!xhr.getAllResponseHeaders()) {
104  // user aborts (like refresh or navigate) do not have headers
105  return;
106  }
107 
108  elgg.register_error(elgg.echo('ajax:error'));
109 };
110 
118 elgg.get = function(url, options) {
119  options = elgg.ajax.handleOptions(url, options);
120 
121  options.type = 'get';
122  return elgg.ajax(options);
123 };
124 
132 elgg.getJSON = function(url, options) {
133  options = elgg.ajax.handleOptions(url, options);
134 
135  options.dataType = 'json';
136  return elgg.get(options);
137 };
138 
146 elgg.post = function(url, options) {
147  options = elgg.ajax.handleOptions(url, options);
148 
149  options.type = 'post';
150  return elgg.ajax(options);
151 };
152 
200 elgg.action = function(action, options) {
201  elgg.assertTypeOf('string', action);
202 
203  // support shortcut and full URLs
204  // this will mangle URLs that aren't elgg actions.
205  // Use post, get, or ajax for those.
206  if (action.indexOf('action/') < 0) {
207  action = 'action/' + action;
208  }
209 
210  options = elgg.ajax.handleOptions(action, options);
211 
212  // This is a misuse of elgg.security.addToken() because it is not always a
213  // full query string with a ?. As such we need a special check for the tokens.
214  if (!elgg.isString(options.data) || options.data.indexOf('__elgg_ts') == -1) {
215  options.data = elgg.security.addToken(options.data);
216  }
217  options.dataType = 'json';
218 
219  //Always display system messages after actions
220  var custom_success = options.success || elgg.nullFunction;
221  options.success = function(json, two, three, four) {
222  if (json && json.system_messages) {
223  elgg.register_error(json.system_messages.error);
224  elgg.system_message(json.system_messages.success);
225  }
226 
227  custom_success(json, two, three, four);
228  };
229 
230  return elgg.post(options);
231 };
232 
249 elgg.api = function (method, options) {
250  elgg.assertTypeOf('string', method);
251 
252  var defaults = {
253  dataType: 'json',
254  data: {}
255  };
256 
257  options = elgg.ajax.handleOptions(method, options);
258  options = $.extend(defaults, options);
259 
260  options.url = 'services/api/rest/' + options.dataType + '/';
261  options.data.method = method;
262 
263  return elgg.ajax(options);
264 };
elgg
Definition: install.js:23
fn editable defaults
elgg message elgg state error
Definition: admin.css.php:247
elgg message elgg state success
Definition: admin.css.php:252
if(!$site) if(!($site instanceof ElggSite)) $site url
elgg action
Definition: ajax.js:200