Elgg  Version 1.12
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 = function(xhr, status, error) {
85  elgg.ajax.handleAjaxError(xhr, status, error);
86  };
87  }
88 
89  if (url) {
90  options.url = url;
91  }
92 
93  return options;
94 };
95 
104 elgg.ajax.handleAjaxError = function(xhr, status, error) {
105  if (!xhr.getAllResponseHeaders()) {
106  // user aborts (like refresh or navigate) do not have headers
107  return;
108  }
109 
110  elgg.register_error(elgg.echo('ajax:error'));
111 };
112 
120 elgg.get = function(url, options) {
121  options = elgg.ajax.handleOptions(url, options);
122 
123  options.type = 'get';
124  return elgg.ajax(options);
125 };
126 
134 elgg.getJSON = function(url, options) {
135  options = elgg.ajax.handleOptions(url, options);
136 
137  options.dataType = 'json';
138  return elgg.get(options);
139 };
140 
148 elgg.post = function(url, options) {
149  options = elgg.ajax.handleOptions(url, options);
150 
151  options.type = 'post';
152  return elgg.ajax(options);
153 };
154 
202 elgg.action = function(action, options) {
203  elgg.assertTypeOf('string', action);
204 
205  // support shortcut and full URLs
206  // this will mangle URLs that aren't elgg actions.
207  // Use post, get, or ajax for those.
208  if (action.indexOf('action/') < 0) {
209  action = 'action/' + action;
210  }
211 
212  options = elgg.ajax.handleOptions(action, options);
213 
214  // This is a misuse of elgg.security.addToken() because it is not always a
215  // full query string with a ?. As such we need a special check for the tokens.
216  if (!elgg.isString(options.data) || options.data.indexOf('__elgg_ts') == -1) {
217  options.data = elgg.security.addToken(options.data);
218  }
219  options.dataType = 'json';
220 
221  //Always display system messages after actions
222  var custom_success = options.success || elgg.nullFunction;
223  options.success = function(json, two, three, four) {
224  if (json && json.system_messages) {
225  elgg.register_error(json.system_messages.error);
226  elgg.system_message(json.system_messages.success);
227  }
228 
229  custom_success(json, two, three, four);
230  };
231 
232  return elgg.post(options);
233 };
234 
251 elgg.api = function (method, options) {
252  elgg.assertTypeOf('string', method);
253 
254  var defaults = {
255  dataType: 'json',
256  data: {}
257  };
258 
259  options = elgg.ajax.handleOptions(method, options);
260  options = $.extend(defaults, options);
261 
262  options.url = 'services/api/rest/' + options.dataType + '/';
263  options.data.method = method;
264 
265  return elgg.ajax(options);
266 };
elgg message elgg state success
Definition: admin.php:252
elgg
Definition: install.js:23
elgg message elgg state error
Definition: admin.php:247
if(!$site) if(!($site instanceof ElggSite)) $site url
elgg action
Definition: ajax.js:202