|
1 /* |
|
2 Copyright (c) 2009, Yahoo! Inc. All rights reserved. |
|
3 Code licensed under the BSD License: |
|
4 http://developer.yahoo.net/yui/license.txt |
|
5 version: 3.0.0b1 |
|
6 build: 1163 |
|
7 */ |
|
8 YUI.add('io-base', function(Y) { |
|
9 |
|
10 /** |
|
11 * Base IO functionality. Provides basic XHR transport support. |
|
12 * @module io |
|
13 * @submodule io-base |
|
14 */ |
|
15 |
|
16 /** |
|
17 * The io class is a utility that brokers HTTP requests through a simplified |
|
18 * interface. Specifically, it allows JavaScript to make HTTP requests to |
|
19 * a resource without a page reload. The underlying transport for making |
|
20 * same-domain requests is the XMLHttpRequest object. YUI.io can also use |
|
21 * Flash, if specified as a transport, for cross-domain requests. |
|
22 * |
|
23 * @class io |
|
24 */ |
|
25 |
|
26 /** |
|
27 * @event io:start |
|
28 * @description This event is fired by YUI.io when a transaction is initiated. |
|
29 * @type Event Custom |
|
30 */ |
|
31 var E_START = 'io:start', |
|
32 |
|
33 /** |
|
34 * @event io:complete |
|
35 * @description This event is fired by YUI.io when a transaction is complete. |
|
36 * Response status and data are accessible, if available. |
|
37 * @type Event Custom |
|
38 */ |
|
39 E_COMPLETE = 'io:complete', |
|
40 |
|
41 /** |
|
42 * @event io:success |
|
43 * @description This event is fired by YUI.io when a transaction is complete, and |
|
44 * the HTTP status resolves to HTTP2xx. |
|
45 * @type Event Custom |
|
46 */ |
|
47 E_SUCCESS = 'io:success', |
|
48 |
|
49 /** |
|
50 * @event io:failure |
|
51 * @description This event is fired by YUI.io when a transaction is complete, and |
|
52 * the HTTP status resolves to HTTP4xx, 5xx and above. |
|
53 * @type Event Custom |
|
54 */ |
|
55 E_FAILURE = 'io:failure', |
|
56 |
|
57 /** |
|
58 * @event io:end |
|
59 * @description This event signifies the end of the transaction lifecycle. The |
|
60 * transaction transport is destroyed. |
|
61 * @type Event Custom |
|
62 */ |
|
63 E_END = 'io:end', |
|
64 |
|
65 //-------------------------------------- |
|
66 // Properties |
|
67 //-------------------------------------- |
|
68 /** |
|
69 * @description A transaction counter that increments for each transaction. |
|
70 * |
|
71 * @property transactionId |
|
72 * @private |
|
73 * @static |
|
74 * @type int |
|
75 */ |
|
76 transactionId = 0, |
|
77 |
|
78 /** |
|
79 * @description Object of default HTTP headers to be initialized and sent |
|
80 * for all transactions. |
|
81 * |
|
82 * @property _headers |
|
83 * @private |
|
84 * @static |
|
85 * @type object |
|
86 */ |
|
87 _headers = { |
|
88 'X-Requested-With' : 'XMLHttpRequest' |
|
89 }, |
|
90 |
|
91 /** |
|
92 * @description Object that stores timeout values for any transaction with |
|
93 * a defined "timeout" configuration property. |
|
94 * |
|
95 * @property _timeOut |
|
96 * @private |
|
97 * @static |
|
98 * @type object |
|
99 */ |
|
100 _timeout = {}, |
|
101 |
|
102 // Window reference |
|
103 w = Y.config.win; |
|
104 |
|
105 //-------------------------------------- |
|
106 // Methods |
|
107 //-------------------------------------- |
|
108 /** |
|
109 * @description Method for requesting a transaction. _io() is implemented as |
|
110 * yui.io(). Each transaction may include a configuration object. Its |
|
111 * properties are: |
|
112 * |
|
113 * method: HTTP method verb (e.g., GET or POST). If this property is not |
|
114 * not defined, the default value will be GET. |
|
115 * |
|
116 * data: This is the name-value string that will be sent as the transaction |
|
117 * data. If the request is HTTP GET, the data become part of |
|
118 * querystring. If HTTP POST, the data are sent in the message body. |
|
119 * |
|
120 * xdr: Defines the transport to be used for cross-domain requests. By |
|
121 * setting this property, the transaction will use the specified |
|
122 * transport instead of XMLHttpRequest. Currently, the only alternate |
|
123 * transport supported is Flash (e.g., { xdr: 'flash' }). |
|
124 * |
|
125 * form: This is a defined object used to process HTML form as data. The |
|
126 * properties are: |
|
127 * { |
|
128 * id: object, //HTML form object or id of HTML form |
|
129 * useDisabled: boolean, //Allow disabled HTML form field values |
|
130 * to be sent as part of the data. |
|
131 * } |
|
132 * |
|
133 * on: This is a defined object used to create and handle specific |
|
134 * events during a transaction lifecycle. These events will fire in |
|
135 * addition to the global io events. The events are: |
|
136 * start - This event is fired when a request is sent to a resource. |
|
137 * complete - This event fires when the transaction is complete. |
|
138 * success - This event fires when the response status resolves to |
|
139 * HTTP 2xx. |
|
140 * failure - This event fires when the response status resolves to |
|
141 * HTTP 4xx, 5xx; and, for all transaction exceptions, |
|
142 * including aborted transactions and transaction timeouts. |
|
143 * end - This even is fired at the conclusion of the transaction |
|
144 * lifecycle, after a success or failure resolution. |
|
145 * |
|
146 * The properties are: |
|
147 * { |
|
148 * start: function(id, args){}, |
|
149 * complete: function(id, responseobject, args){}, |
|
150 * success: function(id, responseobject, args){}, |
|
151 * failure: function(id, responseobject, args){}, |
|
152 * end: function(id, args){} |
|
153 * } |
|
154 * Each property can reference a function or be written as an |
|
155 * inline function. |
|
156 * |
|
157 * context: Object reference for an event handler when it is implemented |
|
158 * as a method of a base object. Defining "context" will preserve |
|
159 * the proper reference of "this" used in the event handler. |
|
160 * headers: This is a defined object of client headers, as many as. |
|
161 * desired for the transaction. These headers are sentThe object |
|
162 * pattern is: |
|
163 * { |
|
164 * header: value |
|
165 * } |
|
166 * |
|
167 * timeout: This value, defined as milliseconds, is a time threshold for the |
|
168 * transaction. When this threshold is reached, and the transaction's |
|
169 * Complete event has not yet fired, the transaction will be aborted. |
|
170 * arguments: Object, array, string, or number passed to all registered |
|
171 * event handlers. This value is available as the second |
|
172 * argument in the "start" and "abort" event handlers; and, it is |
|
173 * the third argument in the "complete", "success", and "failure" |
|
174 * event handlers. |
|
175 * |
|
176 * @method _io |
|
177 * @private |
|
178 * @static |
|
179 * @param {string} uri - qualified path to transaction resource. |
|
180 * @param {object} c - configuration object for the transaction. |
|
181 * @return object |
|
182 */ |
|
183 function _io(uri, c) { |
|
184 var u, f, |
|
185 // Set default value of argument c to Object if |
|
186 // configuration object "c" does not exist. |
|
187 c = c || {}, |
|
188 o = _create((arguments.length === 3) ? arguments[2] : null, c), |
|
189 m = (c.method) ? c.method.toUpperCase() : 'GET', |
|
190 d = (c.data) ? c.data : null; |
|
191 |
|
192 o.abort = function () { |
|
193 c.xdr ? o.c.abort(o.id, c) : _ioCancel(o, 'abort'); |
|
194 }; |
|
195 o.isInProgress = function() { |
|
196 var s = (c.xdr) ? _isInProgress(o) : (o.c.readyState !== 4 && o.c.readyState !== 0); |
|
197 return s; |
|
198 }; |
|
199 |
|
200 /* Determine configuration properties */ |
|
201 // If config.form is defined, perform data operations. |
|
202 if (c.form) { |
|
203 |
|
204 if (c.form.upload) { |
|
205 u = Y.io._upload(o, uri, c); |
|
206 return u; |
|
207 } |
|
208 |
|
209 // Serialize the HTML form into a string of name-value pairs. |
|
210 f = Y.io._serialize(c.form); |
|
211 // If config.data is defined, concatenate the data to the form string. |
|
212 if (d) { |
|
213 f += "&" + d; |
|
214 Y.log('Configuration object.data added to serialized HTML form data. The string is: ' + f, 'info', 'io'); |
|
215 } |
|
216 |
|
217 if (m === 'POST') { |
|
218 d = f; |
|
219 _setHeader('Content-Type', 'application/x-www-form-urlencoded'); |
|
220 } |
|
221 else if (m === 'GET') { |
|
222 uri = _concat(uri, f); |
|
223 Y.log('Configuration object.data added to serialized HTML form data. The querystring is: ' + uri, 'info', 'io'); |
|
224 } |
|
225 } |
|
226 else if (d && m === 'POST') { |
|
227 _setHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); |
|
228 } |
|
229 |
|
230 if (c.xdr) { |
|
231 Y.io._xdr(uri, o, c); |
|
232 return o; |
|
233 } |
|
234 |
|
235 // If config.timeout is defined, and the request is standard XHR, |
|
236 // initialize timeout polling. |
|
237 if (c.timeout) { |
|
238 _startTimeout(o, c.timeout); |
|
239 } |
|
240 /* End Configuration Properties */ |
|
241 |
|
242 o.c.onreadystatechange = function() { _readyState(o, c); }; |
|
243 try { _open(o.c, m, uri); } catch (e0) {} |
|
244 _setHeaders(o.c, (c.headers || {})); |
|
245 |
|
246 // Do not pass null, in the absence of data, as this |
|
247 // will result in a POST request with no Content-Length |
|
248 // defined. |
|
249 _async(o, (d || ''), c); |
|
250 |
|
251 return o; |
|
252 } |
|
253 |
|
254 /** |
|
255 * @description Method for creating and subscribing transaction events. |
|
256 * |
|
257 * @method _tPubSub |
|
258 * @private |
|
259 * @static |
|
260 * @param {string} e - event to be published |
|
261 * @param {object} c - configuration data subset for event subscription. |
|
262 * |
|
263 * @return void |
|
264 */ |
|
265 function _tPubSub(e, c){ |
|
266 var event = new Y.EventTarget().publish('transaction:' + e); |
|
267 event.subscribe(c.on[e], (c.context || this), c.arguments); |
|
268 |
|
269 return event; |
|
270 } |
|
271 |
|
272 /** |
|
273 * @description Fires event "io:start" and creates, fires a |
|
274 * transaction-specific start event, if config.on.start is |
|
275 * defined. |
|
276 * |
|
277 * @method _ioStart |
|
278 * @private |
|
279 * @static |
|
280 * @param {number} id - transaction id |
|
281 * @param {object} c - configuration object for the transaction. |
|
282 * |
|
283 * @return void |
|
284 */ |
|
285 function _ioStart(id, c) { |
|
286 var m = Y.io._fn || {}, |
|
287 fn = (m && m[id]) ? m[id] : null, |
|
288 event; |
|
289 // Set default value of argument c, property "on" to Object if |
|
290 // the property is null or undefined. |
|
291 c.on = c.on || {}; |
|
292 |
|
293 if (fn) { |
|
294 c.on.start = fn.start; |
|
295 } |
|
296 |
|
297 Y.fire(E_START, id); |
|
298 |
|
299 if (c.on.start) { |
|
300 event = _tPubSub('start', c); |
|
301 event.fire(id); |
|
302 } |
|
303 Y.log('Transaction ' + id + ' started.', 'info', 'io'); |
|
304 } |
|
305 |
|
306 |
|
307 /** |
|
308 * @description Fires event "io:complete" and creates, fires a |
|
309 * transaction-specific "complete" event, if config.on.complete is |
|
310 * defined. |
|
311 * |
|
312 * @method _ioComplete |
|
313 * @private |
|
314 * @static |
|
315 * @param {object} o - transaction object. |
|
316 * @param {object} c - configuration object for the transaction. |
|
317 * |
|
318 * @return void |
|
319 */ |
|
320 function _ioComplete(o, c) { |
|
321 var r, event; |
|
322 // Set default value of argument c, property "on" to Object if |
|
323 // the property is null or undefined. |
|
324 c.on = c.on || {}; |
|
325 |
|
326 r = (o.status) ? _response(o.status) : o.c; |
|
327 Y.fire(E_COMPLETE, o.id, r); |
|
328 |
|
329 if (c.on.complete) { |
|
330 event = _tPubSub('complete', c); |
|
331 event.fire(o.id, r); |
|
332 } |
|
333 Y.log('Transaction ' + o.id + ' completed.', 'info', 'io'); |
|
334 } |
|
335 |
|
336 /** |
|
337 * @description Fires event "io:success" and creates, fires a |
|
338 * transaction-specific "success" event, if config.on.success is |
|
339 * defined. |
|
340 * |
|
341 * @method _ioSuccess |
|
342 * @private |
|
343 * @static |
|
344 * @param {object} o - transaction object. |
|
345 * @param {object} c - configuration object for the transaction. |
|
346 * |
|
347 * @return void |
|
348 */ |
|
349 function _ioSuccess(o, c) { |
|
350 var m = Y.io._fn || {}, |
|
351 fn = (m && m[o.id]) ? m[o.id] : null, |
|
352 event; |
|
353 // Set default value of argument c, property "on" to Object if |
|
354 // the property is null or undefined. |
|
355 c.on = c.on || {}; |
|
356 |
|
357 if (fn) { |
|
358 c.on.success = fn.success; |
|
359 //Decode the response from IO.swf |
|
360 o.c.responseText = decodeURI(o.c.responseText); |
|
361 } |
|
362 |
|
363 Y.fire(E_SUCCESS, o.id, o.c); |
|
364 |
|
365 if (c.on.success) { |
|
366 event = _tPubSub('success', c); |
|
367 event.fire(o.id, o.c); |
|
368 } |
|
369 |
|
370 Y.log('HTTP Status evaluates to Success. The transaction is: ' + o.id, 'info', 'io'); |
|
371 _ioEnd(o, c); |
|
372 } |
|
373 |
|
374 /** |
|
375 * @description Fires event "io:failure" and creates, fires a |
|
376 * transaction-specific "failure" event, if config.on.failure is |
|
377 * defined. |
|
378 * |
|
379 * @method _ioFailure |
|
380 * @private |
|
381 * @static |
|
382 * @param {object} o - transaction object. |
|
383 * @param {object} c - configuration object for the transaction. |
|
384 * |
|
385 * @return void |
|
386 */ |
|
387 function _ioFailure(o, c) { |
|
388 var m = Y.io._fn || {}, |
|
389 fn = (m && m[o.id]) ? m[o.id] : null, |
|
390 r, event; |
|
391 // Set default value of argument c, property "on" to Object if |
|
392 // the property is null or undefined. |
|
393 c.on = c.on || {}; |
|
394 |
|
395 if (fn) { |
|
396 c.on.failure = fn.failure; |
|
397 //Decode the response from IO.swf |
|
398 o.c.responseText = decodeURI(o.c.responseText); |
|
399 } |
|
400 |
|
401 r = (o.status) ? _response(o.status) : o.c; |
|
402 Y.fire(E_FAILURE, o.id, r); |
|
403 |
|
404 if (c.on.failure) { |
|
405 event = _tPubSub('failure', c); |
|
406 event.fire(o.id, r); |
|
407 } |
|
408 |
|
409 Y.log('HTTP Status evaluates to Failure. The transaction is: ' + o.id, 'info', 'io'); |
|
410 _ioEnd(o, c); |
|
411 } |
|
412 |
|
413 /** |
|
414 * @description Fires event "io:end" and creates, fires a |
|
415 * transaction-specific "end" event, if config.on.end is |
|
416 * defined. |
|
417 * |
|
418 * @method _ioEnd |
|
419 * @private |
|
420 * @static |
|
421 * @param {object} o - transaction object. |
|
422 * @param {object} c - configuration object for the transaction. |
|
423 * |
|
424 * @return void |
|
425 */ |
|
426 function _ioEnd(o, c) { |
|
427 var m = Y.io._fn || {}, |
|
428 fn = (m && m[o.id]) ? m[o.id] : null, |
|
429 event; |
|
430 // Set default value of argument c, property "on" to Object if |
|
431 // the property is null or undefined. |
|
432 c.on = c.on || {}; |
|
433 |
|
434 if (fn) { |
|
435 c.on.end = fn.end; |
|
436 delete m[o.id]; |
|
437 } |
|
438 |
|
439 Y.fire(E_END, o.id); |
|
440 |
|
441 if (c.on.end) { |
|
442 event = _tPubSub('end', c); |
|
443 event.fire(o.id); |
|
444 } |
|
445 |
|
446 _destroy(o, (c.xdr) ? true : false ); |
|
447 Y.log('Transaction ' + o.id + ' ended.', 'info', 'io'); |
|
448 } |
|
449 |
|
450 /** |
|
451 * @description Terminates a transaction due to an explicit abort or |
|
452 * timeout. |
|
453 * |
|
454 * @method _ioCancel |
|
455 * @private |
|
456 * @static |
|
457 * @param {object} o - Transaction object generated by _create(). |
|
458 * @param {object} c - Configuration object passed to YUI.io(). |
|
459 * @param {string} s - Identifies timed out or aborted transaction. |
|
460 * |
|
461 * @return void |
|
462 */ |
|
463 function _ioCancel(o, s) { |
|
464 if (o && o.c) { |
|
465 o.status = s; |
|
466 o.c.abort(); |
|
467 } |
|
468 Y.log('Transaction cancelled due to time out or explicitly aborted. The transaction is: ' + o.id, 'info', 'io'); |
|
469 } |
|
470 |
|
471 /** |
|
472 * @description Determines if a cross-domain transaction is still |
|
473 * in progress. |
|
474 * |
|
475 * @method _isInProgress |
|
476 * @private |
|
477 * @static |
|
478 * @param {object} o - Transaction object generated by _create(). |
|
479 * |
|
480 * @return boolean |
|
481 */ |
|
482 function _isInProgress(o) { |
|
483 return o.c.isInProgress(o.id); |
|
484 } |
|
485 |
|
486 function _response(s) { |
|
487 return { status:0, statusText:s }; |
|
488 } |
|
489 |
|
490 /** |
|
491 * @description Method that increments _transactionId for each transaction. |
|
492 * |
|
493 * @method _id |
|
494 * @private |
|
495 * @static |
|
496 * @return int |
|
497 */ |
|
498 function _id() { |
|
499 var id = transactionId; |
|
500 transactionId++; |
|
501 |
|
502 Y.log('Transaction id generated. The id is: ' + id, 'info', 'io'); |
|
503 return id; |
|
504 } |
|
505 |
|
506 /** |
|
507 * @description Method that creates a unique transaction object for each |
|
508 * request. |
|
509 * |
|
510 * @method _create |
|
511 * @private |
|
512 * @static |
|
513 * @param {number} s - URI or root data. |
|
514 * @param {number} c - configuration object |
|
515 * @return object |
|
516 */ |
|
517 function _create(i, c) { |
|
518 var o = {}; |
|
519 o.id = Y.Lang.isNumber(i) ? i : _id(); |
|
520 |
|
521 if (c.xdr) { |
|
522 o.c = Y.io._transport[c.xdr.use]; |
|
523 } |
|
524 else if (c.form && c.form.upload) { |
|
525 o.c = {}; |
|
526 } |
|
527 else { |
|
528 o.c = _xhr(); |
|
529 } |
|
530 |
|
531 return o; |
|
532 }; |
|
533 |
|
534 /** |
|
535 * @description Method that creates the XMLHttpRequest transport |
|
536 * |
|
537 * @method _xhr |
|
538 * @private |
|
539 * @static |
|
540 * @return object |
|
541 */ |
|
542 function _xhr() { |
|
543 return (w.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP'); |
|
544 } |
|
545 |
|
546 /** |
|
547 * @description Method that concatenates string data for HTTP GET transactions. |
|
548 * |
|
549 * @method _concat |
|
550 * @private |
|
551 * @static |
|
552 * @param {string} s - URI or root data. |
|
553 * @param {string} d - data to be concatenated onto URI. |
|
554 * @return int |
|
555 */ |
|
556 function _concat(s, d) { |
|
557 s += ((s.indexOf('?') == -1) ? '?' : '&') + d; |
|
558 return s; |
|
559 } |
|
560 |
|
561 /** |
|
562 * @description Method that stores default client headers for all transactions. |
|
563 * If a label is passed with no value argument, the header will be deleted. |
|
564 * |
|
565 * @method _setHeader |
|
566 * @private |
|
567 * @static |
|
568 * @param {string} l - HTTP header |
|
569 * @param {string} v - HTTP header value |
|
570 * @return int |
|
571 */ |
|
572 function _setHeader(l, v) { |
|
573 if (v) { |
|
574 _headers[l] = v; |
|
575 } |
|
576 else { |
|
577 delete _headers[l]; |
|
578 } |
|
579 } |
|
580 |
|
581 /** |
|
582 * @description Method that sets all HTTP headers to be sent in a transaction. |
|
583 * |
|
584 * @method _setHeaders |
|
585 * @private |
|
586 * @static |
|
587 * @param {object} o - XHR instance for the specific transaction. |
|
588 * @param {object} h - HTTP headers for the specific transaction, as defined |
|
589 * in the configuration object passed to YUI.io(). |
|
590 * @return void |
|
591 */ |
|
592 function _setHeaders(o, h) { |
|
593 var p; |
|
594 |
|
595 for (p in _headers) { |
|
596 if (_headers.hasOwnProperty(p)) { |
|
597 if (h[p]) { |
|
598 // Configuration headers will supersede IO preset headers, |
|
599 // if headers match. |
|
600 Y.log('Matching configuration HTTP header: ' + p + ' found with value of ' + _headers[p], 'info', 'io'); |
|
601 break; |
|
602 } |
|
603 else { |
|
604 h[p] = _headers[p]; |
|
605 Y.log('HTTP header ' + p + ' found with value of ' + _headers[p], 'info', 'io'); |
|
606 } |
|
607 } |
|
608 } |
|
609 |
|
610 for (p in h) { |
|
611 if (h.hasOwnProperty(p)) { |
|
612 o.setRequestHeader(p, h[p]); |
|
613 Y.log('HTTP Header ' + p + ' set with value of ' + h[p], 'info', 'io'); |
|
614 } |
|
615 } |
|
616 } |
|
617 |
|
618 function _open(o, m, uri) { |
|
619 o.open(m, uri, true); |
|
620 } |
|
621 |
|
622 /** |
|
623 * @description Method that sends the transaction request. |
|
624 * |
|
625 * @method _async |
|
626 * @private |
|
627 * @static |
|
628 * @param {object} o - Transaction object generated by _create(). |
|
629 * @param {string} d - Transaction data. |
|
630 * @param {object} c - Configuration object passed to YUI.io(). |
|
631 * @return void |
|
632 */ |
|
633 function _async(o, d, c) { |
|
634 o.c.send(d); |
|
635 _ioStart(o.id, c); |
|
636 } |
|
637 |
|
638 /** |
|
639 * @description Starts timeout count if the configuration object |
|
640 * has a defined timeout property. |
|
641 * |
|
642 * @method _startTimeout |
|
643 * @private |
|
644 * @static |
|
645 * @param {object} o - Transaction object generated by _create(). |
|
646 * @param {object} c - Configuration object passed to YUI.io(). |
|
647 * @return void |
|
648 */ |
|
649 function _startTimeout(o, timeout) { |
|
650 _timeout[o.id] = w.setTimeout(function() { _ioCancel(o, 'timeout'); }, timeout); |
|
651 } |
|
652 |
|
653 /** |
|
654 * @description Clears the timeout interval started by _startTimeout(). |
|
655 * |
|
656 * @method _clearTimeout |
|
657 * @private |
|
658 * @static |
|
659 * @param {number} id - Transaction id. |
|
660 * @return void |
|
661 */ |
|
662 function _clearTimeout(id) { |
|
663 w.clearTimeout(_timeout[id]); |
|
664 delete _timeout[id]; |
|
665 } |
|
666 |
|
667 /** |
|
668 * @description Event handler bound to onreadystatechange. |
|
669 * |
|
670 * @method _readyState |
|
671 * @private |
|
672 * @static |
|
673 * @param {object} o - Transaction object generated by _create(). |
|
674 * @param {object} c - Configuration object passed to YUI.io(). |
|
675 * @return void |
|
676 */ |
|
677 function _readyState(o, c) { |
|
678 if (o.c.readyState === 4) { |
|
679 if (c.timeout) { |
|
680 _clearTimeout(o.id); |
|
681 } |
|
682 |
|
683 w.setTimeout( |
|
684 function() { |
|
685 _ioComplete(o, c); |
|
686 _handleResponse(o, c); |
|
687 }, 0); |
|
688 } |
|
689 } |
|
690 |
|
691 /** |
|
692 * @description Method that determines if a transaction response qualifies |
|
693 * as success or failure, based on the response HTTP status code, and |
|
694 * fires the appropriate success or failure events. |
|
695 * |
|
696 * @method _handleResponse |
|
697 * @private |
|
698 * @static |
|
699 * @param {object} o - Transaction object generated by _create(). |
|
700 * @param {object} c - Configuration object passed to io(). |
|
701 * @return void |
|
702 */ |
|
703 function _handleResponse(o, c) { |
|
704 var status; |
|
705 try{ |
|
706 if (o.c.status && o.c.status !== 0) { |
|
707 status = o.c.status; |
|
708 } |
|
709 else { |
|
710 status = 0; |
|
711 } |
|
712 } |
|
713 catch(e1) { |
|
714 status = 0; |
|
715 Y.log('HTTP status unreadable. The transaction is: ' + o.id, 'warn', 'io'); |
|
716 } |
|
717 |
|
718 // IE reports HTTP 204 as HTTP 1223. |
|
719 // But, the response data are still available. |
|
720 if (status >= 200 && status < 300 || status === 1223) { |
|
721 _ioSuccess(o, c); |
|
722 } |
|
723 else { |
|
724 _ioFailure(o, c); |
|
725 } |
|
726 } |
|
727 |
|
728 function _destroy(o, isTransport) { |
|
729 // IE, when using XMLHttpRequest as an ActiveX Object, will throw |
|
730 // a "Type Mismatch" error if the event handler is set to "null". |
|
731 if(w.XMLHttpRequest && !isTransport) { |
|
732 if (o.c) { |
|
733 o.c.onreadystatechange = null; |
|
734 } |
|
735 } |
|
736 |
|
737 o.c = null; |
|
738 o = null; |
|
739 } |
|
740 |
|
741 _io.start = _ioStart; |
|
742 _io.complete = _ioComplete; |
|
743 _io.success = _ioSuccess; |
|
744 _io.failure = _ioFailure; |
|
745 _io.isInProgress = _isInProgress; |
|
746 _io._id = _id; |
|
747 |
|
748 //-------------------------------------- |
|
749 // Begin public interface definition |
|
750 //-------------------------------------- |
|
751 /** |
|
752 * @description Method that stores default client headers for all transactions. |
|
753 * If a label is passed with no value argument, the header will be deleted. |
|
754 * This is the interface for _setHeader(). |
|
755 * |
|
756 * @method header |
|
757 * @public |
|
758 * @static |
|
759 * @param {string} l - HTTP header |
|
760 * @param {string} v - HTTP header value |
|
761 * @return int |
|
762 */ |
|
763 _io.header = _setHeader; |
|
764 |
|
765 /** |
|
766 * @description Method for requesting a transaction. This |
|
767 * is the interface for _io(). |
|
768 * |
|
769 * @method io |
|
770 * @public |
|
771 * @static |
|
772 * @param {string} uri - qualified path to transaction resource. |
|
773 * @param {object} c - configuration object for the transaction. |
|
774 * @return object |
|
775 */ |
|
776 Y.io = _io; |
|
777 Y.io.http = _io; |
|
778 |
|
779 |
|
780 |
|
781 }, '3.0.0b1' ); |