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