API Docs for: 3.10.3
Show:

File: io/js/transports.js

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