PageRenderTime 27ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/ajax/libs/yui/3.1.0pr2/datasource/datasource-local.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 336 lines | 62 code | 25 blank | 249 comment | 8 complexity | ae8a95202eb406bb06820151d419ffc6 MD5 | raw file
  1. YUI.add('datasource-local', function(Y) {
  2. /**
  3. * The DataSource utility provides a common configurable interface for widgets to
  4. * access a variety of data, from JavaScript arrays to online database servers.
  5. *
  6. * @module datasource
  7. */
  8. /**
  9. * Provides the base DataSource implementation, which can be extended to
  10. * create DataSources for specific data protocols, such as the IO Utility, the
  11. * Get Utility, or custom functions.
  12. *
  13. * @module datasource
  14. * @submodule datasource-local
  15. */
  16. /**
  17. * Base class for the DataSource Utility.
  18. * @class DataSource.Local
  19. * @extends Base
  20. * @constructor
  21. */
  22. var LANG = Y.Lang,
  23. DSLocal = function() {
  24. DSLocal.superclass.constructor.apply(this, arguments);
  25. };
  26. /////////////////////////////////////////////////////////////////////////////
  27. //
  28. // DataSource static properties
  29. //
  30. /////////////////////////////////////////////////////////////////////////////
  31. Y.mix(DSLocal, {
  32. /**
  33. * Class name.
  34. *
  35. * @property NAME
  36. * @type String
  37. * @static
  38. * @final
  39. * @value "dataSourceLocal"
  40. */
  41. NAME: "dataSourceLocal",
  42. /////////////////////////////////////////////////////////////////////////////
  43. //
  44. // DataSource Attributes
  45. //
  46. /////////////////////////////////////////////////////////////////////////////
  47. ATTRS: {
  48. /**
  49. * @attribute source
  50. * @description Pointer to live data.
  51. * @type MIXED
  52. * @default null
  53. */
  54. source: {
  55. value: null
  56. }
  57. },
  58. /**
  59. * Global transaction counter.
  60. *
  61. * @property DataSource._tId
  62. * @type Number
  63. * @static
  64. * @private
  65. * @default 0
  66. */
  67. _tId: 0,
  68. /**
  69. * Executes a given callback. The third param determines whether to execute
  70. *
  71. * @method DataSource.issueCallback
  72. * @param callback {Object} The callback object.
  73. * @param params {Array} params to be passed to the callback method
  74. * @param error {Boolean} whether an error occurred
  75. * @static
  76. */
  77. issueCallback: function (e) {
  78. if(e.callback) {
  79. var callbackFunc = (e.error && e.callback.failure) || e.callback.success;
  80. if (callbackFunc) {
  81. callbackFunc(e);
  82. }
  83. }
  84. }
  85. });
  86. Y.extend(DSLocal, Y.Base, {
  87. /**
  88. * Internal init() handler.
  89. *
  90. * @method initializer
  91. * @param config {Object} Config object.
  92. * @private
  93. */
  94. initializer: function(config) {
  95. this._initEvents();
  96. },
  97. /**
  98. * This method creates all the events for this module.
  99. * @method _initEvents
  100. * @private
  101. */
  102. _initEvents: function() {
  103. /**
  104. * Fired when a data request is received.
  105. *
  106. * @event request
  107. * @param e {Event.Facade} Event Facade with the following properties:
  108. * <dl>
  109. * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
  110. * <dt>request (Object)</dt> <dd>The request.</dd>
  111. * <dt>callback (Object)</dt> <dd>The callback object.</dd>
  112. * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
  113. * </dl>
  114. * @preventable _defRequestFn
  115. */
  116. this.publish("request", {defaultFn: Y.bind("_defRequestFn", this), queuable:true});
  117. /**
  118. * Fired when raw data is received.
  119. *
  120. * @event data
  121. * @param e {Event.Facade} Event Facade with the following properties:
  122. * <dl>
  123. * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
  124. * <dt>request (Object)</dt> <dd>The request.</dd>
  125. * <dt>callback (Object)</dt> <dd>The callback object with the following properties:
  126. * <dl>
  127. * <dt>success (Function)</dt> <dd>Success handler.</dd>
  128. * <dt>failure (Function)</dt> <dd>Failure handler.</dd>
  129. * </dl>
  130. * </dd>
  131. * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
  132. * <dt>data (Object)</dt> <dd>Raw data.</dd>
  133. * </dl>
  134. * @preventable _defDataFn
  135. */
  136. this.publish("data", {defaultFn: Y.bind("_defDataFn", this), queuable:true});
  137. /**
  138. * Fired when response is returned.
  139. *
  140. * @event response
  141. * @param e {Event.Facade} Event Facade with the following properties:
  142. * <dl>
  143. * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
  144. * <dt>request (Object)</dt> <dd>The request.</dd>
  145. * <dt>callback (Object)</dt> <dd>The callback object with the following properties:
  146. * <dl>
  147. * <dt>success (Function)</dt> <dd>Success handler.</dd>
  148. * <dt>failure (Function)</dt> <dd>Failure handler.</dd>
  149. * </dl>
  150. * </dd>
  151. * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
  152. * <dt>data (Object)</dt> <dd>Raw data.</dd>
  153. * <dt>response (Object)</dt> <dd>Normalized response object with the following properties:
  154. * <dl>
  155. * <dt>results (Object)</dt> <dd>Parsed results.</dd>
  156. * <dt>meta (Object)</dt> <dd>Parsed meta data.</dd>
  157. * <dt>error (Boolean)</dt> <dd>Error flag.</dd>
  158. * </dl>
  159. * </dd>
  160. * </dl>
  161. * @preventable _defResponseFn
  162. */
  163. this.publish("response", {defaultFn: Y.bind("_defResponseFn", this), queuable:true});
  164. /**
  165. * Fired when an error is encountered.
  166. *
  167. * @event error
  168. * @param e {Event.Facade} Event Facade with the following properties:
  169. * <dl>
  170. * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
  171. * <dt>request (Object)</dt> <dd>The request.</dd>
  172. * <dt>callback (Object)</dt> <dd>The callback object with the following properties:
  173. * <dl>
  174. * <dt>success (Function)</dt> <dd>Success handler.</dd>
  175. * <dt>failure (Function)</dt> <dd>Failure handler.</dd>
  176. * </dl>
  177. * </dd>
  178. * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
  179. * <dt>data (Object)</dt> <dd>Raw data.</dd>
  180. * <dt>response (Object)</dt> <dd>Normalized response object with the following properties:
  181. * <dl>
  182. * <dt>results (Object)</dt> <dd>Parsed results.</dd>
  183. * <dt>meta (Object)</dt> <dd>Parsed meta data.</dd>
  184. * <dt>error (Object)</dt> <dd>Error object.</dd>
  185. * </dl>
  186. * </dd>
  187. * </dl>
  188. */
  189. },
  190. /**
  191. * Manages request/response transaction. Must fire <code>response</code>
  192. * event when response is received. This method should be implemented by
  193. * subclasses to achieve more complex behavior such as accessing remote data.
  194. *
  195. * @method _defRequestFn
  196. * @param e {Event.Facade} Event Facadewith the following properties:
  197. * <dl>
  198. * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
  199. * <dt>request (Object)</dt> <dd>The request.</dd>
  200. * <dt>callback (Object)</dt> <dd>The callback object with the following properties:
  201. * <dl>
  202. * <dt>success (Function)</dt> <dd>Success handler.</dd>
  203. * <dt>failure (Function)</dt> <dd>Failure handler.</dd>
  204. * </dl>
  205. * </dd>
  206. * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
  207. * </dl>
  208. * @protected
  209. */
  210. _defRequestFn: function(e) {
  211. var data = this.get("source");
  212. // Problematic data
  213. if(LANG.isUndefined(data)) {
  214. e.error = new Error("Local source undefined");
  215. }
  216. if(e.error) {
  217. this.fire("error", e);
  218. }
  219. this.fire("data", Y.mix({data:data}, e));
  220. },
  221. /**
  222. * Normalizes raw data into a response that includes results and meta properties.
  223. *
  224. * @method _defDataFn
  225. * @param e {Event.Facade} Event Facade with the following properties:
  226. * <dl>
  227. * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
  228. * <dt>request (Object)</dt> <dd>The request.</dd>
  229. * <dt>callback (Object)</dt> <dd>The callback object with the following properties:
  230. * <dl>
  231. * <dt>success (Function)</dt> <dd>Success handler.</dd>
  232. * <dt>failure (Function)</dt> <dd>Failure handler.</dd>
  233. * </dl>
  234. * </dd>
  235. * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
  236. * <dt>data (Object)</dt> <dd>Raw data.</dd>
  237. * </dl>
  238. * @protected
  239. */
  240. _defDataFn: function(e) {
  241. var data = e.data,
  242. meta = e.meta,
  243. response = {
  244. results: (LANG.isArray(data)) ? data : [data],
  245. meta: (meta) ? meta : {}
  246. };
  247. this.fire("response", Y.mix({response: response}, e));
  248. },
  249. /**
  250. * Sends data as a normalized response to callback.
  251. *
  252. * @method _defResponseFn
  253. * @param e {Event.Facade} Event Facade with the following properties:
  254. * <dl>
  255. * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
  256. * <dt>request (Object)</dt> <dd>The request.</dd>
  257. * <dt>callback (Object)</dt> <dd>The callback object with the following properties:
  258. * <dl>
  259. * <dt>success (Function)</dt> <dd>Success handler.</dd>
  260. * <dt>failure (Function)</dt> <dd>Failure handler.</dd>
  261. * </dl>
  262. * </dd>
  263. * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
  264. * <dt>data (Object)</dt> <dd>Raw data.</dd>
  265. * <dt>response (Object)</dt> <dd>Normalized response object with the following properties:
  266. * <dl>
  267. * <dt>results (Object)</dt> <dd>Parsed results.</dd>
  268. * <dt>meta (Object)</dt> <dd>Parsed meta data.</dd>
  269. * <dt>error (Boolean)</dt> <dd>Error flag.</dd>
  270. * </dl>
  271. * </dd>
  272. * </dl>
  273. * @protected
  274. */
  275. _defResponseFn: function(e) {
  276. // Send the response back to the callback
  277. DSLocal.issueCallback(e);
  278. },
  279. /**
  280. * Generates a unique transaction ID and fires <code>request</code> event.
  281. *
  282. * @method sendRequest
  283. * @param request {Object} An object literal with the following properties:
  284. * <dl>
  285. * <dt><code>request</code></dt>
  286. * <dd>The request to send to the live data source, if any.</dd>
  287. * <dt><code>callback</code></dt>
  288. * <dd>An object literal with the following properties:
  289. * <dl>
  290. * <dt><code>success</code></dt>
  291. * <dd>The function to call when the data is ready.</dd>
  292. * <dt><code>failure</code></dt>
  293. * <dd>The function to call upon a response failure condition.</dd>
  294. * <dt><code>argument</code></dt>
  295. * <dd>Arbitrary data payload that will be passed back to the success and failure handlers.</dd>
  296. * </dl>
  297. * </dd>
  298. * <dt><code>cfg</code></dt>
  299. * <dd>Configuration object, if any.</dd>
  300. * </dl>
  301. * @return {Number} Transaction ID.
  302. */
  303. sendRequest: function(request) {
  304. request = request || {};
  305. var tId = DSLocal._tId++;
  306. this.fire("request", {tId:tId, request:request.request, callback:request.callback, cfg:request.cfg || {}});
  307. return tId;
  308. }
  309. });
  310. Y.namespace("DataSource").Local = DSLocal;
  311. }, '@VERSION@' ,{requires:['base']});