1 |
efrain |
1 |
YUI.add('io-base', function (Y, NAME) {
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
Base IO functionality. Provides basic XHR transport support.
|
|
|
5 |
|
|
|
6 |
@module io
|
|
|
7 |
@submodule io-base
|
|
|
8 |
@for IO
|
|
|
9 |
**/
|
|
|
10 |
|
|
|
11 |
var // List of events that comprise the IO event lifecycle.
|
|
|
12 |
EVENTS = ['start', 'complete', 'end', 'success', 'failure', 'progress'],
|
|
|
13 |
|
|
|
14 |
// Whitelist of used XHR response object properties.
|
|
|
15 |
XHR_PROPS = ['status', 'statusText', 'responseText', 'responseXML'],
|
|
|
16 |
|
|
|
17 |
win = Y.config.win,
|
|
|
18 |
uid = 0;
|
|
|
19 |
|
|
|
20 |
/**
|
|
|
21 |
The IO class is a utility that brokers HTTP requests through a simplified
|
|
|
22 |
interface. Specifically, it allows JavaScript to make HTTP requests to
|
|
|
23 |
a resource without a page reload. The underlying transport for making
|
|
|
24 |
same-domain requests is the XMLHttpRequest object. IO can also use
|
|
|
25 |
Flash, if specified as a transport, for cross-domain requests.
|
|
|
26 |
|
|
|
27 |
@class IO
|
|
|
28 |
@constructor
|
|
|
29 |
@param {Object} config Object of EventTarget's publish method configurations
|
|
|
30 |
used to configure IO's events.
|
|
|
31 |
|
|
|
32 |
IO can be called statically using {{#crossLink "YUI/io:method"}}YUI.io{{/crossLink}}.
|
|
|
33 |
**/
|
|
|
34 |
function IO (config) {
|
|
|
35 |
var io = this;
|
|
|
36 |
|
|
|
37 |
io._uid = 'io:' + uid++;
|
|
|
38 |
io._init(config);
|
|
|
39 |
Y.io._map[io._uid] = io;
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
IO.prototype = {
|
|
|
43 |
//--------------------------------------
|
|
|
44 |
// Properties
|
|
|
45 |
//--------------------------------------
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
* A counter that increments for each transaction.
|
|
|
49 |
*
|
|
|
50 |
* @property _id
|
|
|
51 |
* @private
|
|
|
52 |
* @type {Number}
|
|
|
53 |
*/
|
|
|
54 |
_id: 0,
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* Object of IO HTTP headers sent with each transaction.
|
|
|
58 |
*
|
|
|
59 |
* @property _headers
|
|
|
60 |
* @private
|
|
|
61 |
* @type {Object}
|
|
|
62 |
*/
|
|
|
63 |
_headers: {
|
|
|
64 |
'X-Requested-With' : 'XMLHttpRequest'
|
|
|
65 |
},
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* Object that stores timeout values for any transaction with a defined
|
|
|
69 |
* "timeout" configuration property.
|
|
|
70 |
*
|
|
|
71 |
* @property _timeout
|
|
|
72 |
* @private
|
|
|
73 |
* @type {Object}
|
|
|
74 |
*/
|
|
|
75 |
_timeout: {},
|
|
|
76 |
|
|
|
77 |
//--------------------------------------
|
|
|
78 |
// Methods
|
|
|
79 |
//--------------------------------------
|
|
|
80 |
|
|
|
81 |
_init: function(config) {
|
|
|
82 |
var io = this, i, len;
|
|
|
83 |
|
|
|
84 |
io.cfg = config || {};
|
|
|
85 |
|
|
|
86 |
Y.augment(io, Y.EventTarget);
|
|
|
87 |
for (i = 0, len = EVENTS.length; i < len; ++i) {
|
|
|
88 |
// Publish IO global events with configurations, if any.
|
|
|
89 |
// IO global events are set to broadcast by default.
|
|
|
90 |
// These events use the "io:" namespace.
|
|
|
91 |
io.publish('io:' + EVENTS[i], Y.merge({ broadcast: 1 }, config));
|
|
|
92 |
// Publish IO transaction events with configurations, if
|
|
|
93 |
// any. These events use the "io-trn:" namespace.
|
|
|
94 |
io.publish('io-trn:' + EVENTS[i], config);
|
|
|
95 |
}
|
|
|
96 |
},
|
|
|
97 |
|
|
|
98 |
/**
|
|
|
99 |
* Method that creates a unique transaction object for each request.
|
|
|
100 |
*
|
|
|
101 |
* @method _create
|
|
|
102 |
* @private
|
|
|
103 |
* @param {Object} cfg Configuration object subset to determine if
|
|
|
104 |
* the transaction is an XDR or file upload,
|
|
|
105 |
* requiring an alternate transport.
|
|
|
106 |
* @param {Number} id Transaction id
|
|
|
107 |
* @return {Object} The transaction object
|
|
|
108 |
*/
|
|
|
109 |
_create: function(config, id) {
|
|
|
110 |
var io = this,
|
|
|
111 |
transaction = {
|
|
|
112 |
id : Y.Lang.isNumber(id) ? id : io._id++,
|
|
|
113 |
uid: io._uid
|
|
|
114 |
},
|
|
|
115 |
alt = config.xdr ? config.xdr.use : null,
|
|
|
116 |
form = config.form && config.form.upload ? 'iframe' : null,
|
|
|
117 |
use;
|
|
|
118 |
|
|
|
119 |
if (alt === 'native') {
|
|
|
120 |
// Non-IE and IE >= 10 can use XHR level 2 and not rely on an
|
|
|
121 |
// external transport.
|
|
|
122 |
alt = Y.UA.ie && !SUPPORTS_CORS ? 'xdr' : null;
|
|
|
123 |
|
|
|
124 |
// Prevent "pre-flight" OPTIONS request by removing the
|
|
|
125 |
// `X-Requested-With` HTTP header from CORS requests. This header
|
|
|
126 |
// can be added back on a per-request basis, if desired.
|
|
|
127 |
io.setHeader('X-Requested-With');
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
use = alt || form;
|
|
|
131 |
transaction = use ? Y.merge(Y.IO.customTransport(use), transaction) :
|
|
|
132 |
Y.merge(Y.IO.defaultTransport(), transaction);
|
|
|
133 |
|
|
|
134 |
if (transaction.notify) {
|
|
|
135 |
config.notify = function (e, t, c) { io.notify(e, t, c); };
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
if (!use) {
|
|
|
139 |
if (win && win.FormData && config.data instanceof win.FormData) {
|
|
|
140 |
transaction.c.upload.onprogress = function (e) {
|
|
|
141 |
io.progress(transaction, e, config);
|
|
|
142 |
};
|
|
|
143 |
transaction.c.onload = function (e) {
|
|
|
144 |
io.load(transaction, e, config);
|
|
|
145 |
};
|
|
|
146 |
transaction.c.onerror = function (e) {
|
|
|
147 |
io.error(transaction, e, config);
|
|
|
148 |
};
|
|
|
149 |
transaction.upload = true;
|
|
|
150 |
}
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
return transaction;
|
|
|
154 |
},
|
|
|
155 |
|
|
|
156 |
_destroy: function(transaction) {
|
|
|
157 |
if (win && !transaction.notify && !transaction.xdr) {
|
|
|
158 |
if (XHR && !transaction.upload) {
|
|
|
159 |
transaction.c.onreadystatechange = null;
|
|
|
160 |
} else if (transaction.upload) {
|
|
|
161 |
transaction.c.upload.onprogress = null;
|
|
|
162 |
transaction.c.onload = null;
|
|
|
163 |
transaction.c.onerror = null;
|
|
|
164 |
} else if (Y.UA.ie && !transaction.e) {
|
|
|
165 |
// IE, when using XMLHttpRequest as an ActiveX Object, will throw
|
|
|
166 |
// a "Type Mismatch" error if the event handler is set to "null".
|
|
|
167 |
transaction.c.abort();
|
|
|
168 |
}
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
transaction = transaction.c = null;
|
|
|
172 |
},
|
|
|
173 |
|
|
|
174 |
/**
|
|
|
175 |
* Method for creating and firing events.
|
|
|
176 |
*
|
|
|
177 |
* @method _evt
|
|
|
178 |
* @private
|
|
|
179 |
* @param {String} eventName Event to be published.
|
|
|
180 |
* @param {Object} transaction Transaction object.
|
|
|
181 |
* @param {Object} config Configuration data subset for event subscription.
|
|
|
182 |
*/
|
|
|
183 |
_evt: function(eventName, transaction, config) {
|
|
|
184 |
var io = this, params,
|
|
|
185 |
args = config['arguments'],
|
|
|
186 |
emitFacade = io.cfg.emitFacade,
|
|
|
187 |
globalEvent = "io:" + eventName,
|
|
|
188 |
trnEvent = "io-trn:" + eventName;
|
|
|
189 |
|
|
|
190 |
// Workaround for #2532107
|
|
|
191 |
this.detach(trnEvent);
|
|
|
192 |
|
|
|
193 |
if (transaction.e) {
|
|
|
194 |
transaction.c = { status: 0, statusText: transaction.e };
|
|
|
195 |
}
|
|
|
196 |
|
|
|
197 |
// Fire event with parameters or an Event Facade.
|
|
|
198 |
params = [ emitFacade ?
|
|
|
199 |
{
|
|
|
200 |
id: transaction.id,
|
|
|
201 |
data: transaction.c,
|
|
|
202 |
cfg: config,
|
|
|
203 |
'arguments': args
|
|
|
204 |
} :
|
|
|
205 |
transaction.id
|
|
|
206 |
];
|
|
|
207 |
|
|
|
208 |
if (!emitFacade) {
|
|
|
209 |
if (eventName === EVENTS[0] || eventName === EVENTS[2]) {
|
|
|
210 |
if (args) {
|
|
|
211 |
params.push(args);
|
|
|
212 |
}
|
|
|
213 |
} else {
|
|
|
214 |
if (transaction.evt) {
|
|
|
215 |
params.push(transaction.evt);
|
|
|
216 |
} else {
|
|
|
217 |
params.push(transaction.c);
|
|
|
218 |
}
|
|
|
219 |
if (args) {
|
|
|
220 |
params.push(args);
|
|
|
221 |
}
|
|
|
222 |
}
|
|
|
223 |
}
|
|
|
224 |
|
|
|
225 |
params.unshift(globalEvent);
|
|
|
226 |
// Fire global events.
|
|
|
227 |
io.fire.apply(io, params);
|
|
|
228 |
// Fire transaction events, if receivers are defined.
|
|
|
229 |
if (config.on) {
|
|
|
230 |
params[0] = trnEvent;
|
|
|
231 |
io.once(trnEvent, config.on[eventName], config.context || Y);
|
|
|
232 |
io.fire.apply(io, params);
|
|
|
233 |
}
|
|
|
234 |
},
|
|
|
235 |
|
|
|
236 |
/**
|
|
|
237 |
* Fires event "io:start" and creates, fires a transaction-specific
|
|
|
238 |
* start event, if `config.on.start` is defined.
|
|
|
239 |
*
|
|
|
240 |
* @method start
|
|
|
241 |
* @param {Object} transaction Transaction object.
|
|
|
242 |
* @param {Object} config Configuration object for the transaction.
|
|
|
243 |
*/
|
|
|
244 |
start: function(transaction, config) {
|
|
|
245 |
/**
|
|
|
246 |
* Signals the start of an IO request.
|
|
|
247 |
* @event io:start
|
|
|
248 |
*/
|
|
|
249 |
this._evt(EVENTS[0], transaction, config);
|
|
|
250 |
},
|
|
|
251 |
|
|
|
252 |
/**
|
|
|
253 |
* Fires event "io:complete" and creates, fires a
|
|
|
254 |
* transaction-specific "complete" event, if config.on.complete is
|
|
|
255 |
* defined.
|
|
|
256 |
*
|
|
|
257 |
* @method complete
|
|
|
258 |
* @param {Object} transaction Transaction object.
|
|
|
259 |
* @param {Object} config Configuration object for the transaction.
|
|
|
260 |
*/
|
|
|
261 |
complete: function(transaction, config) {
|
|
|
262 |
/**
|
|
|
263 |
* Signals the completion of the request-response phase of a
|
|
|
264 |
* transaction. Response status and data are accessible, if
|
|
|
265 |
* available, in this event.
|
|
|
266 |
* @event io:complete
|
|
|
267 |
*/
|
|
|
268 |
this._evt(EVENTS[1], transaction, config);
|
|
|
269 |
},
|
|
|
270 |
|
|
|
271 |
/**
|
|
|
272 |
* Fires event "io:end" and creates, fires a transaction-specific "end"
|
|
|
273 |
* event, if config.on.end is defined.
|
|
|
274 |
*
|
|
|
275 |
* @method end
|
|
|
276 |
* @param {Object} transaction Transaction object.
|
|
|
277 |
* @param {Object} config Configuration object for the transaction.
|
|
|
278 |
*/
|
|
|
279 |
end: function(transaction, config) {
|
|
|
280 |
/**
|
|
|
281 |
* Signals the end of the transaction lifecycle.
|
|
|
282 |
* @event io:end
|
|
|
283 |
*/
|
|
|
284 |
this._evt(EVENTS[2], transaction, config);
|
|
|
285 |
this._destroy(transaction);
|
|
|
286 |
},
|
|
|
287 |
|
|
|
288 |
/**
|
|
|
289 |
* Fires event "io:success" and creates, fires a transaction-specific
|
|
|
290 |
* "success" event, if config.on.success is defined.
|
|
|
291 |
*
|
|
|
292 |
* @method success
|
|
|
293 |
* @param {Object} transaction Transaction object.
|
|
|
294 |
* @param {Object} config Configuration object for the transaction.
|
|
|
295 |
*/
|
|
|
296 |
success: function(transaction, config) {
|
|
|
297 |
/**
|
|
|
298 |
* Signals an HTTP response with status in the 2xx range.
|
|
|
299 |
* Fires after io:complete.
|
|
|
300 |
* @event io:success
|
|
|
301 |
*/
|
|
|
302 |
this._evt(EVENTS[3], transaction, config);
|
|
|
303 |
this.end(transaction, config);
|
|
|
304 |
},
|
|
|
305 |
|
|
|
306 |
/**
|
|
|
307 |
* Fires event "io:failure" and creates, fires a transaction-specific
|
|
|
308 |
* "failure" event, if config.on.failure is defined.
|
|
|
309 |
*
|
|
|
310 |
* @method failure
|
|
|
311 |
* @param {Object} transaction Transaction object.
|
|
|
312 |
* @param {Object} config Configuration object for the transaction.
|
|
|
313 |
*/
|
|
|
314 |
failure: function(transaction, config) {
|
|
|
315 |
/**
|
|
|
316 |
* Signals an HTTP response with status outside of the 2xx range.
|
|
|
317 |
* Fires after io:complete.
|
|
|
318 |
* @event io:failure
|
|
|
319 |
*/
|
|
|
320 |
this._evt(EVENTS[4], transaction, config);
|
|
|
321 |
this.end(transaction, config);
|
|
|
322 |
},
|
|
|
323 |
|
|
|
324 |
/**
|
|
|
325 |
* Fires event "io:progress" and creates, fires a transaction-specific
|
|
|
326 |
* "progress" event -- for XMLHttpRequest file upload -- if
|
|
|
327 |
* config.on.progress is defined.
|
|
|
328 |
*
|
|
|
329 |
* @method progress
|
|
|
330 |
* @param {Object} transaction Transaction object.
|
|
|
331 |
* @param {Object} progress event.
|
|
|
332 |
* @param {Object} config Configuration object for the transaction.
|
|
|
333 |
*/
|
|
|
334 |
progress: function(transaction, e, config) {
|
|
|
335 |
/**
|
|
|
336 |
* Signals the interactive state during a file upload transaction.
|
|
|
337 |
* This event fires after io:start and before io:complete.
|
|
|
338 |
* @event io:progress
|
|
|
339 |
*/
|
|
|
340 |
transaction.evt = e;
|
|
|
341 |
this._evt(EVENTS[5], transaction, config);
|
|
|
342 |
},
|
|
|
343 |
|
|
|
344 |
/**
|
|
|
345 |
* Fires event "io:complete" and creates, fires a transaction-specific
|
|
|
346 |
* "complete" event -- for XMLHttpRequest file upload -- if
|
|
|
347 |
* config.on.complete is defined.
|
|
|
348 |
*
|
|
|
349 |
* @method load
|
|
|
350 |
* @param {Object} transaction Transaction object.
|
|
|
351 |
* @param {Object} load event.
|
|
|
352 |
* @param {Object} config Configuration object for the transaction.
|
|
|
353 |
*/
|
|
|
354 |
load: function (transaction, e, config) {
|
|
|
355 |
transaction.evt = e.target;
|
|
|
356 |
this._evt(EVENTS[1], transaction, config);
|
|
|
357 |
},
|
|
|
358 |
|
|
|
359 |
/**
|
|
|
360 |
* Fires event "io:failure" and creates, fires a transaction-specific
|
|
|
361 |
* "failure" event -- for XMLHttpRequest file upload -- if
|
|
|
362 |
* config.on.failure is defined.
|
|
|
363 |
*
|
|
|
364 |
* @method error
|
|
|
365 |
* @param {Object} transaction Transaction object.
|
|
|
366 |
* @param {Object} error event.
|
|
|
367 |
* @param {Object} config Configuration object for the transaction.
|
|
|
368 |
*/
|
|
|
369 |
error: function (transaction, e, config) {
|
|
|
370 |
transaction.evt = e;
|
|
|
371 |
this._evt(EVENTS[4], transaction, config);
|
|
|
372 |
},
|
|
|
373 |
|
|
|
374 |
/**
|
|
|
375 |
* Retry an XDR transaction, using the Flash tranport, if the native
|
|
|
376 |
* transport fails.
|
|
|
377 |
*
|
|
|
378 |
* @method _retry
|
|
|
379 |
* @private
|
|
|
380 |
* @param {Object} transaction Transaction object.
|
|
|
381 |
* @param {String} uri Qualified path to transaction resource.
|
|
|
382 |
* @param {Object} config Configuration object for the transaction.
|
|
|
383 |
*/
|
|
|
384 |
_retry: function(transaction, uri, config) {
|
|
|
385 |
this._destroy(transaction);
|
|
|
386 |
config.xdr.use = 'flash';
|
|
|
387 |
return this.send(uri, config, transaction.id);
|
|
|
388 |
},
|
|
|
389 |
|
|
|
390 |
/**
|
|
|
391 |
* Method that concatenates string data for HTTP GET transactions.
|
|
|
392 |
*
|
|
|
393 |
* @method _concat
|
|
|
394 |
* @private
|
|
|
395 |
* @param {String} uri URI or root data.
|
|
|
396 |
* @param {String} data Data to be concatenated onto URI.
|
|
|
397 |
* @return {String}
|
|
|
398 |
*/
|
|
|
399 |
_concat: function(uri, data) {
|
|
|
400 |
uri += (uri.indexOf('?') === -1 ? '?' : '&') + data;
|
|
|
401 |
return uri;
|
|
|
402 |
},
|
|
|
403 |
|
|
|
404 |
/**
|
|
|
405 |
* Stores default client headers for all transactions. If a label is
|
|
|
406 |
* passed with no value argument, the header will be deleted.
|
|
|
407 |
*
|
|
|
408 |
* @method setHeader
|
|
|
409 |
* @param {String} name HTTP header
|
|
|
410 |
* @param {String} value HTTP header value
|
|
|
411 |
*/
|
|
|
412 |
setHeader: function(name, value) {
|
|
|
413 |
if (value) {
|
|
|
414 |
this._headers[name] = value;
|
|
|
415 |
} else {
|
|
|
416 |
delete this._headers[name];
|
|
|
417 |
}
|
|
|
418 |
},
|
|
|
419 |
|
|
|
420 |
/**
|
|
|
421 |
* Method that sets all HTTP headers to be sent in a transaction.
|
|
|
422 |
*
|
|
|
423 |
* @method _setHeaders
|
|
|
424 |
* @private
|
|
|
425 |
* @param {Object} transaction - XHR instance for the specific transaction.
|
|
|
426 |
* @param {Object} headers - HTTP headers for the specific transaction, as
|
|
|
427 |
* defined in the configuration object passed to YUI.io().
|
|
|
428 |
*/
|
|
|
429 |
_setHeaders: function(transaction, headers) {
|
|
|
430 |
headers = Y.merge(this._headers, headers);
|
|
|
431 |
Y.Object.each(headers, function(value, name) {
|
|
|
432 |
if (value !== 'disable') {
|
|
|
433 |
transaction.setRequestHeader(name, headers[name]);
|
|
|
434 |
}
|
|
|
435 |
});
|
|
|
436 |
},
|
|
|
437 |
|
|
|
438 |
/**
|
|
|
439 |
* Starts timeout count if the configuration object has a defined
|
|
|
440 |
* timeout property.
|
|
|
441 |
*
|
|
|
442 |
* @method _startTimeout
|
|
|
443 |
* @private
|
|
|
444 |
* @param {Object} transaction Transaction object generated by _create().
|
|
|
445 |
* @param {Object} timeout Timeout in milliseconds.
|
|
|
446 |
*/
|
|
|
447 |
_startTimeout: function(transaction, timeout) {
|
|
|
448 |
var io = this;
|
|
|
449 |
|
|
|
450 |
io._timeout[transaction.id] = setTimeout(function() {
|
|
|
451 |
io._abort(transaction, 'timeout');
|
|
|
452 |
}, timeout);
|
|
|
453 |
},
|
|
|
454 |
|
|
|
455 |
/**
|
|
|
456 |
* Clears the timeout interval started by _startTimeout().
|
|
|
457 |
*
|
|
|
458 |
* @method _clearTimeout
|
|
|
459 |
* @private
|
|
|
460 |
* @param {Number} id - Transaction id.
|
|
|
461 |
*/
|
|
|
462 |
_clearTimeout: function(id) {
|
|
|
463 |
clearTimeout(this._timeout[id]);
|
|
|
464 |
delete this._timeout[id];
|
|
|
465 |
},
|
|
|
466 |
|
|
|
467 |
/**
|
|
|
468 |
* Method that determines if a transaction response qualifies as success
|
|
|
469 |
* or failure, based on the response HTTP status code, and fires the
|
|
|
470 |
* appropriate success or failure events.
|
|
|
471 |
*
|
|
|
472 |
* @method _result
|
|
|
473 |
* @private
|
|
|
474 |
* @static
|
|
|
475 |
* @param {Object} transaction Transaction object generated by _create().
|
|
|
476 |
* @param {Object} config Configuration object passed to io().
|
|
|
477 |
*/
|
|
|
478 |
_result: function(transaction, config) {
|
|
|
479 |
var status;
|
|
|
480 |
// Firefox will throw an exception if attempting to access
|
|
|
481 |
// an XHR object's status property, after a request is aborted.
|
|
|
482 |
try {
|
|
|
483 |
status = transaction.c.status;
|
|
|
484 |
} catch(e) {
|
|
|
485 |
status = 0;
|
|
|
486 |
}
|
|
|
487 |
|
|
|
488 |
// IE reports HTTP 204 as HTTP 1223.
|
|
|
489 |
if (status >= 200 && status < 300 || status === 304 || status === 1223) {
|
|
|
490 |
this.success(transaction, config);
|
|
|
491 |
} else {
|
|
|
492 |
this.failure(transaction, config);
|
|
|
493 |
}
|
|
|
494 |
},
|
|
|
495 |
|
|
|
496 |
/**
|
|
|
497 |
* Event handler bound to onreadystatechange.
|
|
|
498 |
*
|
|
|
499 |
* @method _rS
|
|
|
500 |
* @private
|
|
|
501 |
* @param {Object} transaction Transaction object generated by _create().
|
|
|
502 |
* @param {Object} config Configuration object passed to YUI.io().
|
|
|
503 |
*/
|
|
|
504 |
_rS: function(transaction, config) {
|
|
|
505 |
var io = this;
|
|
|
506 |
|
|
|
507 |
if (transaction.c.readyState === 4) {
|
|
|
508 |
if (config.timeout) {
|
|
|
509 |
io._clearTimeout(transaction.id);
|
|
|
510 |
}
|
|
|
511 |
|
|
|
512 |
// Yield in the event of request timeout or abort.
|
|
|
513 |
setTimeout(function() {
|
|
|
514 |
io.complete(transaction, config);
|
|
|
515 |
io._result(transaction, config);
|
|
|
516 |
}, 0);
|
|
|
517 |
}
|
|
|
518 |
},
|
|
|
519 |
|
|
|
520 |
/**
|
|
|
521 |
* Terminates a transaction due to an explicit abort or timeout.
|
|
|
522 |
*
|
|
|
523 |
* @method _abort
|
|
|
524 |
* @private
|
|
|
525 |
* @param {Object} transaction Transaction object generated by _create().
|
|
|
526 |
* @param {String} type Identifies timed out or aborted transaction.
|
|
|
527 |
*/
|
|
|
528 |
_abort: function(transaction, type) {
|
|
|
529 |
if (transaction && transaction.c) {
|
|
|
530 |
transaction.e = type;
|
|
|
531 |
transaction.c.abort();
|
|
|
532 |
}
|
|
|
533 |
},
|
|
|
534 |
|
|
|
535 |
/**
|
|
|
536 |
* Requests a transaction. `send()` is implemented as `Y.io()`. Each
|
|
|
537 |
* transaction may include a configuration object. Its properties are:
|
|
|
538 |
*
|
|
|
539 |
* <dl>
|
|
|
540 |
* <dt>method</dt>
|
|
|
541 |
* <dd>HTTP method verb (e.g., GET or POST). If this property is not
|
|
|
542 |
* not defined, the default value will be GET.</dd>
|
|
|
543 |
*
|
|
|
544 |
* <dt>data</dt>
|
|
|
545 |
* <dd>This is the name-value string that will be sent as the
|
|
|
546 |
* transaction data. If the request is HTTP GET, the data become
|
|
|
547 |
* part of querystring. If HTTP POST, the data are sent in the
|
|
|
548 |
* message body.</dd>
|
|
|
549 |
*
|
|
|
550 |
* <dt>xdr</dt>
|
|
|
551 |
* <dd>Defines the transport to be used for cross-domain requests.
|
|
|
552 |
* By setting this property, the transaction will use the specified
|
|
|
553 |
* transport instead of XMLHttpRequest. The properties of the
|
|
|
554 |
* transport object are:
|
|
|
555 |
* <dl>
|
|
|
556 |
* <dt>use</dt>
|
|
|
557 |
* <dd>The transport to be used: 'flash' or 'native'</dd>
|
|
|
558 |
* <dt>dataType</dt>
|
|
|
559 |
* <dd>Set the value to 'XML' if that is the expected response
|
|
|
560 |
* content type.</dd>
|
|
|
561 |
* <dt>credentials</dt>
|
|
|
562 |
* <dd>Set the value to 'true' to set XHR.withCredentials property to true.</dd>
|
|
|
563 |
* </dl></dd>
|
|
|
564 |
*
|
|
|
565 |
* <dt>form</dt>
|
|
|
566 |
* <dd>Form serialization configuration object. Its properties are:
|
|
|
567 |
* <dl>
|
|
|
568 |
* <dt>id</dt>
|
|
|
569 |
* <dd>Node object or id of HTML form</dd>
|
|
|
570 |
* <dt>useDisabled</dt>
|
|
|
571 |
* <dd>`true` to also serialize disabled form field values
|
|
|
572 |
* (defaults to `false`)</dd>
|
|
|
573 |
* </dl></dd>
|
|
|
574 |
*
|
|
|
575 |
* <dt>on</dt>
|
|
|
576 |
* <dd>Assigns transaction event subscriptions. Available events are:
|
|
|
577 |
* <dl>
|
|
|
578 |
* <dt>start</dt>
|
|
|
579 |
* <dd>Fires when a request is sent to a resource.</dd>
|
|
|
580 |
* <dt>complete</dt>
|
|
|
581 |
* <dd>Fires when the transaction is complete.</dd>
|
|
|
582 |
* <dt>success</dt>
|
|
|
583 |
* <dd>Fires when the HTTP response status is within the 2xx
|
|
|
584 |
* range.</dd>
|
|
|
585 |
* <dt>failure</dt>
|
|
|
586 |
* <dd>Fires when the HTTP response status is outside the 2xx
|
|
|
587 |
* range, if an exception occurs, if the transation is aborted,
|
|
|
588 |
* or if the transaction exceeds a configured `timeout`.</dd>
|
|
|
589 |
* <dt>end</dt>
|
|
|
590 |
* <dd>Fires at the conclusion of the transaction
|
|
|
591 |
* lifecycle, after `success` or `failure`.</dd>
|
|
|
592 |
* </dl>
|
|
|
593 |
*
|
|
|
594 |
* <p>Callback functions for `start` and `end` receive the id of the
|
|
|
595 |
* transaction as a first argument. For `complete`, `success`, and
|
|
|
596 |
* `failure`, callbacks receive the id and the response object
|
|
|
597 |
* (usually the XMLHttpRequest instance). If the `arguments`
|
|
|
598 |
* property was included in the configuration object passed to
|
|
|
599 |
* `Y.io()`, the configured data will be passed to all callbacks as
|
|
|
600 |
* the last argument.</p>
|
|
|
601 |
* </dd>
|
|
|
602 |
*
|
|
|
603 |
* <dt>sync</dt>
|
|
|
604 |
* <dd>Pass `true` to make a same-domain transaction synchronous.
|
|
|
605 |
* <strong>CAVEAT</strong>: This will negatively impact the user
|
|
|
606 |
* experience. Have a <em>very</em> good reason if you intend to use
|
|
|
607 |
* this.</dd>
|
|
|
608 |
*
|
|
|
609 |
* <dt>context</dt>
|
|
|
610 |
* <dd>The "`this'" object for all configured event handlers. If a
|
|
|
611 |
* specific context is needed for individual callbacks, bind the
|
|
|
612 |
* callback to a context using `Y.bind()`.</dd>
|
|
|
613 |
*
|
|
|
614 |
* <dt>headers</dt>
|
|
|
615 |
* <dd>Object map of transaction headers to send to the server. The
|
|
|
616 |
* object keys are the header names and the values are the header
|
|
|
617 |
* values.</dd>
|
|
|
618 |
*
|
|
|
619 |
* <dt>username</dt>
|
|
|
620 |
* <dd>Username to use in a HTTP authentication.</dd>
|
|
|
621 |
*
|
|
|
622 |
* <dt>password</dt>
|
|
|
623 |
* <dd>Password to use in a HTTP authentication.</dd>
|
|
|
624 |
*
|
|
|
625 |
* <dt>timeout</dt>
|
|
|
626 |
* <dd>Millisecond threshold for the transaction before being
|
|
|
627 |
* automatically aborted.</dd>
|
|
|
628 |
*
|
|
|
629 |
* <dt>arguments</dt>
|
|
|
630 |
* <dd>User-defined data passed to all registered event handlers.
|
|
|
631 |
* This value is available as the second argument in the "start" and
|
|
|
632 |
* "end" event handlers. It is the third argument in the "complete",
|
|
|
633 |
* "success", and "failure" event handlers. <strong>Be sure to quote
|
|
|
634 |
* this property name in the transaction configuration as
|
|
|
635 |
* "arguments" is a reserved word in JavaScript</strong> (e.g.
|
|
|
636 |
* `Y.io({ ..., "arguments": stuff })`).</dd>
|
|
|
637 |
* </dl>
|
|
|
638 |
*
|
|
|
639 |
* @method send
|
|
|
640 |
* @public
|
|
|
641 |
* @param {String} uri Qualified path to transaction resource.
|
|
|
642 |
* @param {Object} config Configuration object for the transaction.
|
|
|
643 |
* @param {Number} id Transaction id, if already set.
|
|
|
644 |
* @return {Object} An object containing:
|
|
|
645 |
* <dl>
|
|
|
646 |
* <dt>`id`</dt>
|
|
|
647 |
* <dd>
|
|
|
648 |
* The transaction ID for this request.
|
|
|
649 |
* </dd>
|
|
|
650 |
* <dt>`abort`</dt>
|
|
|
651 |
* <dd>
|
|
|
652 |
* A function to abort the current transaction.
|
|
|
653 |
* </dd>
|
|
|
654 |
* <dt>`isInProgress`</dt>
|
|
|
655 |
* <dd>
|
|
|
656 |
* A helper to determine whether the current transaction is in progress.
|
|
|
657 |
* </dd>
|
|
|
658 |
* <dt>`io`</dt>
|
|
|
659 |
* <dd>
|
|
|
660 |
* A reference to the IO object for this transaction.
|
|
|
661 |
* </dd>
|
|
|
662 |
* </dl>
|
|
|
663 |
*/
|
|
|
664 |
send: function(uri, config, id) {
|
|
|
665 |
var transaction, method, i, len, sync, data,
|
|
|
666 |
io = this,
|
|
|
667 |
u = uri,
|
|
|
668 |
response = {};
|
|
|
669 |
|
|
|
670 |
config = config ? Y.Object(config) : {};
|
|
|
671 |
transaction = io._create(config, id);
|
|
|
672 |
method = config.method ? config.method.toUpperCase() : 'GET';
|
|
|
673 |
sync = config.sync;
|
|
|
674 |
data = config.data;
|
|
|
675 |
|
|
|
676 |
// Serialize a map object into a key-value string using
|
|
|
677 |
// querystring-stringify-simple.
|
|
|
678 |
if ((Y.Lang.isObject(data) && !data.nodeType) && !transaction.upload) {
|
|
|
679 |
if (Y.QueryString && Y.QueryString.stringify) {
|
|
|
680 |
config.data = data = Y.QueryString.stringify(data);
|
|
|
681 |
} else {
|
|
|
682 |
}
|
|
|
683 |
}
|
|
|
684 |
|
|
|
685 |
if (config.form) {
|
|
|
686 |
if (config.form.upload) {
|
|
|
687 |
// This is a file upload transaction, calling
|
|
|
688 |
// upload() in io-upload-iframe.
|
|
|
689 |
return io.upload(transaction, uri, config);
|
|
|
690 |
} else {
|
|
|
691 |
// Serialize HTML form data into a key-value string.
|
|
|
692 |
data = io._serialize(config.form, data);
|
|
|
693 |
}
|
|
|
694 |
}
|
|
|
695 |
|
|
|
696 |
// Convert falsy values to an empty string. This way IE can't be
|
|
|
697 |
// rediculous and translate `undefined` to "undefined".
|
|
|
698 |
data || (data = '');
|
|
|
699 |
|
|
|
700 |
if (data) {
|
|
|
701 |
switch (method) {
|
|
|
702 |
case 'GET':
|
|
|
703 |
case 'HEAD':
|
|
|
704 |
case 'DELETE':
|
|
|
705 |
u = io._concat(u, data);
|
|
|
706 |
data = '';
|
|
|
707 |
break;
|
|
|
708 |
case 'POST':
|
|
|
709 |
case 'PUT':
|
|
|
710 |
// If Content-Type is defined in the configuration object, or
|
|
|
711 |
// or as a default header, it will be used instead of
|
|
|
712 |
// 'application/x-www-form-urlencoded; charset=UTF-8'
|
|
|
713 |
config.headers = Y.merge({
|
|
|
714 |
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
|
|
|
715 |
}, config.headers);
|
|
|
716 |
break;
|
|
|
717 |
}
|
|
|
718 |
}
|
|
|
719 |
|
|
|
720 |
if (transaction.xdr) {
|
|
|
721 |
// Route data to io-xdr module for flash and XDomainRequest.
|
|
|
722 |
return io.xdr(u, transaction, config);
|
|
|
723 |
}
|
|
|
724 |
else if (transaction.notify) {
|
|
|
725 |
// Route data to custom transport
|
|
|
726 |
return transaction.c.send(transaction, uri, config);
|
|
|
727 |
}
|
|
|
728 |
|
|
|
729 |
if (!sync && !transaction.upload) {
|
|
|
730 |
transaction.c.onreadystatechange = function() {
|
|
|
731 |
io._rS(transaction, config);
|
|
|
732 |
};
|
|
|
733 |
}
|
|
|
734 |
|
|
|
735 |
try {
|
|
|
736 |
// Determine if request is to be set as
|
|
|
737 |
// synchronous or asynchronous.
|
|
|
738 |
transaction.c.open(method, u, !sync, config.username || null, config.password || null);
|
|
|
739 |
io._setHeaders(transaction.c, config.headers || {});
|
|
|
740 |
io.start(transaction, config);
|
|
|
741 |
|
|
|
742 |
// Will work only in browsers that implement the
|
|
|
743 |
// Cross-Origin Resource Sharing draft.
|
|
|
744 |
if (config.xdr && config.xdr.credentials && SUPPORTS_CORS) {
|
|
|
745 |
transaction.c.withCredentials = true;
|
|
|
746 |
}
|
|
|
747 |
|
|
|
748 |
// Using "null" with HTTP POST will result in a request
|
|
|
749 |
// with no Content-Length header defined.
|
|
|
750 |
transaction.c.send(data);
|
|
|
751 |
|
|
|
752 |
if (sync) {
|
|
|
753 |
// Create a response object for synchronous transactions,
|
|
|
754 |
// mixing id and arguments properties with the xhr
|
|
|
755 |
// properties whitelist.
|
|
|
756 |
for (i = 0, len = XHR_PROPS.length; i < len; ++i) {
|
|
|
757 |
response[XHR_PROPS[i]] = transaction.c[XHR_PROPS[i]];
|
|
|
758 |
}
|
|
|
759 |
|
|
|
760 |
response.getAllResponseHeaders = function() {
|
|
|
761 |
return transaction.c.getAllResponseHeaders();
|
|
|
762 |
};
|
|
|
763 |
|
|
|
764 |
response.getResponseHeader = function(name) {
|
|
|
765 |
return transaction.c.getResponseHeader(name);
|
|
|
766 |
};
|
|
|
767 |
|
|
|
768 |
io.complete(transaction, config);
|
|
|
769 |
io._result(transaction, config);
|
|
|
770 |
|
|
|
771 |
return response;
|
|
|
772 |
}
|
|
|
773 |
} catch(e) {
|
|
|
774 |
if (transaction.xdr) {
|
|
|
775 |
// This exception is usually thrown by browsers
|
|
|
776 |
// that do not support XMLHttpRequest Level 2.
|
|
|
777 |
// Retry the request with the XDR transport set
|
|
|
778 |
// to 'flash'. If the Flash transport is not
|
|
|
779 |
// initialized or available, the transaction
|
|
|
780 |
// will resolve to a transport error.
|
|
|
781 |
return io._retry(transaction, uri, config);
|
|
|
782 |
} else {
|
|
|
783 |
io.complete(transaction, config);
|
|
|
784 |
io._result(transaction, config);
|
|
|
785 |
}
|
|
|
786 |
}
|
|
|
787 |
|
|
|
788 |
// If config.timeout is defined, and the request is standard XHR,
|
|
|
789 |
// initialize timeout polling.
|
|
|
790 |
if (config.timeout) {
|
|
|
791 |
io._startTimeout(transaction, config.timeout);
|
|
|
792 |
}
|
|
|
793 |
|
|
|
794 |
return {
|
|
|
795 |
id: transaction.id,
|
|
|
796 |
abort: function() {
|
|
|
797 |
return transaction.c ? io._abort(transaction, 'abort') : false;
|
|
|
798 |
},
|
|
|
799 |
isInProgress: function() {
|
|
|
800 |
return transaction.c ? (transaction.c.readyState % 4) : false;
|
|
|
801 |
},
|
|
|
802 |
io: io
|
|
|
803 |
};
|
|
|
804 |
}
|
|
|
805 |
};
|
|
|
806 |
|
|
|
807 |
/**
|
|
|
808 |
Method for initiating an ajax call. The first argument is the url end
|
|
|
809 |
point for the call. The second argument is an object to configure the
|
|
|
810 |
transaction and attach event subscriptions. The configuration object
|
|
|
811 |
supports the following properties:
|
|
|
812 |
|
|
|
813 |
<dl>
|
|
|
814 |
<dt>method</dt>
|
|
|
815 |
<dd>HTTP method verb (e.g., GET or POST). If this property is not
|
|
|
816 |
not defined, the default value will be GET.</dd>
|
|
|
817 |
|
|
|
818 |
<dt>data</dt>
|
|
|
819 |
<dd>This is the name-value string that will be sent as the
|
|
|
820 |
transaction data. If the request is HTTP GET, the data become
|
|
|
821 |
part of querystring. If HTTP POST, the data are sent in the
|
|
|
822 |
message body.</dd>
|
|
|
823 |
|
|
|
824 |
<dt>xdr</dt>
|
|
|
825 |
<dd>Defines the transport to be used for cross-domain requests.
|
|
|
826 |
By setting this property, the transaction will use the specified
|
|
|
827 |
transport instead of XMLHttpRequest. The properties of the
|
|
|
828 |
transport object are:
|
|
|
829 |
<dl>
|
|
|
830 |
<dt>use</dt>
|
|
|
831 |
<dd>The transport to be used: 'flash' or 'native'</dd>
|
|
|
832 |
<dt>dataType</dt>
|
|
|
833 |
<dd>Set the value to 'XML' if that is the expected response
|
|
|
834 |
content type.</dd>
|
|
|
835 |
</dl></dd>
|
|
|
836 |
|
|
|
837 |
<dt>form</dt>
|
|
|
838 |
<dd>Form serialization configuration object. Its properties are:
|
|
|
839 |
<dl>
|
|
|
840 |
<dt>id</dt>
|
|
|
841 |
<dd>Node object or id of HTML form</dd>
|
|
|
842 |
<dt>useDisabled</dt>
|
|
|
843 |
<dd>`true` to also serialize disabled form field values
|
|
|
844 |
(defaults to `false`)</dd>
|
|
|
845 |
</dl></dd>
|
|
|
846 |
|
|
|
847 |
<dt>on</dt>
|
|
|
848 |
<dd>Assigns transaction event subscriptions. Available events are:
|
|
|
849 |
<dl>
|
|
|
850 |
<dt>start</dt>
|
|
|
851 |
<dd>Fires when a request is sent to a resource.</dd>
|
|
|
852 |
<dt>complete</dt>
|
|
|
853 |
<dd>Fires when the transaction is complete.</dd>
|
|
|
854 |
<dt>success</dt>
|
|
|
855 |
<dd>Fires when the HTTP response status is within the 2xx
|
|
|
856 |
range.</dd>
|
|
|
857 |
<dt>failure</dt>
|
|
|
858 |
<dd>Fires when the HTTP response status is outside the 2xx
|
|
|
859 |
range, if an exception occurs, if the transation is aborted,
|
|
|
860 |
or if the transaction exceeds a configured `timeout`.</dd>
|
|
|
861 |
<dt>end</dt>
|
|
|
862 |
<dd>Fires at the conclusion of the transaction
|
|
|
863 |
lifecycle, after `success` or `failure`.</dd>
|
|
|
864 |
</dl>
|
|
|
865 |
|
|
|
866 |
<p>Callback functions for `start` and `end` receive the id of the
|
|
|
867 |
transaction as a first argument. For `complete`, `success`, and
|
|
|
868 |
`failure`, callbacks receive the id and the response object
|
|
|
869 |
(usually the XMLHttpRequest instance). If the `arguments`
|
|
|
870 |
property was included in the configuration object passed to
|
|
|
871 |
`Y.io()`, the configured data will be passed to all callbacks as
|
|
|
872 |
the last argument.</p>
|
|
|
873 |
</dd>
|
|
|
874 |
|
|
|
875 |
<dt>sync</dt>
|
|
|
876 |
<dd>Pass `true` to make a same-domain transaction synchronous.
|
|
|
877 |
<strong>CAVEAT</strong>: This will negatively impact the user
|
|
|
878 |
experience. Have a <em>very</em> good reason if you intend to use
|
|
|
879 |
this.</dd>
|
|
|
880 |
|
|
|
881 |
<dt>context</dt>
|
|
|
882 |
<dd>The "`this'" object for all configured event handlers. If a
|
|
|
883 |
specific context is needed for individual callbacks, bind the
|
|
|
884 |
callback to a context using `Y.bind()`.</dd>
|
|
|
885 |
|
|
|
886 |
<dt>headers</dt>
|
|
|
887 |
<dd>Object map of transaction headers to send to the server. The
|
|
|
888 |
object keys are the header names and the values are the header
|
|
|
889 |
values.</dd>
|
|
|
890 |
|
|
|
891 |
<dt>timeout</dt>
|
|
|
892 |
<dd>Millisecond threshold for the transaction before being
|
|
|
893 |
automatically aborted.</dd>
|
|
|
894 |
|
|
|
895 |
<dt>arguments</dt>
|
|
|
896 |
<dd>User-defined data passed to all registered event handlers.
|
|
|
897 |
This value is available as the second argument in the "start" and
|
|
|
898 |
"end" event handlers. It is the third argument in the "complete",
|
|
|
899 |
"success", and "failure" event handlers. <strong>Be sure to quote
|
|
|
900 |
this property name in the transaction configuration as
|
|
|
901 |
"arguments" is a reserved word in JavaScript</strong> (e.g.
|
|
|
902 |
`Y.io({ ..., "arguments": stuff })`).</dd>
|
|
|
903 |
</dl>
|
|
|
904 |
|
|
|
905 |
@method io
|
|
|
906 |
@static
|
|
|
907 |
@param {String} url qualified path to transaction resource.
|
|
|
908 |
@param {Object} config configuration object for the transaction.
|
|
|
909 |
@return {Object} An object containing:
|
|
|
910 |
<dl>
|
|
|
911 |
<dt>`id`</dt>
|
|
|
912 |
<dd>
|
|
|
913 |
The transaction ID for this request.
|
|
|
914 |
</dd>
|
|
|
915 |
<dt>`abort`</dt>
|
|
|
916 |
<dd>
|
|
|
917 |
A function to abort the current transaction.
|
|
|
918 |
</dd>
|
|
|
919 |
<dt>`isInProgress`</dt>
|
|
|
920 |
<dd>
|
|
|
921 |
A helper to determine whether the current transaction is in progress.
|
|
|
922 |
</dd>
|
|
|
923 |
<dt>`io`</dt>
|
|
|
924 |
<dd>
|
|
|
925 |
A reference to the IO object for this transaction.
|
|
|
926 |
</dd>
|
|
|
927 |
</dl>
|
|
|
928 |
|
|
|
929 |
@for YUI
|
|
|
930 |
**/
|
|
|
931 |
Y.io = function(url, config) {
|
|
|
932 |
// Calling IO through the static interface will use and reuse
|
|
|
933 |
// an instance of IO.
|
|
|
934 |
var transaction = Y.io._map['io:0'] || new IO();
|
|
|
935 |
return transaction.send.apply(transaction, [url, config]);
|
|
|
936 |
};
|
|
|
937 |
|
|
|
938 |
/**
|
|
|
939 |
Method for setting and deleting IO HTTP headers to be sent with every
|
|
|
940 |
request.
|
|
|
941 |
|
|
|
942 |
Hosted as a property on the `io` function (e.g. `Y.io.header`).
|
|
|
943 |
|
|
|
944 |
@method header
|
|
|
945 |
@param {String} name HTTP header
|
|
|
946 |
@param {String} value HTTP header value
|
|
|
947 |
@static
|
|
|
948 |
**/
|
|
|
949 |
Y.io.header = function(name, value) {
|
|
|
950 |
// Calling IO through the static interface will use and reuse
|
|
|
951 |
// an instance of IO.
|
|
|
952 |
var transaction = Y.io._map['io:0'] || new IO();
|
|
|
953 |
transaction.setHeader(name, value);
|
|
|
954 |
};
|
|
|
955 |
|
|
|
956 |
Y.IO = IO;
|
|
|
957 |
// Map of all IO instances created.
|
|
|
958 |
Y.io._map = {};
|
|
|
959 |
var XHR = win && win.XMLHttpRequest,
|
|
|
960 |
XDR = win && win.XDomainRequest,
|
|
|
961 |
AX = win && win.ActiveXObject,
|
|
|
962 |
|
|
|
963 |
// Checks for the presence of the `withCredentials` in an XHR instance
|
|
|
964 |
// object, which will be present if the environment supports CORS.
|
|
|
965 |
SUPPORTS_CORS = XHR && 'withCredentials' in (new XMLHttpRequest());
|
|
|
966 |
|
|
|
967 |
|
|
|
968 |
Y.mix(Y.IO, {
|
|
|
969 |
/**
|
|
|
970 |
* The ID of the default IO transport, defaults to `xhr`
|
|
|
971 |
* @property _default
|
|
|
972 |
* @type {String}
|
|
|
973 |
* @static
|
|
|
974 |
*/
|
|
|
975 |
_default: 'xhr',
|
|
|
976 |
/**
|
|
|
977 |
*
|
|
|
978 |
* @method defaultTransport
|
|
|
979 |
* @static
|
|
|
980 |
* @param {String} [id] The transport to set as the default, if empty a new transport is created.
|
|
|
981 |
* @return {Object} The transport object with a `send` method
|
|
|
982 |
*/
|
|
|
983 |
defaultTransport: function(id) {
|
|
|
984 |
if (id) {
|
|
|
985 |
Y.IO._default = id;
|
|
|
986 |
} else {
|
|
|
987 |
var o = {
|
|
|
988 |
c: Y.IO.transports[Y.IO._default](),
|
|
|
989 |
notify: Y.IO._default === 'xhr' ? false : true
|
|
|
990 |
};
|
|
|
991 |
return o;
|
|
|
992 |
}
|
|
|
993 |
},
|
|
|
994 |
/**
|
|
|
995 |
* An object hash of custom transports available to IO
|
|
|
996 |
* @property transports
|
|
|
997 |
* @type {Object}
|
|
|
998 |
* @static
|
|
|
999 |
*/
|
|
|
1000 |
transports: {
|
|
|
1001 |
xhr: function () {
|
|
|
1002 |
return XHR ? new XMLHttpRequest() :
|
|
|
1003 |
AX ? new ActiveXObject('Microsoft.XMLHTTP') : null;
|
|
|
1004 |
},
|
|
|
1005 |
xdr: function () {
|
|
|
1006 |
return XDR ? new XDomainRequest() : null;
|
|
|
1007 |
},
|
|
|
1008 |
iframe: function () { return {}; },
|
|
|
1009 |
flash: null,
|
|
|
1010 |
nodejs: null
|
|
|
1011 |
},
|
|
|
1012 |
/**
|
|
|
1013 |
* Create a custom transport of type and return it's object
|
|
|
1014 |
* @method customTransport
|
|
|
1015 |
* @param {String} id The id of the transport to create.
|
|
|
1016 |
* @static
|
|
|
1017 |
*/
|
|
|
1018 |
customTransport: function(id) {
|
|
|
1019 |
var o = { c: Y.IO.transports[id]() };
|
|
|
1020 |
|
|
|
1021 |
o[(id === 'xdr' || id === 'flash') ? 'xdr' : 'notify'] = true;
|
|
|
1022 |
return o;
|
|
|
1023 |
}
|
|
|
1024 |
});
|
|
|
1025 |
|
|
|
1026 |
Y.mix(Y.IO.prototype, {
|
|
|
1027 |
/**
|
|
|
1028 |
* Fired from the notify method of the transport which in turn fires
|
|
|
1029 |
* the event on the IO object.
|
|
|
1030 |
* @method notify
|
|
|
1031 |
* @param {String} event The name of the event
|
|
|
1032 |
* @param {Object} transaction The transaction object
|
|
|
1033 |
* @param {Object} config The configuration object for this transaction
|
|
|
1034 |
*/
|
|
|
1035 |
notify: function(event, transaction, config) {
|
|
|
1036 |
var io = this;
|
|
|
1037 |
|
|
|
1038 |
switch (event) {
|
|
|
1039 |
case 'timeout':
|
|
|
1040 |
case 'abort':
|
|
|
1041 |
case 'transport error':
|
|
|
1042 |
transaction.c = { status: 0, statusText: event };
|
|
|
1043 |
event = 'failure';
|
|
|
1044 |
default:
|
|
|
1045 |
io[event].apply(io, [transaction, config]);
|
|
|
1046 |
}
|
|
|
1047 |
}
|
|
|
1048 |
});
|
|
|
1049 |
|
|
|
1050 |
|
|
|
1051 |
|
|
|
1052 |
|
|
|
1053 |
}, '3.18.1', {"requires": ["event-custom-base", "querystring-stringify-simple"]});
|