API Docs for: 3.18.1
Show:

File: datasource/js/datasource-cache.js

  1. /**
  2. * Plugs DataSource with caching functionality.
  3. *
  4. * @module datasource
  5. * @submodule datasource-cache
  6. */
  7.  
  8. /**
  9. * DataSourceCache extension binds Cache to DataSource.
  10. * @class DataSourceCacheExtension
  11. */
  12. var DataSourceCacheExtension = function() {
  13. };
  14.  
  15. Y.mix(DataSourceCacheExtension, {
  16. /**
  17. * The namespace for the plugin. This will be the property on the host which
  18. * references the plugin instance.
  19. *
  20. * @property NS
  21. * @type String
  22. * @static
  23. * @final
  24. * @value "cache"
  25. */
  26. NS: "cache",
  27.  
  28. /**
  29. * Class name.
  30. *
  31. * @property NAME
  32. * @type String
  33. * @static
  34. * @final
  35. * @value "dataSourceCacheExtension"
  36. */
  37. NAME: "dataSourceCacheExtension"
  38. });
  39.  
  40. DataSourceCacheExtension.prototype = {
  41. /**
  42. * Internal init() handler.
  43. *
  44. * @method initializer
  45. * @param config {Object} Config object.
  46. * @private
  47. */
  48. initializer: function(config) {
  49. this.doBefore("_defRequestFn", this._beforeDefRequestFn);
  50. this.doBefore("_defResponseFn", this._beforeDefResponseFn);
  51. },
  52.  
  53. /**
  54. * First look for cached response, then send request to live data.
  55. *
  56. * @method _beforeDefRequestFn
  57. * @param e {EventFacade} Event Facade with the following properties:
  58. * <dl>
  59. * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
  60. * <dt>request (Object)</dt> <dd>The request.</dd>
  61. * <dt>callback (Object)</dt> <dd>The callback object.</dd>
  62. * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
  63. * </dl>
  64. * @protected
  65. */
  66. _beforeDefRequestFn: function(e) {
  67. // Is response already in the Cache?
  68. var entry = (this.retrieve(e.request)) || null,
  69. payload = e.details[0];
  70.  
  71. if (entry && entry.response) {
  72. payload.cached = entry.cached;
  73. payload.response = entry.response;
  74. payload.data = entry.data;
  75.  
  76. this.get("host").fire("response", payload);
  77.  
  78. return new Y.Do.Halt("DataSourceCache extension halted _defRequestFn");
  79. }
  80. },
  81.  
  82. /**
  83. * Adds data to cache before returning data.
  84. *
  85. * @method _beforeDefResponseFn
  86. * @param e {EventFacade} Event Facade with the following properties:
  87. * <dl>
  88. * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
  89. * <dt>request (Object)</dt> <dd>The request.</dd>
  90. * <dt>callback (Object)</dt> <dd>The callback object with the following properties:
  91. * <dl>
  92. * <dt>success (Function)</dt> <dd>Success handler.</dd>
  93. * <dt>failure (Function)</dt> <dd>Failure handler.</dd>
  94. * </dl>
  95. * </dd>
  96. * <dt>data (Object)</dt> <dd>Raw data.</dd>
  97. * <dt>response (Object)</dt> <dd>Normalized response object with the following properties:
  98. * <dl>
  99. * <dt>cached (Object)</dt> <dd>True when response is cached.</dd>
  100. * <dt>results (Object)</dt> <dd>Parsed results.</dd>
  101. * <dt>meta (Object)</dt> <dd>Parsed meta data.</dd>
  102. * <dt>error (Object)</dt> <dd>Error object.</dd>
  103. * </dl>
  104. * </dd>
  105. * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
  106. * </dl>
  107. * @protected
  108. */
  109. _beforeDefResponseFn: function(e) {
  110. // Add to Cache before returning
  111. if(e.response && !e.cached) {
  112. this.add(e.request, e.response);
  113. }
  114. }
  115. };
  116.  
  117. Y.namespace("Plugin").DataSourceCacheExtension = DataSourceCacheExtension;
  118.  
  119.  
  120.  
  121. /**
  122. * DataSource plugin adds cache functionality.
  123. * @class DataSourceCache
  124. * @extends Cache
  125. * @uses Plugin.Base, DataSourceCachePlugin
  126. */
  127. function DataSourceCache(config) {
  128. var cache = config && config.cache ? config.cache : Y.Cache,
  129. tmpclass = Y.Base.create("dataSourceCache", cache, [Y.Plugin.Base, Y.Plugin.DataSourceCacheExtension]),
  130. tmpinstance = new tmpclass(config);
  131. tmpclass.NS = "tmpClass";
  132. return tmpinstance;
  133. }
  134.  
  135. Y.mix(DataSourceCache, {
  136. /**
  137. * The namespace for the plugin. This will be the property on the host which
  138. * references the plugin instance.
  139. *
  140. * @property NS
  141. * @type String
  142. * @static
  143. * @final
  144. * @value "cache"
  145. */
  146. NS: "cache",
  147.  
  148. /**
  149. * Class name.
  150. *
  151. * @property NAME
  152. * @type String
  153. * @static
  154. * @final
  155. * @value "dataSourceCache"
  156. */
  157. NAME: "dataSourceCache"
  158. });
  159.  
  160.  
  161. Y.namespace("Plugin").DataSourceCache = DataSourceCache;
  162.