API Docs for: 3.17.2
Show:

File: datasource/js/datasource-io.js

  1. /**
  2. * Provides a DataSource implementation which can be used to retrieve data via the IO Utility.
  3. *
  4. * @module datasource
  5. * @submodule datasource-io
  6. */
  7.  
  8. /**
  9. * IO subclass for the DataSource Utility.
  10. * @class DataSource.IO
  11. * @extends DataSource.Local
  12. * @constructor
  13. */
  14. var DSIO = function() {
  15. DSIO.superclass.constructor.apply(this, arguments);
  16. };
  17.  
  18.  
  19. /////////////////////////////////////////////////////////////////////////////
  20. //
  21. // DataSource.IO static properties
  22. //
  23. /////////////////////////////////////////////////////////////////////////////
  24. Y.mix(DSIO, {
  25. /**
  26. * Class name.
  27. *
  28. * @property NAME
  29. * @type String
  30. * @static
  31. * @final
  32. * @value "dataSourceIO"
  33. */
  34. NAME: "dataSourceIO",
  35.  
  36.  
  37. /////////////////////////////////////////////////////////////////////////////
  38. //
  39. // DataSource.IO Attributes
  40. //
  41. /////////////////////////////////////////////////////////////////////////////
  42.  
  43. ATTRS: {
  44. /**
  45. * Pointer to IO Utility.
  46. *
  47. * @attribute io
  48. * @type Y.io
  49. * @default Y.io
  50. */
  51. io: {
  52. value: Y.io,
  53. cloneDefaultValue: false
  54. },
  55.  
  56. /**
  57. * Default IO Config.
  58. *
  59. * @attribute ioConfig
  60. * @type Object
  61. * @default null
  62. */
  63. ioConfig: {
  64. value: null
  65. }
  66. }
  67. });
  68.  
  69. Y.extend(DSIO, Y.DataSource.Local, {
  70. /**
  71. * Internal init() handler.
  72. *
  73. * @method initializer
  74. * @param config {Object} Config object.
  75. * @private
  76. */
  77. initializer: function(config) {
  78. this._queue = {interval:null, conn:null, requests:[]};
  79. },
  80.  
  81. /**
  82. * IO success callback.
  83. *
  84. * @method successHandler
  85. * @param id {String} Transaction ID.
  86. * @param response {String} Response.
  87. * @param e {EventFacade} Event facade.
  88. * @private
  89. */
  90. successHandler: function (id, response, e) {
  91. var defIOConfig = this.get("ioConfig"),
  92. payload = e.details[0];
  93.  
  94. delete Y.DataSource.Local.transactions[e.tId];
  95.  
  96. payload.data = response;
  97. this.fire("data", payload);
  98.  
  99. Y.log("Received IO data response for \"" + e.request + "\"", "info", "datasource-io");
  100.  
  101. if (defIOConfig && defIOConfig.on && defIOConfig.on.success) {
  102. defIOConfig.on.success.apply(defIOConfig.context || Y, arguments);
  103. }
  104. },
  105.  
  106. /**
  107. * IO failure callback.
  108. *
  109. * @method failureHandler
  110. * @param id {String} Transaction ID.
  111. * @param response {String} Response.
  112. * @param e {EventFacade} Event facade.
  113. * @private
  114. */
  115. failureHandler: function (id, response, e) {
  116. var defIOConfig = this.get("ioConfig"),
  117. payload = e.details[0];
  118.  
  119. delete Y.DataSource.Local.transactions[e.tId];
  120.  
  121. payload.error = new Error("IO data failure");
  122. Y.log("IO data failure", "error", "datasource-io");
  123.  
  124. payload.data = response;
  125. this.fire("data", payload);
  126.  
  127. Y.log("Received IO data failure for \"" + e.request + "\"", "info", "datasource-io");
  128.  
  129. if (defIOConfig && defIOConfig.on && defIOConfig.on.failure) {
  130. defIOConfig.on.failure.apply(defIOConfig.context || Y, arguments);
  131. }
  132. },
  133.  
  134. /**
  135. * @property _queue
  136. * @description Object literal to manage asynchronous request/response
  137. * cycles enabled if queue needs to be managed (asyncMode/ioConnMode):
  138. * <dl>
  139. * <dt>interval {Number}</dt>
  140. * <dd>Interval ID of in-progress queue.</dd>
  141. * <dt>conn</dt>
  142. * <dd>In-progress connection identifier (if applicable).</dd>
  143. * <dt>requests {Object[]}</dt>
  144. * <dd>Array of queued request objects: {request:request, callback:callback}.</dd>
  145. * </dl>
  146. * @type Object
  147. * @default {interval:null, conn:null, requests:[]}
  148. * @private
  149. */
  150. _queue: null,
  151.  
  152. /**
  153. * Passes query string to IO. Fires <code>response</code> event when
  154. * response is received asynchronously.
  155. *
  156. * @method _defRequestFn
  157. * @param e {EventFacade} Event Facade with the following properties:
  158. * <dl>
  159. * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
  160. * <dt>request (Object)</dt> <dd>The request.</dd>
  161. * <dt>callback (Object)</dt> <dd>The callback object with the following properties:
  162. * <dl>
  163. * <dt>success (Function)</dt> <dd>Success handler.</dd>
  164. * <dt>failure (Function)</dt> <dd>Failure handler.</dd>
  165. * </dl>
  166. * </dd>
  167. * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
  168. * </dl>
  169. * @protected
  170. */
  171. _defRequestFn: function(e) {
  172. var uri = this.get("source"),
  173. io = this.get("io"),
  174. defIOConfig = this.get("ioConfig"),
  175. request = e.request,
  176. cfg = Y.merge(defIOConfig, e.cfg, {
  177. on: Y.merge(defIOConfig, {
  178. success: this.successHandler,
  179. failure: this.failureHandler
  180. }),
  181. context: this,
  182. "arguments": e
  183. });
  184.  
  185. // Support for POST transactions
  186. if(Y.Lang.isString(request)) {
  187. if(cfg.method && (cfg.method.toUpperCase() === "POST")) {
  188. cfg.data = cfg.data ? cfg.data+request : request;
  189. }
  190. else {
  191. uri += request;
  192. }
  193. }
  194. Y.DataSource.Local.transactions[e.tId] = io(uri, cfg);
  195. return e.tId;
  196. }
  197. });
  198.  
  199. Y.DataSource.IO = DSIO;
  200.