Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

datasource-get.js 6.2KB

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