Elgg  Version 1.11
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  elgg.register_error(elgg.echo('ajax:error'));
106 };
107 
115 elgg.get = function(url, options) {
116  options = elgg.ajax.handleOptions(url, options);
117 
118  options.type = 'get';
119  return elgg.ajax(options);
120 };
121 
129 elgg.getJSON = function(url, options) {
130  options = elgg.ajax.handleOptions(url, options);
131 
132  options.dataType = 'json';
133  return elgg.get(options);
134 };
135 
143 elgg.post = function(url, options) {
144  options = elgg.ajax.handleOptions(url, options);
145 
146  options.type = 'post';
147  return elgg.ajax(options);
148 };
149 
197 elgg.action = function(action, options) {
198  elgg.assertTypeOf('string', action);
199 
200  // support shortcut and full URLs
201  // this will mangle URLs that aren't elgg actions.
202  // Use post, get, or ajax for those.
203  if (action.indexOf('action/') < 0) {
204  action = 'action/' + action;
205  }
206 
207  options = elgg.ajax.handleOptions(action, options);
208 
209  // This is a misuse of elgg.security.addToken() because it is not always a
210  // full query string with a ?. As such we need a special check for the tokens.
211  if (!elgg.isString(options.data) || options.data.indexOf('__elgg_ts') == -1) {
212  options.data = elgg.security.addToken(options.data);
213  }
214  options.dataType = 'json';
215 
216  //Always display system messages after actions
217  var custom_success = options.success || elgg.nullFunction;
218  options.success = function(json, two, three, four) {
219  if (json && json.system_messages) {
220  elgg.register_error(json.system_messages.error);
221  elgg.system_message(json.system_messages.success);
222  }
223 
224  custom_success(json, two, three, four);
225  };
226 
227  return elgg.post(options);
228 };
229 
246 elgg.api = function (method, options) {
247  elgg.assertTypeOf('string', method);
248 
249  var defaults = {
250  dataType: 'json',
251  data: {}
252  };
253 
254  options = elgg.ajax.handleOptions(method, options);
255  options = $.extend(defaults, options);
256 
257  options.url = 'services/api/rest/' + options.dataType + '/';
258  options.data.method = method;
259 
260  return elgg.ajax(options);
261 };
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:197