2
Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3
Code licensed under the BSD License:
4
http://developer.yahoo.com/yui/license.html
8
YUI.add('datasource-cache', function(Y) {
11
* Plugs DataSource with caching functionality.
14
* @submodule datasource-cache
18
* DataSourceCache extension binds Cache to DataSource.
19
* @class DataSourceCacheExtension
21
var DataSourceCacheExtension = function() {
24
Y.mix(DataSourceCacheExtension, {
26
* The namespace for the plugin. This will be the property on the host which
27
* references the plugin instance.
44
* @value "dataSourceCacheExtension"
46
NAME: "dataSourceCacheExtension"
49
DataSourceCacheExtension.prototype = {
51
* Internal init() handler.
54
* @param config {Object} Config object.
57
initializer: function(config) {
58
this.doBefore("_defRequestFn", this._beforeDefRequestFn);
59
this.doBefore("_defResponseFn", this._beforeDefResponseFn);
63
* First look for cached response, then send request to live data.
65
* @method _beforeDefRequestFn
66
* @param e {Event.Facade} Event Facade with the following properties:
68
* <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
69
* <dt>request (Object)</dt> <dd>The request.</dd>
70
* <dt>callback (Object)</dt> <dd>The callback object.</dd>
71
* <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
75
_beforeDefRequestFn: function(e) {
76
// Is response already in the Cache?
77
var entry = (this.retrieve(e.request)) || null;
78
if(entry && entry.response) {
79
this.get("host").fire("response", Y.mix(entry, e));
80
return new Y.Do.Halt("DataSourceCache extension halted _defRequestFn");
85
* Adds data to cache before returning data.
87
* @method _beforeDefResponseFn
88
* @param e {Event.Facade} Event Facade with the following properties:
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:
94
* <dt>success (Function)</dt> <dd>Success handler.</dd>
95
* <dt>failure (Function)</dt> <dd>Failure handler.</dd>
98
* <dt>data (Object)</dt> <dd>Raw data.</dd>
99
* <dt>response (Object)</dt> <dd>Normalized response object with the following properties:
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>
107
* <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
111
_beforeDefResponseFn: function(e) {
112
// Add to Cache before returning
113
if(e.response && !e.cached) {
114
this.add(e.request, e.response);
119
Y.namespace("Plugin").DataSourceCacheExtension = DataSourceCacheExtension;
124
* DataSource plugin adds cache functionality.
125
* @class DataSourceCache
127
* @uses Plugin.Base, DataSourceCachePlugin
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";
137
Y.mix(DataSourceCache, {
139
* The namespace for the plugin. This will be the property on the host which
140
* references the plugin instance.
157
* @value "dataSourceCache"
159
NAME: "dataSourceCache"
163
Y.namespace("Plugin").DataSourceCache = DataSourceCache;
167
}, '3.2.0' ,{requires:['datasource-local']});