3
Copyright 2011 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
7
YUI.add('datasource-cache', function(Y) {
10
* Plugs DataSource with caching functionality.
13
* @submodule datasource-cache
17
* DataSourceCache extension binds Cache to DataSource.
18
* @class DataSourceCacheExtension
20
var DataSourceCacheExtension = function() {
23
Y.mix(DataSourceCacheExtension, {
25
* The namespace for the plugin. This will be the property on the host which
26
* references the plugin instance.
43
* @value "dataSourceCacheExtension"
45
NAME: "dataSourceCacheExtension"
48
DataSourceCacheExtension.prototype = {
50
* Internal init() handler.
53
* @param config {Object} Config object.
56
initializer: function(config) {
57
this.doBefore("_defRequestFn", this._beforeDefRequestFn);
58
this.doBefore("_defResponseFn", this._beforeDefResponseFn);
62
* First look for cached response, then send request to live data.
64
* @method _beforeDefRequestFn
65
* @param e {Event.Facade} Event Facade with the following properties:
67
* <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
68
* <dt>request (Object)</dt> <dd>The request.</dd>
69
* <dt>callback (Object)</dt> <dd>The callback object.</dd>
70
* <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
74
_beforeDefRequestFn: function(e) {
75
// Is response already in the Cache?
76
var entry = (this.retrieve(e.request)) || null,
77
payload = e.details[0];
79
if (entry && entry.response) {
80
payload.cached = entry.cached;
81
payload.response = entry.response;
82
payload.data = entry.data;
84
this.get("host").fire("response", payload);
86
return new Y.Do.Halt("DataSourceCache extension halted _defRequestFn");
91
* Adds data to cache before returning data.
93
* @method _beforeDefResponseFn
94
* @param e {Event.Facade} Event Facade with the following properties:
96
* <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
97
* <dt>request (Object)</dt> <dd>The request.</dd>
98
* <dt>callback (Object)</dt> <dd>The callback object with the following properties:
100
* <dt>success (Function)</dt> <dd>Success handler.</dd>
101
* <dt>failure (Function)</dt> <dd>Failure handler.</dd>
104
* <dt>data (Object)</dt> <dd>Raw data.</dd>
105
* <dt>response (Object)</dt> <dd>Normalized response object with the following properties:
107
* <dt>cached (Object)</dt> <dd>True when response is cached.</dd>
108
* <dt>results (Object)</dt> <dd>Parsed results.</dd>
109
* <dt>meta (Object)</dt> <dd>Parsed meta data.</dd>
110
* <dt>error (Object)</dt> <dd>Error object.</dd>
113
* <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
117
_beforeDefResponseFn: function(e) {
118
// Add to Cache before returning
119
if(e.response && !e.cached) {
120
this.add(e.request, e.response);
125
Y.namespace("Plugin").DataSourceCacheExtension = DataSourceCacheExtension;
130
* DataSource plugin adds cache functionality.
131
* @class DataSourceCache
133
* @uses Plugin.Base, DataSourceCachePlugin
135
function DataSourceCache(config) {
136
var cache = config && config.cache ? config.cache : Y.Cache,
137
tmpclass = Y.Base.create("dataSourceCache", cache, [Y.Plugin.Base, Y.Plugin.DataSourceCacheExtension]),
138
tmpinstance = new tmpclass(config);
139
tmpclass.NS = "tmpClass";
143
Y.mix(DataSourceCache, {
145
* The namespace for the plugin. This will be the property on the host which
146
* references the plugin instance.
163
* @value "dataSourceCache"
165
NAME: "dataSourceCache"
169
Y.namespace("Plugin").DataSourceCache = DataSourceCache;
172
}, '3.4.1' ,{requires:['datasource-local', 'cache-base', 'plugin']});