Elgg  Version 1.9
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 (url) {
83  options.url = url;
84  }
85 
86  return options;
87 };
88 
96 elgg.get = function(url, options) {
97  options = elgg.ajax.handleOptions(url, options);
98 
99  options.type = 'get';
100  return elgg.ajax(options);
101 };
102 
110 elgg.getJSON = function(url, options) {
111  options = elgg.ajax.handleOptions(url, options);
112 
113  options.dataType = 'json';
114  return elgg.get(options);
115 };
116 
124 elgg.post = function(url, options) {
125  options = elgg.ajax.handleOptions(url, options);
126 
127  options.type = 'post';
128  return elgg.ajax(options);
129 };
130 
178 elgg.action = function(action, options) {
179  elgg.assertTypeOf('string', action);
180 
181  // support shortcut and full URLs
182  // this will mangle URLs that aren't elgg actions.
183  // Use post, get, or ajax for those.
184  if (action.indexOf('action/') < 0) {
185  action = 'action/' + action;
186  }
187 
188  options = elgg.ajax.handleOptions(action, options);
189 
190  // This is a misuse of elgg.security.addToken() because it is not always a
191  // full query string with a ?. As such we need a special check for the tokens.
192  if (!elgg.isString(options.data) || options.data.indexOf('__elgg_ts') == -1) {
193  options.data = elgg.security.addToken(options.data);
194  }
195  options.dataType = 'json';
196 
197  //Always display system messages after actions
198  var custom_success = options.success || elgg.nullFunction;
199  options.success = function(json, two, three, four) {
200  if (json && json.system_messages) {
201  elgg.register_error(json.system_messages.error);
202  elgg.system_message(json.system_messages.success);
203  }
204 
205  custom_success(json, two, three, four);
206  };
207 
208  return elgg.post(options);
209 };
210 
227 elgg.api = function (method, options) {
228  elgg.assertTypeOf('string', method);
229 
230  var defaults = {
231  dataType: 'json',
232  data: {}
233  };
234 
235  options = elgg.ajax.handleOptions(method, options);
236  options = $.extend(defaults, options);
237 
238  options.url = 'services/api/rest/' + options.dataType + '/';
239  options.data.method = method;
240 
241  return elgg.ajax(options);
242 };
elgg message elgg state success
Definition: admin.php:243
elgg
Definition: install.js:23
$CONFIG url
The full URL where Elgg is installed.
Definition: config.php:94
elgg action
Definition: ajax.js:178