1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
/*globals elgg, $*/
elgg.provide('elgg.ajax');
/**
* @author Evan Winslow
* Provides a bunch of useful shortcut functions for making ajax calls
*/
/**
* 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);
};
/**
* @const
*/
elgg.ajax.SUCCESS = 0;
/**
* @const
*/
elgg.ajax.ERROR = -1;
/**
* Handle optional arguments and return the resulting options object
*
* @param url
* @param options
* @return {Object}
* @private
*/
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;
} else {
for (member in options) {
//elgg.ajax('example/file.php', {callback:function(){...}});
if (elgg.isFunction(options[member])) {
data_only = false;
}
}
}
//elgg.ajax('example/file.php', {notdata:notfunc});
if (data_only) {
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>
*
* 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
* <pre>
* elgg.action('friend/add', {
* data: {
* friend: some_guid
* },
* success: function(json) {
* //do something
* },
* }
* </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);
// 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) {
if (json && json.system_messages) {
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', {
* success: function(data) {
* console.log(data);
* }
* });
* </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);
};
|