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