API Docs for: 3.13.0
Show:

File: datasource/js/datasource-get.js

  1. /**
  2. * Provides a DataSource implementation which can be used to retrieve data via the Get Utility.
  3. *
  4. * @module datasource
  5. * @submodule datasource-get
  6. */
  7.  
  8. /**
  9. * Get Utility subclass for the DataSource Utility.
  10. * @class DataSource.Get
  11. * @extends DataSource.Local
  12. * @constructor
  13. */
  14. var DSGet = function() {
  15. DSGet.superclass.constructor.apply(this, arguments);
  16. };
  17.  
  18.  
  19. Y.DataSource.Get = Y.extend(DSGet, Y.DataSource.Local, {
  20. /**
  21. * Passes query string to Get Utility. Fires <code>response</code> event when
  22. * response is received asynchronously.
  23. *
  24. * @method _defRequestFn
  25. * @param e {Event.Facade} Event Facade with the following properties:
  26. * <dl>
  27. * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
  28. * <dt>request (Object)</dt> <dd>The request.</dd>
  29. * <dt>callback (Object)</dt> <dd>The callback object with the following properties:
  30. * <dl>
  31. * <dt>success (Function)</dt> <dd>Success handler.</dd>
  32. * <dt>failure (Function)</dt> <dd>Failure handler.</dd>
  33. * </dl>
  34. * </dd>
  35. * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
  36. * </dl>
  37. * @protected
  38. */
  39. _defRequestFn: function(e) {
  40. var uri = this.get("source"),
  41. get = this.get("get"),
  42. guid = Y.guid().replace(/\-/g, '_'),
  43. generateRequest = this.get( "generateRequestCallback" ),
  44. payload = e.details[0],
  45. self = this;
  46.  
  47. /**
  48. * Stores the most recent request id for validation against stale
  49. * response handling.
  50. *
  51. * @property _last
  52. * @type {String}
  53. * @protected
  54. */
  55. this._last = guid;
  56.  
  57. // Dynamically add handler function with a closure to the callback stack
  58. // for access to guid
  59. YUI.Env.DataSource.callbacks[guid] = function(response) {
  60. delete YUI.Env.DataSource.callbacks[guid];
  61. delete Y.DataSource.Local.transactions[e.tId];
  62.  
  63. var process = self.get('asyncMode') !== "ignoreStaleResponses" ||
  64. self._last === guid;
  65.  
  66. if (process) {
  67. payload.data = response;
  68.  
  69. self.fire("data", payload);
  70. } else {
  71. Y.log("DataSource ignored stale response for id " + e.tId + "(" + e.request + ")", "info", "datasource-get");
  72. }
  73.  
  74. };
  75.  
  76. // Add the callback param to the request url
  77. uri += e.request + generateRequest.call( this, guid );
  78.  
  79. Y.log("DataSource is querying URL " + uri, "info", "datasource-get");
  80.  
  81. Y.DataSource.Local.transactions[e.tId] = get.script(uri, {
  82. autopurge: true,
  83. // Works in Firefox only....
  84. onFailure: function (o) {
  85. delete YUI.Env.DataSource.callbacks[guid];
  86. delete Y.DataSource.Local.transactions[e.tId];
  87.  
  88. payload.error = new Error(o.msg || "Script node data failure");
  89.  
  90. Y.log("Script node data failure", "error", "datasource-get");
  91.  
  92. self.fire("data", payload);
  93. },
  94. onTimeout: function(o) {
  95. delete YUI.Env.DataSource.callbacks[guid];
  96. delete Y.DataSource.Local.transactions[e.tId];
  97.  
  98. payload.error = new Error(o.msg || "Script node data timeout");
  99.  
  100. Y.log("Script node data timeout", "error", "datasource-get");
  101.  
  102. self.fire("data", payload);
  103. }
  104. });
  105.  
  106. return e.tId;
  107. },
  108.  
  109.  
  110. /**
  111. * Default method for adding callback param to url. See
  112. * generateRequestCallback attribute.
  113. *
  114. * @method _generateRequest
  115. * @param guid {String} unique identifier for callback function wrapper
  116. * @protected
  117. */
  118. _generateRequest: function (guid) {
  119. return "&" + this.get("scriptCallbackParam") +
  120. "=YUI.Env.DataSource.callbacks." + guid;
  121. }
  122.  
  123. }, {
  124.  
  125. /**
  126. * Class name.
  127. *
  128. * @property NAME
  129. * @type String
  130. * @static
  131. * @final
  132. * @value "dataSourceGet"
  133. */
  134. NAME: "dataSourceGet",
  135.  
  136.  
  137. ////////////////////////////////////////////////////////////////////////////
  138. //
  139. // DataSource.Get Attributes
  140. //
  141. ////////////////////////////////////////////////////////////////////////////
  142. ATTRS: {
  143. /**
  144. * Pointer to Get Utility.
  145. *
  146. * @attribute get
  147. * @type Y.Get
  148. * @default Y.Get
  149. */
  150. get: {
  151. value: Y.Get,
  152. cloneDefaultValue: false
  153. },
  154.  
  155. /**
  156. * Defines request/response management in the following manner:
  157. * <dl>
  158. * <!--<dt>queueRequests</dt>
  159. * <dd>If a request is already in progress, wait until response is
  160. * returned before sending the next request.</dd>
  161. * <dt>cancelStaleRequests</dt>
  162. * <dd>If a request is already in progress, cancel it before
  163. * sending the next request.</dd>-->
  164. * <dt>ignoreStaleResponses</dt>
  165. * <dd>Send all requests, but handle only the response for the most
  166. * recently sent request.</dd>
  167. * <dt>allowAll</dt>
  168. * <dd>Send all requests and handle all responses.</dd>
  169. * </dl>
  170. *
  171. * @attribute asyncMode
  172. * @type String
  173. * @default "allowAll"
  174. */
  175. asyncMode: {
  176. value: "allowAll"
  177. },
  178.  
  179. /**
  180. * Callback string parameter name sent to the remote script. By default,
  181. * requests are sent to
  182. * &#60;URI&#62;?&#60;scriptCallbackParam&#62;=callbackFunction
  183. *
  184. * @attribute scriptCallbackParam
  185. * @type String
  186. * @default "callback"
  187. */
  188. scriptCallbackParam : {
  189. value: "callback"
  190. },
  191.  
  192. /**
  193. * Accepts the DataSource instance and a callback ID, and returns a callback
  194. * param/value string that gets appended to the script URI. Implementers
  195. * can customize this string to match their server's query syntax.
  196. *
  197. * @attribute generateRequestCallback
  198. * @type Function
  199. */
  200. generateRequestCallback : {
  201. value: function () {
  202. return this._generateRequest.apply(this, arguments);
  203. }
  204. }
  205. }
  206. });
  207.  
  208. YUI.namespace("Env.DataSource.callbacks");
  209.