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