Dashboard sipadu mbip
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

io-base-debug.js 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. /*
  2. Copyright (c) 2010, Yahoo! Inc. All rights reserved.
  3. Code licensed under the BSD License:
  4. http://developer.yahoo.com/yui/license.html
  5. version: 3.4.0
  6. build: nightly
  7. */
  8. YUI.add('io-base', function(Y) {
  9. /**
  10. Base IO functionality. Provides basic XHR transport support.
  11. @module io-base
  12. @main io-base
  13. @for IO
  14. **/
  15. var isNumber = Y.Lang.isNumber,
  16. isObject = Y.Lang.isObject,
  17. // List of events that comprise the IO event lifecycle.
  18. EVENTS = ['start', 'complete', 'end', 'success', 'failure'],
  19. // Whitelist of used XHR response object properties.
  20. XHR_PROPS = ['status', 'statusText', 'responseText', 'responseXML'],
  21. win = Y.config.win,
  22. NativeXHR = win.XMLHttpRequest,
  23. NativeXDR = win.XDomainRequest,
  24. uid = 0;
  25. /**
  26. The IO class is a utility that brokers HTTP requests through a simplified
  27. interface. Specifically, it allows JavaScript to make HTTP requests to
  28. a resource without a page reload. The underlying transport for making
  29. same-domain requests is the XMLHttpRequest object. IO can also use
  30. Flash, if specified as a transport, for cross-domain requests.
  31. @class IO
  32. @constructor
  33. @param {Object} config Object of EventTarget's publish method configurations
  34. used to configure IO's events.
  35. **/
  36. function IO (config) {
  37. var io = this;
  38. io._uid = 'io:' + uid++;
  39. io._init(config);
  40. Y.io._map[io._uid] = io;
  41. }
  42. IO.prototype = {
  43. //--------------------------------------
  44. // Properties
  45. //--------------------------------------
  46. /**
  47. * A counter that increments for each transaction.
  48. *
  49. * @property _id
  50. * @private
  51. * @type {Number}
  52. */
  53. _id: 0,
  54. /**
  55. * Object of IO HTTP headers sent with each transaction.
  56. *
  57. * @property _headers
  58. * @private
  59. * @type {Object}
  60. */
  61. _headers: {
  62. 'X-Requested-With' : 'XMLHttpRequest'
  63. },
  64. /**
  65. * Object that stores timeout values for any transaction with a defined
  66. * "timeout" configuration property.
  67. *
  68. * @property _timeout
  69. * @private
  70. * @type {Object}
  71. */
  72. _timeout: {},
  73. //--------------------------------------
  74. // Methods
  75. //--------------------------------------
  76. _init: function(config) {
  77. var io = this, i;
  78. io.cfg = config || {};
  79. Y.augment(io, Y.EventTarget);
  80. for (i = 0; i < 5; i++) {
  81. // Publish IO global events with configurations, if any.
  82. // IO global events are set to broadcast by default.
  83. // These events use the "io:" namespace.
  84. io.publish('io:' + EVENTS[i], Y.merge({ broadcast: 1 }, config));
  85. // Publish IO transaction events with configurations, if
  86. // any. These events use the "io-trn:" namespace.
  87. io.publish('io-trn:' + EVENTS[i], config);
  88. }
  89. },
  90. /**
  91. * Method that creates a unique transaction object for each request.
  92. *
  93. * @method _create
  94. * @private
  95. * @param {Object} config Configuration object subset to determine if
  96. * the transaction is an XDR or file upload,
  97. * requiring an alternate transport.
  98. * @param {Number} id Transaction id
  99. * @return {Object} The transaction object
  100. */
  101. _create: function(config, id) {
  102. var io = this,
  103. transaction = {
  104. id : isNumber(id) ? id : io._id++,
  105. uid: io._uid
  106. },
  107. xdrConfig = config.xdr,
  108. use = xdrConfig && xdrConfig.use,
  109. ie = (xdrConfig && xdrConfig.use === 'native' && NativeXDR),
  110. transport = io._transport;
  111. if (!use) {
  112. use = (config.form && config.form.upload) ? 'iframe' : 'xhr';
  113. }
  114. switch (use) {
  115. case 'native':
  116. case 'xhr':
  117. transaction.c = ie ?
  118. new NativeXDR() :
  119. NativeXHR ?
  120. new NativeXHR() :
  121. new ActiveXObject('Microsoft.XMLHTTP');
  122. transaction.t = ie ? true : false;
  123. break;
  124. default:
  125. transaction.c = (transport && transport[use]) || {};
  126. transaction.t = true;
  127. }
  128. return transaction;
  129. },
  130. _destroy: function(transaction) {
  131. if (win && !transaction.t) {
  132. if (NativeXHR) {
  133. transaction.c.onreadystatechange = null;
  134. } else if (Y.UA.ie && !transaction.e) {
  135. // IE, when using XMLHttpRequest as an ActiveX Object, will throw
  136. // a "Type Mismatch" error if the event handler is set to "null".
  137. transaction.c.abort();
  138. }
  139. }
  140. transaction = transaction.c = null;
  141. },
  142. /**
  143. * Method for creating and firing events.
  144. *
  145. * @method _evt
  146. * @private
  147. * @param {String} eventName Event to be published.
  148. * @param {Object} transaction Transaction object.
  149. * @param {Object} config Configuration data subset for event subscription.
  150. */
  151. _evt: function(eventName, transaction, config) {
  152. var io = this, params,
  153. args = config['arguments'],
  154. emitFacade = io.cfg.emitFacade,
  155. globalEvent = "io:" + eventName,
  156. trnEvent = "io-trn:" + eventName;
  157. if (transaction.e) {
  158. transaction.c = { status: 0, statusText: transaction.e };
  159. }
  160. // Fire event with parameters or an Event Facade.
  161. params = [(emitFacade) ?
  162. {
  163. id: transaction.id,
  164. data: transaction.c,
  165. cfg: config,
  166. 'arguments': args
  167. } :
  168. transaction.id
  169. ];
  170. if (!emitFacade) {
  171. if (eventName === EVENTS[0] || eventName === EVENTS[2]) {
  172. if (args) {
  173. params.push(args);
  174. }
  175. } else {
  176. params.push(transaction.c);
  177. if (args) {
  178. params.push(args);
  179. }
  180. }
  181. }
  182. params.unshift(globalEvent);
  183. // Fire global events.
  184. io.fire.apply(io, params);
  185. // Fire transaction events, if receivers are defined.
  186. if (config.on) {
  187. params[0] = trnEvent;
  188. io.once(trnEvent, config.on[eventName], config.context || Y);
  189. io.fire.apply(io, params);
  190. }
  191. },
  192. /**
  193. * Fires event "io:start" and creates, fires a transaction-specific
  194. * start event, if `config.on.start` is defined.
  195. *
  196. * @method start
  197. * @param {Object} transaction Transaction object.
  198. * @param {Object} config Configuration object for the transaction.
  199. */
  200. start: function(transaction, config) {
  201. /**
  202. * Signals the start of an IO request.
  203. * @event io:start
  204. */
  205. this._evt(EVENTS[0], transaction, config);
  206. },
  207. /**
  208. * Fires event "io:complete" and creates, fires a
  209. * transaction-specific "complete" event, if config.on.complete is
  210. * defined.
  211. *
  212. * @method complete
  213. * @param {Object} transaction Transaction object.
  214. * @param {Object} config Configuration object for the transaction.
  215. */
  216. complete: function(transaction, config) {
  217. /**
  218. * Signals the completion of the request-response phase of a
  219. * transaction. Response status and data are accessible, if
  220. * available, in this event.
  221. * @event io:complete
  222. */
  223. this._evt(EVENTS[1], transaction, config);
  224. },
  225. /**
  226. * Fires event "io:end" and creates, fires a transaction-specific "end"
  227. * event, if config.on.end is defined.
  228. *
  229. * @method end
  230. * @param {Object} transaction Transaction object.
  231. * @param {Object} config Configuration object for the transaction.
  232. */
  233. end: function(transaction, config) {
  234. /**
  235. * Signals the end of the transaction lifecycle.
  236. * @event io:end
  237. */
  238. this._evt(EVENTS[2], transaction, config);
  239. this._destroy(transaction);
  240. },
  241. /**
  242. * Fires event "io:success" and creates, fires a transaction-specific
  243. * "success" event, if config.on.success is defined.
  244. *
  245. * @method success
  246. * @param {Object} transaction Transaction object.
  247. * @param {Object} config Configuration object for the transaction.
  248. */
  249. success: function(transaction, config) {
  250. /**
  251. * Signals an HTTP response with status in the 2xx range.
  252. * Fires after io:complete.
  253. * @event io:success
  254. */
  255. this._evt(EVENTS[3], transaction, config);
  256. this.end(transaction, config);
  257. },
  258. /**
  259. * Fires event "io:failure" and creates, fires a transaction-specific
  260. * "failure" event, if config.on.failure is defined.
  261. *
  262. * @method failure
  263. * @param {Object} transaction Transaction object.
  264. * @param {Object} config Configuration object for the transaction.
  265. */
  266. failure: function(transaction, config) {
  267. /**
  268. * Signals an HTTP response with status outside of the 2xx range.
  269. * Fires after io:complete.
  270. * @event io:failure
  271. */
  272. this._evt(EVENTS[4], transaction, config);
  273. this.end(transaction, config);
  274. },
  275. /**
  276. * Retry an XDR transaction, using the Flash tranport, if the native
  277. * transport fails.
  278. *
  279. * @method _retry
  280. * @private
  281. * @param {Object} transaction Transaction object.
  282. * @param {String} uri Qualified path to transaction resource.
  283. * @param {Object} config Configuration object for the transaction.
  284. */
  285. _retry: function(transaction, uri, config) {
  286. this._destroy(transaction);
  287. config.xdr.use = 'flash';
  288. return this.send(uri, config, transaction.id);
  289. },
  290. /**
  291. * Method that concatenates string data for HTTP GET transactions.
  292. *
  293. * @method _concat
  294. * @private
  295. * @param {String} uri URI or root data.
  296. * @param {String} data Data to be concatenated onto URI.
  297. * @return {String}
  298. */
  299. _concat: function(uri, data) {
  300. uri += (uri.indexOf('?') === -1 ? '?' : '&') + data;
  301. return uri;
  302. },
  303. /**
  304. * Stores default client headers for all transactions. If a label is
  305. * passed with no value argument, the header will be deleted.
  306. *
  307. * @method setHeader
  308. * @param {String} name HTTP header
  309. * @param {String} value HTTP header value
  310. */
  311. setHeader: function(name, value) {
  312. if (value) {
  313. this._headers[name] = value;
  314. } else {
  315. delete this._headers[name];
  316. }
  317. },
  318. /**
  319. * Method that sets all HTTP headers to be sent in a transaction.
  320. *
  321. * @method _setHeaders
  322. * @private
  323. * @param {Object} transaction - XHR instance for the specific transaction.
  324. * @param {Object} headers - HTTP headers for the specific transaction, as
  325. * defined in the configuration object passed to YUI.io().
  326. */
  327. _setHeaders: function(transaction, headers) {
  328. headers = Y.merge(this._headers, headers);
  329. Y.Object.each(headers, function(value, name) {
  330. if (value !== 'disable') {
  331. transaction.setRequestHeader(name, headers[name]);
  332. }
  333. });
  334. },
  335. /**
  336. * Starts timeout count if the configuration object has a defined
  337. * timeout property.
  338. *
  339. * @method _startTimeout
  340. * @private
  341. * @param {Object} transaction Transaction object generated by _create().
  342. * @param {Object} timeout Timeout in milliseconds.
  343. */
  344. _startTimeout: function(transaction, timeout) {
  345. var io = this;
  346. io._timeout[transaction.id] = win.setTimeout(function() {
  347. io._abort(transaction, 'timeout');
  348. }, timeout);
  349. },
  350. /**
  351. * Clears the timeout interval started by _startTimeout().
  352. *
  353. * @method _clearTimeout
  354. * @private
  355. * @param {Number} id - Transaction id.
  356. */
  357. _clearTimeout: function(id) {
  358. win.clearTimeout(this._timeout[id]);
  359. delete this._timeout[id];
  360. },
  361. /**
  362. * Method that determines if a transaction response qualifies as success
  363. * or failure, based on the response HTTP status code, and fires the
  364. * appropriate success or failure events.
  365. *
  366. * @method _result
  367. * @private
  368. * @static
  369. * @param {Object} transaction Transaction object generated by _create().
  370. * @param {Object} config Configuration object passed to io().
  371. */
  372. _result: function(transaction, config) {
  373. var status;
  374. // Firefox will throw an exception if attempting to access
  375. // an XHR object's status property, after a request is aborted.
  376. try {
  377. status = transaction.c.status;
  378. } catch(e) {
  379. status = 0;
  380. }
  381. // IE reports HTTP 204 as HTTP 1223.
  382. if (status >= 200 && status < 300 || status === 304 || status === 1223) {
  383. this.success(transaction, config);
  384. } else {
  385. this.failure(transaction, config);
  386. }
  387. },
  388. /**
  389. * Event handler bound to onreadystatechange.
  390. *
  391. * @method _rS
  392. * @private
  393. * @param {Object} transaction Transaction object generated by _create().
  394. * @param {Object} config Configuration object passed to YUI.io().
  395. */
  396. _rS: function(transaction, config) {
  397. var io = this;
  398. if (transaction.c.readyState === 4) {
  399. if (config.timeout) {
  400. io._clearTimeout(transaction.id);
  401. }
  402. // Yield in the event of request timeout or abort.
  403. win.setTimeout(function() {
  404. io.complete(transaction, config);
  405. io._result(transaction, config);
  406. }, 0);
  407. }
  408. },
  409. /**
  410. * Terminates a transaction due to an explicit abort or timeout.
  411. *
  412. * @method _abort
  413. * @private
  414. * @param {Object} transaction Transaction object generated by _create().
  415. * @param {String} type Identifies timed out or aborted transaction.
  416. */
  417. _abort: function(transaction, type) {
  418. if (transaction && transaction.c) {
  419. transaction.e = type;
  420. transaction.c.abort();
  421. }
  422. },
  423. /**
  424. * Requests a transaction. `send()` is implemented as `Y.io()`. Each
  425. * transaction may include a configuration object. Its properties are:
  426. *
  427. * <dl>
  428. * <dt>method</dt>
  429. * <dd>HTTP method verb (e.g., GET or POST). If this property is not
  430. * not defined, the default value will be GET.</dd>
  431. *
  432. * <dt>data</dt>
  433. * <dd>This is the name-value string that will be sent as the
  434. * transaction data. If the request is HTTP GET, the data become
  435. * part of querystring. If HTTP POST, the data are sent in the
  436. * message body.</dd>
  437. *
  438. * <dt>xdr</dt>
  439. * <dd>Defines the transport to be used for cross-domain requests.
  440. * By setting this property, the transaction will use the specified
  441. * transport instead of XMLHttpRequest. The properties of the
  442. * transport object are:
  443. * <dl>
  444. * <dt>use</dt>
  445. * <dd>The transport to be used: 'flash' or 'native'</dd>
  446. * <dt>dataType</dt>
  447. * <dd>Set the value to 'XML' if that is the expected response
  448. * content type.</dd>
  449. * </dl></dd>
  450. *
  451. * <dt>form</dt>
  452. * <dd>Form serialization configuration object. Its properties are:
  453. * <dl>
  454. * <dt>id</dt>
  455. * <dd>Node object or id of HTML form</dd>
  456. * <dt>useDisabled</dt>
  457. * <dd>`true` to also serialize disabled form field values
  458. * (defaults to `false`)</dd>
  459. * </dl></dd>
  460. *
  461. * <dt>on</dt>
  462. * <dd>Assigns transaction event subscriptions. Available events are:
  463. * <dl>
  464. * <dt>start</dt>
  465. * <dd>Fires when a request is sent to a resource.</dd>
  466. * <dt>complete</dt>
  467. * <dd>Fires when the transaction is complete.</dd>
  468. * <dt>success</dt>
  469. * <dd>Fires when the HTTP response status is within the 2xx
  470. * range.</dd>
  471. * <dt>failure</dt>
  472. * <dd>Fires when the HTTP response status is outside the 2xx
  473. * range, if an exception occurs, if the transation is aborted,
  474. * or if the transaction exceeds a configured `timeout`.</dd>
  475. * <dt>end</dt>
  476. * <dd>Fires at the conclusion of the transaction
  477. * lifecycle, after `success` or `failure`.</dd>
  478. * </dl>
  479. *
  480. * <p>Callback functions for `start` and `end` receive the id of the
  481. * transaction as a first argument. For `complete`, `success`, and
  482. * `failure`, callbacks receive the id and the response object
  483. * (usually the XMLHttpRequest instance). If the `arguments`
  484. * property was included in the configuration object passed to
  485. * `Y.io()`, the configured data will be passed to all callbacks as
  486. * the last argument.</p>
  487. * </dd>
  488. *
  489. * <dt>sync</dt>
  490. * <dd>Pass `true` to make a same-domain transaction synchronous.
  491. * <strong>CAVEAT</strong>: This will negatively impact the user
  492. * experience. Have a <em>very</em> good reason if you intend to use
  493. * this.</dd>
  494. *
  495. * <dt>context</dt>
  496. * <dd>The "`this'" object for all configured event handlers. If a
  497. * specific context is needed for individual callbacks, bind the
  498. * callback to a context using `Y.bind()`.</dd>
  499. *
  500. * <dt>headers</dt>
  501. * <dd>Object map of transaction headers to send to the server. The
  502. * object keys are the header names and the values are the header
  503. * values.</dd>
  504. *
  505. * <dt>timeout</dt>
  506. * <dd>Millisecond threshold for the transaction before being
  507. * automatically aborted.</dd>
  508. *
  509. * <dt>arguments</dt>
  510. * <dd>User-defined data passed to all registered event handlers.
  511. * This value is available as the second argument in the "start" and
  512. * "end" event handlers. It is the third argument in the "complete",
  513. * "success", and "failure" event handlers. <strong>Be sure to quote
  514. * this property name in the transaction configuration as
  515. * "arguments" is a reserved word in JavaScript</strong> (e.g.
  516. * `Y.io({ ..., "arguments": stuff })`).</dd>
  517. * </dl>
  518. *
  519. * @method send
  520. * @public
  521. * @param {String} uri Qualified path to transaction resource.
  522. * @param {Object} config Configuration object for the transaction.
  523. * @param {Number} id Transaction id, if already set.
  524. * @return {Object}
  525. */
  526. send: function(uri, config, id) {
  527. var transaction, method, i, len, sync, data,
  528. io = this,
  529. u = uri,
  530. response = {};
  531. config = config ? Y.Object(config) : {};
  532. transaction = io._create(config, id);
  533. method = config.method ? config.method.toUpperCase() : 'GET';
  534. sync = config.sync;
  535. data = config.data;
  536. // Serialize an object into a key-value string using
  537. // querystring-stringify-simple.
  538. if (isObject(data)) {
  539. data = Y.QueryString.stringify(data);
  540. }
  541. if (config.form) {
  542. if (config.form.upload) {
  543. // This is a file upload transaction, calling
  544. // upload() in io-upload-iframe.
  545. return io.upload(transaction, uri, config);
  546. } else {
  547. // Serialize HTML form data into a key-value string.
  548. data = io._serialize(config.form, data);
  549. }
  550. }
  551. if (data) {
  552. switch (method) {
  553. case 'GET':
  554. case 'HEAD':
  555. case 'DELETE':
  556. u = io._concat(u, data);
  557. data = '';
  558. Y.log('HTTP' + method + ' with data. The querystring is: ' + u, 'info', 'io');
  559. break;
  560. case 'POST':
  561. case 'PUT':
  562. // If Content-Type is defined in the configuration object, or
  563. // or as a default header, it will be used instead of
  564. // 'application/x-www-form-urlencoded; charset=UTF-8'
  565. config.headers = Y.merge({
  566. 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
  567. }, config.headers);
  568. break;
  569. }
  570. }
  571. if (transaction.t) {
  572. // Cross-domain request or custom transport configured.
  573. return io.xdr(u, transaction, config);
  574. }
  575. if (!sync) {
  576. transaction.c.onreadystatechange = function() {
  577. io._rS(transaction, config);
  578. };
  579. }
  580. try {
  581. // Determine if request is to be set as
  582. // synchronous or asynchronous.
  583. transaction.c.open(method, u, !sync, config.username || null, config.password || null);
  584. io._setHeaders(transaction.c, config.headers || {});
  585. io.start(transaction, config);
  586. // Will work only in browsers that implement the
  587. // Cross-Origin Resource Sharing draft.
  588. if (config.xdr && config.xdr.credentials) {
  589. if (!Y.UA.ie) {
  590. transaction.c.withCredentials = true;
  591. }
  592. }
  593. // Using "null" with HTTP POST will result in a request
  594. // with no Content-Length header defined.
  595. transaction.c.send(data);
  596. if (sync) {
  597. // Create a response object for synchronous transactions,
  598. // mixing id and arguments properties with the xhr
  599. // properties whitelist.
  600. for (i = 0, len = XHR_PROPS.length; i < len; ++i) {
  601. response[XHR_PROPS[i]] = transaction.c[XHR_PROPS[i]];
  602. }
  603. response.getAllResponseHeaders = function() {
  604. return transaction.c.getAllResponseHeaders();
  605. };
  606. response.getResponseHeader = function(name) {
  607. return transaction.c.getResponseHeader(name);
  608. };
  609. io.complete(transaction, config);
  610. io._result(transaction, config);
  611. return response;
  612. }
  613. } catch(e) {
  614. if (transaction.t) {
  615. // This exception is usually thrown by browsers
  616. // that do not support XMLHttpRequest Level 2.
  617. // Retry the request with the XDR transport set
  618. // to 'flash'. If the Flash transport is not
  619. // initialized or available, the transaction
  620. // will resolve to a transport error.
  621. return io._retry(transaction, uri, config);
  622. } else {
  623. io.complete(transaction, config);
  624. io._result(transaction, config);
  625. }
  626. }
  627. // If config.timeout is defined, and the request is standard XHR,
  628. // initialize timeout polling.
  629. if (config.timeout) {
  630. io._startTimeout(transaction, config.timeout);
  631. Y.log('Configuration timeout set to: ' + config.timeout, 'info', 'io');
  632. }
  633. return {
  634. id: transaction.id,
  635. abort: function() {
  636. return transaction.c ? io._abort(transaction, 'abort') : false;
  637. },
  638. isInProgress: function() {
  639. return transaction.c ? (transaction.c.readyState % 4) : false;
  640. },
  641. io: io
  642. };
  643. }
  644. };
  645. /**
  646. Method for initiating an ajax call. The first argument is the url end
  647. point for the call. The second argument is an object to configure the
  648. transaction and attach event subscriptions. The configuration object
  649. supports the following properties:
  650. <dl>
  651. <dt>method</dt>
  652. <dd>HTTP method verb (e.g., GET or POST). If this property is not
  653. not defined, the default value will be GET.</dd>
  654. <dt>data</dt>
  655. <dd>This is the name-value string that will be sent as the
  656. transaction data. If the request is HTTP GET, the data become
  657. part of querystring. If HTTP POST, the data are sent in the
  658. message body.</dd>
  659. <dt>xdr</dt>
  660. <dd>Defines the transport to be used for cross-domain requests.
  661. By setting this property, the transaction will use the specified
  662. transport instead of XMLHttpRequest. The properties of the
  663. transport object are:
  664. <dl>
  665. <dt>use</dt>
  666. <dd>The transport to be used: 'flash' or 'native'</dd>
  667. <dt>dataType</dt>
  668. <dd>Set the value to 'XML' if that is the expected response
  669. content type.</dd>
  670. </dl></dd>
  671. <dt>form</dt>
  672. <dd>Form serialization configuration object. Its properties are:
  673. <dl>
  674. <dt>id</dt>
  675. <dd>Node object or id of HTML form</dd>
  676. <dt>useDisabled</dt>
  677. <dd>`true` to also serialize disabled form field values
  678. (defaults to `false`)</dd>
  679. </dl></dd>
  680. <dt>on</dt>
  681. <dd>Assigns transaction event subscriptions. Available events are:
  682. <dl>
  683. <dt>start</dt>
  684. <dd>Fires when a request is sent to a resource.</dd>
  685. <dt>complete</dt>
  686. <dd>Fires when the transaction is complete.</dd>
  687. <dt>success</dt>
  688. <dd>Fires when the HTTP response status is within the 2xx
  689. range.</dd>
  690. <dt>failure</dt>
  691. <dd>Fires when the HTTP response status is outside the 2xx
  692. range, if an exception occurs, if the transation is aborted,
  693. or if the transaction exceeds a configured `timeout`.</dd>
  694. <dt>end</dt>
  695. <dd>Fires at the conclusion of the transaction
  696. lifecycle, after `success` or `failure`.</dd>
  697. </dl>
  698. <p>Callback functions for `start` and `end` receive the id of the
  699. transaction as a first argument. For `complete`, `success`, and
  700. `failure`, callbacks receive the id and the response object
  701. (usually the XMLHttpRequest instance). If the `arguments`
  702. property was included in the configuration object passed to
  703. `Y.io()`, the configured data will be passed to all callbacks as
  704. the last argument.</p>
  705. </dd>
  706. <dt>sync</dt>
  707. <dd>Pass `true` to make a same-domain transaction synchronous.
  708. <strong>CAVEAT</strong>: This will negatively impact the user
  709. experience. Have a <em>very</em> good reason if you intend to use
  710. this.</dd>
  711. <dt>context</dt>
  712. <dd>The "`this'" object for all configured event handlers. If a
  713. specific context is needed for individual callbacks, bind the
  714. callback to a context using `Y.bind()`.</dd>
  715. <dt>headers</dt>
  716. <dd>Object map of transaction headers to send to the server. The
  717. object keys are the header names and the values are the header
  718. values.</dd>
  719. <dt>timeout</dt>
  720. <dd>Millisecond threshold for the transaction before being
  721. automatically aborted.</dd>
  722. <dt>arguments</dt>
  723. <dd>User-defined data passed to all registered event handlers.
  724. This value is available as the second argument in the "start" and
  725. "end" event handlers. It is the third argument in the "complete",
  726. "success", and "failure" event handlers. <strong>Be sure to quote
  727. this property name in the transaction configuration as
  728. "arguments" is a reserved word in JavaScript</strong> (e.g.
  729. `Y.io({ ..., "arguments": stuff })`).</dd>
  730. </dl>
  731. @method io
  732. @static
  733. @param {String} url qualified path to transaction resource.
  734. @param {Object} config configuration object for the transaction.
  735. @return {Object}
  736. @for YUI
  737. **/
  738. Y.io = function(url, config) {
  739. // Calling IO through the static interface will use and reuse
  740. // an instance of IO.
  741. var transaction = Y.io._map['io:0'] || new IO();
  742. return transaction.send.apply(transaction, [url, config]);
  743. };
  744. /**
  745. Method for setting and deleting IO HTTP headers to be sent with every
  746. request.
  747. Hosted as a property on the `io` function (e.g. `Y.io.header`).
  748. @method header
  749. @param {String} name HTTP header
  750. @param {String} value HTTP header value
  751. @static
  752. **/
  753. Y.io.header = function(name, value) {
  754. // Calling IO through the static interface will use and reuse
  755. // an instance of IO.
  756. var transaction = Y.io._map['io:0'] || new IO();
  757. transaction.setHeader(name, value);
  758. };
  759. Y.IO = IO;
  760. // Map of all IO instances created.
  761. Y.io._map = {};
  762. }, '3.4.0' ,{requires:['event-custom-base', 'querystring-stringify-simple']});