Version 3.18.1
Show:

File: io/js/transports.js

  1. var XHR = win && win.XMLHttpRequest,
  2. XDR = win && win.XDomainRequest,
  3. AX = win && win.ActiveXObject,
  4. // Checks for the presence of the `withCredentials` in an XHR instance
  5. // object, which will be present if the environment supports CORS.
  6. SUPPORTS_CORS = XHR && 'withCredentials' in (new XMLHttpRequest());
  7. Y.mix(Y.IO, {
  8. /**
  9. * The ID of the default IO transport, defaults to `xhr`
  10. * @property _default
  11. * @type {String}
  12. * @static
  13. */
  14. _default: 'xhr',
  15. /**
  16. *
  17. * @method defaultTransport
  18. * @static
  19. * @param {String} [id] The transport to set as the default, if empty a new transport is created.
  20. * @return {Object} The transport object with a `send` method
  21. */
  22. defaultTransport: function(id) {
  23. if (id) {
  24. Y.log('Setting default IO to: ' + id, 'info', 'io');
  25. Y.IO._default = id;
  26. } else {
  27. var o = {
  28. c: Y.IO.transports[Y.IO._default](),
  29. notify: Y.IO._default === 'xhr' ? false : true
  30. };
  31. Y.log('Creating default transport: ' + Y.IO._default, 'info', 'io');
  32. return o;
  33. }
  34. },
  35. /**
  36. * An object hash of custom transports available to IO
  37. * @property transports
  38. * @type {Object}
  39. * @static
  40. */
  41. transports: {
  42. xhr: function () {
  43. return XHR ? new XMLHttpRequest() :
  44. AX ? new ActiveXObject('Microsoft.XMLHTTP') : null;
  45. },
  46. xdr: function () {
  47. return XDR ? new XDomainRequest() : null;
  48. },
  49. iframe: function () { return {}; },
  50. flash: null,
  51. nodejs: null
  52. },
  53. /**
  54. * Create a custom transport of type and return it's object
  55. * @method customTransport
  56. * @param {String} id The id of the transport to create.
  57. * @static
  58. */
  59. customTransport: function(id) {
  60. var o = { c: Y.IO.transports[id]() };
  61. o[(id === 'xdr' || id === 'flash') ? 'xdr' : 'notify'] = true;
  62. return o;
  63. }
  64. });
  65. Y.mix(Y.IO.prototype, {
  66. /**
  67. * Fired from the notify method of the transport which in turn fires
  68. * the event on the IO object.
  69. * @method notify
  70. * @param {String} event The name of the event
  71. * @param {Object} transaction The transaction object
  72. * @param {Object} config The configuration object for this transaction
  73. */
  74. notify: function(event, transaction, config) {
  75. var io = this;
  76. switch (event) {
  77. case 'timeout':
  78. case 'abort':
  79. case 'transport error':
  80. transaction.c = { status: 0, statusText: event };
  81. event = 'failure';
  82. default:
  83. io[event].apply(io, [transaction, config]);
  84. }
  85. }
  86. });