API Docs for: 3.17.2
Show:

File: datasource/js/datasource-local.js

  1. /**
  2. * The DataSource utility provides a common configurable interface for widgets to
  3. * access a variety of data, from JavaScript arrays to online database servers.
  4. *
  5. * @module datasource
  6. * @main datasource
  7. */
  8.  
  9. /**
  10. * Provides the base DataSource implementation, which can be extended to
  11. * create DataSources for specific data protocols, such as the IO Utility, the
  12. * Get Utility, or custom functions.
  13. *
  14. * @module datasource
  15. * @submodule datasource-local
  16. */
  17.  
  18. /**
  19. * Base class for the DataSource Utility.
  20. * @class DataSource.Local
  21. * @extends Base
  22. * @constructor
  23. */
  24. var LANG = Y.Lang,
  25.  
  26. DSLocal = function() {
  27. DSLocal.superclass.constructor.apply(this, arguments);
  28. };
  29.  
  30. /////////////////////////////////////////////////////////////////////////////
  31. //
  32. // DataSource static properties
  33. //
  34. /////////////////////////////////////////////////////////////////////////////
  35. Y.mix(DSLocal, {
  36. /**
  37. * Class name.
  38. *
  39. * @property NAME
  40. * @type String
  41. * @static
  42. * @final
  43. * @value "dataSourceLocal"
  44. */
  45. NAME: "dataSourceLocal",
  46.  
  47. /////////////////////////////////////////////////////////////////////////////
  48. //
  49. // DataSource Attributes
  50. //
  51. /////////////////////////////////////////////////////////////////////////////
  52.  
  53. ATTRS: {
  54. /**
  55. * @attribute source
  56. * @description Pointer to live data.
  57. * @type MIXED
  58. * @default null
  59. */
  60. source: {
  61. value: null
  62. }
  63. },
  64.  
  65. /**
  66. * Global transaction counter.
  67. *
  68. * @property _tId
  69. * @type Number
  70. * @static
  71. * @private
  72. * @default 0
  73. */
  74. _tId: 0,
  75.  
  76. /**
  77. * Global in-progress transaction objects.
  78. *
  79. * @property transactions
  80. * @type Object
  81. * @static
  82. */
  83. transactions: {},
  84.  
  85. /**
  86. * Returns data to callback.
  87. *
  88. * @method issueCallback
  89. * @param e {EventFacade} Event Facade.
  90. * @param caller {DataSource} Calling DataSource instance.
  91. * @static
  92. */
  93. issueCallback: function (e, caller) {
  94. var callbacks = e.on || e.callback,
  95. callback = callbacks && callbacks.success,
  96. payload = e.details[0];
  97.  
  98. payload.error = (e.error || e.response.error);
  99.  
  100. if (payload.error) {
  101. caller.fire("error", payload);
  102. callback = callbacks && callbacks.failure;
  103. }
  104.  
  105. if (callback) {
  106. //TODO: this should be executed from a specific context
  107. callback(payload);
  108. }
  109. }
  110. });
  111.  
  112. Y.extend(DSLocal, Y.Base, {
  113. /**
  114. * Internal init() handler.
  115. *
  116. * @method initializer
  117. * @param config {Object} Config object.
  118. * @private
  119. */
  120. initializer: function(config) {
  121. this._initEvents();
  122. },
  123.  
  124. /**
  125. * This method creates all the events for this module.
  126. * @method _initEvents
  127. * @private
  128. */
  129. _initEvents: function() {
  130. /**
  131. * Fired when a data request is received.
  132. *
  133. * @event request
  134. * @param e {EventFacade} Event Facade with the following properties:
  135. * <dl>
  136. * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
  137. * <dt>request (Object)</dt> <dd>The request.</dd>
  138. * <dt>callback (Object)</dt> <dd>The callback object
  139. * (deprecated, refer to <strong>on</strong></dd>
  140. * <dt>on (Object)</dt> <dd>The map of configured callback
  141. * functions.</dd>
  142. * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
  143. * </dl>
  144. * @preventable _defRequestFn
  145. */
  146. this.publish("request", {defaultFn: Y.bind("_defRequestFn", this), queuable:true});
  147.  
  148. /**
  149. * Fired when raw data is received.
  150. *
  151. * @event data
  152. * @param e {EventFacade} Event Facade with the following properties:
  153. * <dl>
  154. * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
  155. * <dt>request (Object)</dt> <dd>The request.</dd>
  156. * <dt>callback (Object)</dt> <dd>Deprecated alias for the
  157. * <strong>on</strong> property</dd>
  158. * <dt>on (Object)</dt> <dd>The map of configured transaction
  159. * callbacks. An object with the following properties:
  160. * <dl>
  161. * <dt>success (Function)</dt> <dd>Success handler.</dd>
  162. * <dt>failure (Function)</dt> <dd>Failure handler.</dd>
  163. * </dl>
  164. * </dd>
  165. * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
  166. * <dt>data (Object)</dt> <dd>Raw data.</dd>
  167. * </dl>
  168. * @preventable _defDataFn
  169. */
  170. this.publish("data", {defaultFn: Y.bind("_defDataFn", this), queuable:true});
  171.  
  172. /**
  173. * Fired when response is returned.
  174. *
  175. * @event response
  176. * @param e {EventFacade} Event Facade with the following properties:
  177. * <dl>
  178. * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
  179. * <dt>request (Object)</dt> <dd>The request.</dd>
  180. * <dt>callback (Object)</dt> <dd>Deprecated alias for the
  181. * <strong>on</strong> property</dd>
  182. * <dt>on (Object)</dt> <dd>The map of configured transaction
  183. * callbacks. An object with the following properties:
  184. * <dl>
  185. * <dt>success (Function)</dt> <dd>Success handler.</dd>
  186. * <dt>failure (Function)</dt> <dd>Failure handler.</dd>
  187. * </dl>
  188. * </dd>
  189. * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
  190. * <dt>data (Object)</dt> <dd>Raw data.</dd>
  191. * <dt>response (Object)</dt>
  192. * <dd>Normalized response object with the following properties:
  193. * <dl>
  194. * <dt>results (Object)</dt> <dd>Parsed results.</dd>
  195. * <dt>meta (Object)</dt> <dd>Parsed meta data.</dd>
  196. * <dt>error (Boolean)</dt> <dd>Error flag.</dd>
  197. * </dl>
  198. * </dd>
  199. * <dt>error</dt>
  200. * <dd>Any error that occurred along the transaction lifecycle.</dd>
  201. * </dl>
  202. * @preventable _defResponseFn
  203. */
  204. this.publish("response", {defaultFn: Y.bind("_defResponseFn", this), queuable:true});
  205.  
  206. /**
  207. * Fired when an error is encountered.
  208. *
  209. * @event error
  210. * @param e {EventFacade} Event Facade with the following properties:
  211. * <dl>
  212. * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
  213. * <dt>request (Object)</dt> <dd>The request.</dd>
  214. * <dt>callback (Object)</dt> <dd>Deprecated alias for the
  215. * <strong>on</strong> property</dd>
  216. * <dt>on (Object)</dt> <dd>The map of configured transaction
  217. * callbacks. An object with the following properties:
  218. * <dl>
  219. * <dt>success (Function)</dt> <dd>Success handler.</dd>
  220. * <dt>failure (Function)</dt> <dd>Failure handler.</dd>
  221. * </dl>
  222. * </dd>
  223. * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
  224. * <dt>data (Object)</dt> <dd>Raw data.</dd>
  225. * <dt>response (Object)</dt>
  226. * <dd>Normalized response object with the following properties:
  227. * <dl>
  228. * <dt>results (Object)</dt> <dd>Parsed results.</dd>
  229. * <dt>meta (Object)</dt> <dd>Parsed meta data.</dd>
  230. * <dt>error (Object)</dt> <dd>Error object.</dd>
  231. * </dl>
  232. * </dd>
  233. * <dt>error</dt>
  234. * <dd>Any error that occurred along the transaction lifecycle.</dd>
  235. * </dl>
  236. */
  237.  
  238. },
  239.  
  240. /**
  241. * Manages request/response transaction. Must fire <code>response</code>
  242. * event when response is received. This method should be implemented by
  243. * subclasses to achieve more complex behavior such as accessing remote data.
  244. *
  245. * @method _defRequestFn
  246. * @param e {EventFacade} Event Facadewith the following properties:
  247. * <dl>
  248. * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
  249. * <dt>request (Object)</dt> <dd>The request.</dd>
  250. * <dt>callback (Object)</dt> <dd>Deprecated alias for the
  251. * <strong>on</strong> property</dd>
  252. * <dt>on (Object)</dt> <dd>The map of configured transaction
  253. * callbacks. An object with the following properties:
  254. * <dl>
  255. * <dt>success (Function)</dt> <dd>Success handler.</dd>
  256. * <dt>failure (Function)</dt> <dd>Failure handler.</dd>
  257. * </dl>
  258. * </dd>
  259. * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
  260. * </dl>
  261. * @protected
  262. */
  263. _defRequestFn: function(e) {
  264. var data = this.get("source"),
  265. payload = e.details[0];
  266.  
  267. // Problematic data
  268. if(LANG.isUndefined(data)) {
  269. payload.error = new Error("Local source undefined");
  270. Y.log("Local source undefined", "error", "datasource-local");
  271. }
  272.  
  273. payload.data = data;
  274. this.fire("data", payload);
  275. Y.log("Transaction " + e.tId + " complete. Request: " +
  276. Y.dump(e.request) + " . Response: " + Y.dump(e.response), "info", "datasource-local");
  277. },
  278.  
  279. /**
  280. * Normalizes raw data into a response that includes results and meta properties.
  281. *
  282. * @method _defDataFn
  283. * @param e {EventFacade} Event Facade with the following properties:
  284. * <dl>
  285. * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
  286. * <dt>request (Object)</dt> <dd>The request.</dd>
  287. * <dt>callback (Object)</dt> <dd>Deprecated alias for the
  288. * <strong>on</strong> property</dd>
  289. * <dt>on (Object)</dt> <dd>The map of configured transaction
  290. * callbacks. An object with the following properties:
  291. * <dl>
  292. * <dt>success (Function)</dt> <dd>Success handler.</dd>
  293. * <dt>failure (Function)</dt> <dd>Failure handler.</dd>
  294. * </dl>
  295. * </dd>
  296. * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
  297. * <dt>data (Object)</dt> <dd>Raw data.</dd>
  298. * </dl>
  299. * @protected
  300. */
  301. _defDataFn: function(e) {
  302. var data = e.data,
  303. meta = e.meta,
  304. response = {
  305. results: (LANG.isArray(data)) ? data : [data],
  306. meta: (meta) ? meta : {}
  307. },
  308. payload = e.details[0];
  309.  
  310. payload.response = response;
  311. this.fire("response", payload);
  312. },
  313.  
  314. /**
  315. * Sends data as a normalized response to callback.
  316. *
  317. * @method _defResponseFn
  318. * @param e {EventFacade} Event Facade with the following properties:
  319. * <dl>
  320. * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
  321. * <dt>request (Object)</dt> <dd>The request.</dd>
  322. * <dt>callback (Object)</dt> <dd>Deprecated alias for the
  323. * <strong>on</strong> property</dd>
  324. * <dt>on (Object)</dt> <dd>The map of configured transaction
  325. * callbacks. An object with the following properties:
  326. * <dl>
  327. * <dt>success (Function)</dt> <dd>Success handler.</dd>
  328. * <dt>failure (Function)</dt> <dd>Failure handler.</dd>
  329. * </dl>
  330. * </dd>
  331. * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
  332. * <dt>data (Object)</dt> <dd>Raw data.</dd>
  333. * <dt>response (Object)</dt> <dd>Normalized response object with the following properties:
  334. * <dl>
  335. * <dt>results (Object)</dt> <dd>Parsed results.</dd>
  336. * <dt>meta (Object)</dt> <dd>Parsed meta data.</dd>
  337. * <dt>error (Boolean)</dt> <dd>Error flag.</dd>
  338. * </dl>
  339. * </dd>
  340. * </dl>
  341. * @protected
  342. */
  343. _defResponseFn: function(e) {
  344. // Send the response back to the callback
  345. DSLocal.issueCallback(e, this);
  346. },
  347.  
  348. /**
  349. * Generates a unique transaction ID and fires <code>request</code> event.
  350. * <strong>Note</strong>: the property <code>callback</code> is a
  351. * deprecated alias for the <code>on</code> transaction configuration
  352. * property described below.
  353. *
  354. * @method sendRequest
  355. * @param [request] {Object} An object literal with the following properties:
  356. * <dl>
  357. * <dt><code>request</code></dt>
  358. * <dd>The request to send to the live data source, if any.</dd>
  359. * <dt><code>on</code></dt>
  360. * <dd>An object literal with the following properties:
  361. * <dl>
  362. * <dt><code>success</code></dt>
  363. * <dd>The function to call when the data is ready.</dd>
  364. * <dt><code>failure</code></dt>
  365. * <dd>The function to call upon a response failure condition.</dd>
  366. * <dt><code>argument</code></dt>
  367. * <dd>Arbitrary data payload that will be passed back to the success and failure handlers.</dd>
  368. * </dl>
  369. * </dd>
  370. * <dt><code>cfg</code></dt>
  371. * <dd>Configuration object, if any.</dd>
  372. * </dl>
  373. * @return {Number} Transaction ID.
  374. */
  375. sendRequest: function(request) {
  376. var tId = DSLocal._tId++,
  377. callbacks;
  378.  
  379. request = request || {};
  380.  
  381. callbacks = request.on || request.callback;
  382.  
  383. this.fire("request", {
  384. tId: tId,
  385. request: request.request,
  386. on: callbacks,
  387. callback: callbacks,
  388. cfg: request.cfg || {}
  389. });
  390.  
  391. Y.log("Transaction " + tId + " sent request: " + Y.dump(request.request), "info", "datasource-local");
  392.  
  393. return tId;
  394. }
  395. });
  396.  
  397. Y.namespace("DataSource").Local = DSLocal;
  398.