|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Example: DataSource with Offline Cache</title> |
|
|
6 |
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic"> |
|
|
7 |
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css"> |
|
|
8 |
<link rel="stylesheet" href="../assets/css/main.css"> |
|
|
9 |
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css"> |
|
|
10 |
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png"> |
|
|
11 |
<script src="../../build/yui/yui-min.js"></script> |
|
|
12 |
|
|
|
13 |
</head> |
|
|
14 |
<body> |
|
|
15 |
<!-- |
|
|
16 |
<a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a> |
|
|
17 |
--> |
|
|
18 |
<div id="doc"> |
|
|
19 |
<div id="hd"> |
|
|
20 |
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1> |
|
|
21 |
</div> |
|
|
22 |
|
|
|
23 |
|
|
|
24 |
<h1>Example: DataSource with Offline Cache</h1> |
|
|
25 |
<div class="yui3-g"> |
|
|
26 |
<div class="yui3-u-3-4"> |
|
|
27 |
<div id="main"> |
|
|
28 |
<div class="content"><style scoped> |
|
|
29 |
#demo .output { |
|
|
30 |
margin-bottom:1em; |
|
|
31 |
padding:10px; |
|
|
32 |
border:1px solid #D9D9D9; |
|
|
33 |
} |
|
|
34 |
#demo .output pre { |
|
|
35 |
font-size: 11px; |
|
|
36 |
} |
|
|
37 |
#demo .output strong { |
|
|
38 |
padding: .25em .4em; |
|
|
39 |
background: #333; |
|
|
40 |
color: #fff; |
|
|
41 |
text-shadow: -1px -1px 1px #000; |
|
|
42 |
border-radius: 5px; |
|
|
43 |
} |
|
|
44 |
</style> |
|
|
45 |
|
|
|
46 |
<div class="intro"> |
|
|
47 |
<p>The DataSourceCache plugin enables caching on any DataSource to reduce high-latency calls to remote sources and to reduce server load. The plugin can point to <code>Y.OfflineCache</code> to allow cached data to persist across browser sessions in browsers that support <a href="http://dev.w3.org/html5/webstorage/" title="Web Storage">HTML5 localStorage</a>.</p> |
|
|
48 |
|
|
|
49 |
</div> |
|
|
50 |
|
|
|
51 |
<div class="example yui3-skin-sam"> |
|
|
52 |
<form id="demo" action="http://search.yahoo.com/search"> |
|
|
53 |
<h6>Look up github repositories by username (ex. lsmith, davglass, or rgrove):</h6> |
|
|
54 |
<input type="input" id="demo_input_query" name="p"> |
|
|
55 |
<input type="submit" id="demo_query_retrieve" value="Retrieve data"> |
|
|
56 |
<input type="button" id="demo_cache_clear" value="Clear cache"> |
|
|
57 |
<div id="demo_output_response" class="output"></div> |
|
|
58 |
</form> |
|
|
59 |
|
|
|
60 |
|
|
|
61 |
|
|
|
62 |
<script type="text/javascript"> |
|
|
63 |
YUI().use("json-stringify","node", "datasource-get", "datasource-jsonschema", "cache", "datasource-cache", "datatype-date", function (Y) { |
|
|
64 |
var output = Y.one("#demo_output_response"), |
|
|
65 |
source = "remote source", |
|
|
66 |
|
|
|
67 |
myDataSource = new Y.DataSource.Get({ |
|
|
68 |
source:"https://api.github.com/users/", |
|
|
69 |
generateRequestCallback: function (guid) { |
|
|
70 |
return '/repos?callback=YUI.Env.DataSource.callbacks.' + guid; |
|
|
71 |
} |
|
|
72 |
}), |
|
|
73 |
|
|
|
74 |
callback = { |
|
|
75 |
success: function(e){ |
|
|
76 |
var when = Y.DataType.Date.format(new Date(), {format:"%F %r"}), |
|
|
77 |
data = Y.JSON.stringify(e.response, null, 2); |
|
|
78 |
|
|
|
79 |
output.setHTML( |
|
|
80 |
"<p>[" + when + "] Retrieved from " + |
|
|
81 |
"<strong>" + source + "</strong></p>" + |
|
|
82 |
"<pre>" + |
|
|
83 |
data.replace(/&/g,"&") |
|
|
84 |
.replace(/</g,"<") |
|
|
85 |
.replace(/>/g,">") + |
|
|
86 |
"</pre>"); |
|
|
87 |
}, |
|
|
88 |
failure: function(e){ |
|
|
89 |
var when = Y.DataType.Date.format(new Date(), {format:"%F %r"}), |
|
|
90 |
message = /fields retrieval/.test(e.error.message) ? |
|
|
91 |
"User not found" : e.error.message; |
|
|
92 |
|
|
|
93 |
output.setHTML( |
|
|
94 |
"<p>[" + when + "] Could not retrieve data: " + |
|
|
95 |
"<em>" + message + "</em>" + |
|
|
96 |
"</p>"); |
|
|
97 |
} |
|
|
98 |
}; |
|
|
99 |
|
|
|
100 |
myDataSource.plug(Y.Plugin.DataSourceJSONSchema, { |
|
|
101 |
schema: { |
|
|
102 |
resultListLocator: "data", |
|
|
103 |
resultFields: ["name"] |
|
|
104 |
} |
|
|
105 |
}); |
|
|
106 |
|
|
|
107 |
myDataSource.plug(Y.Plugin.DataSourceCache, { |
|
|
108 |
cache: Y.CacheOffline, |
|
|
109 |
expires: 60000, // 1 min. |
|
|
110 |
max:5 |
|
|
111 |
}); |
|
|
112 |
myDataSource.cache.on("retrieve", function(){ |
|
|
113 |
source = "cache"; |
|
|
114 |
}); |
|
|
115 |
|
|
|
116 |
Y.one("#demo_cache_clear").on("click", function(){ |
|
|
117 |
var when = Y.DataType.Date.format(new Date(), {format:"%F %r"}); |
|
|
118 |
|
|
|
119 |
myDataSource.cache.flush(); |
|
|
120 |
output.setHTML("<p>[" + when + "] Cache cleared.</p>"); |
|
|
121 |
}); |
|
|
122 |
|
|
|
123 |
Y.on("submit", function(e){ |
|
|
124 |
e.halt(); |
|
|
125 |
var query = encodeURIComponent( |
|
|
126 |
Y.one("#demo_input_query") |
|
|
127 |
.get("value") |
|
|
128 |
.replace(/"/g,'\\"') |
|
|
129 |
.replace(/\W/g, '')); |
|
|
130 |
|
|
|
131 |
if(query) { |
|
|
132 |
source = "remote source"; |
|
|
133 |
myDataSource.sendRequest({ |
|
|
134 |
request:query, |
|
|
135 |
callback:callback |
|
|
136 |
}); |
|
|
137 |
} else { |
|
|
138 |
output.setHTML("<p>Please enter a query.</p>"); |
|
|
139 |
} |
|
|
140 |
}, "#demo"); |
|
|
141 |
}); |
|
|
142 |
</script> |
|
|
143 |
|
|
|
144 |
</div> |
|
|
145 |
|
|
|
146 |
<p>Use the <code>plug()</code> method to initialize the DataSourceCache plugin |
|
|
147 |
and point the configuration value <code>cache</code> to Y.CacheOffline to enable |
|
|
148 |
offline caching. The configuration value <code>expires</code> can be set to |
|
|
149 |
change how soon the data expires.</p> |
|
|
150 |
|
|
|
151 |
<pre class="code prettyprint">YUI().use("datasource", "dataschema", "cache", function(Y) { |
|
|
152 |
var callback = { |
|
|
153 |
success: function (e) { /* output to screen */ }, |
|
|
154 |
failure: function (e) { /* output to screen */ } |
|
|
155 |
}, |
|
|
156 |
|
|
|
157 |
myDataSource = new Y.DataSource.Get({ |
|
|
158 |
source: "https://api.github.com/users/", |
|
|
159 |
|
|
|
160 |
// this is only needed because the query appends the url |
|
|
161 |
// rather than the url's query params |
|
|
162 |
generateRequestCallback: function (guid) { |
|
|
163 |
return '/repos?callback=YUI.Env.DataSource.callbacks.' + guid; |
|
|
164 |
} |
|
|
165 |
}), |
|
|
166 |
|
|
|
167 |
myDataSource.plug(Y.Plugin.DataSourceJSONSchema, { |
|
|
168 |
schema: { |
|
|
169 |
resultListLocator: "data", |
|
|
170 |
resultFields: ["name"] |
|
|
171 |
} |
|
|
172 |
}); |
|
|
173 |
|
|
|
174 |
// Enable offline data caching by pointing to Y.CacheOffline. |
|
|
175 |
// For demonstration purposes, data is set to expire after 10 seconds |
|
|
176 |
myDataSource.plug(Y.Plugin.DataSourceCache, { |
|
|
177 |
cache: Y.CacheOffline, |
|
|
178 |
expires: 60000, // 1 min. |
|
|
179 |
max: 5 |
|
|
180 |
}); |
|
|
181 |
|
|
|
182 |
// Retrieves from server. Adds to cache |
|
|
183 |
myDataSource.sendRequest({ |
|
|
184 |
request : "lsmith", |
|
|
185 |
callback: callback |
|
|
186 |
}); |
|
|
187 |
|
|
|
188 |
// Retrieves from server. Adds to cache |
|
|
189 |
myDataSource.sendRequest({ |
|
|
190 |
request : "davglass", |
|
|
191 |
callback: callback |
|
|
192 |
}); |
|
|
193 |
|
|
|
194 |
// Retrieves from cache if requested within 5 seconds |
|
|
195 |
myDataSource.sendRequest({ |
|
|
196 |
request : "lsmith", |
|
|
197 |
callback: callback |
|
|
198 |
}); |
|
|
199 |
|
|
|
200 |
// ... wait 60 seconds ... |
|
|
201 |
|
|
|
202 |
// Cached data has expired. Retrieves from server. Adds to cache. |
|
|
203 |
myDataSource.sendRequest({ |
|
|
204 |
request : "davglass", |
|
|
205 |
callback: callback |
|
|
206 |
}); |
|
|
207 |
});</pre> |
|
|
208 |
|
|
|
209 |
|
|
|
210 |
<h3 id="fullsource">Full Example Source Listing</h3> |
|
|
211 |
|
|
|
212 |
<pre class="code prettyprint"><form id="demo" action="http://search.yahoo.com/search"> |
|
|
213 |
<h6>Look up github repositories by username (ex. lsmith, davglass, or rgrove):</h6> |
|
|
214 |
<input type="input" id="demo_input_query" name="p"> |
|
|
215 |
<input type="submit" id="demo_query_retrieve" value="Retrieve data"> |
|
|
216 |
<input type="button" id="demo_cache_clear" value="Clear cache"> |
|
|
217 |
<div id="demo_output_response" class="output"></div> |
|
|
218 |
</form> |
|
|
219 |
|
|
|
220 |
|
|
|
221 |
|
|
|
222 |
<script type="text/javascript"> |
|
|
223 |
YUI().use("json-stringify","node", "datasource-get", "datasource-jsonschema", "cache", "datasource-cache", "datatype-date", function (Y) { |
|
|
224 |
var output = Y.one("#demo_output_response"), |
|
|
225 |
source = "remote source", |
|
|
226 |
|
|
|
227 |
myDataSource = new Y.DataSource.Get({ |
|
|
228 |
source:"https://api.github.com/users/", |
|
|
229 |
generateRequestCallback: function (guid) { |
|
|
230 |
return '/repos?callback=YUI.Env.DataSource.callbacks.' + guid; |
|
|
231 |
} |
|
|
232 |
}), |
|
|
233 |
|
|
|
234 |
callback = { |
|
|
235 |
success: function(e){ |
|
|
236 |
var when = Y.DataType.Date.format(new Date(), {format:"%F %r"}), |
|
|
237 |
data = Y.JSON.stringify(e.response, null, 2); |
|
|
238 |
|
|
|
239 |
output.setHTML( |
|
|
240 |
"<p>[" + when + "] Retrieved from " + |
|
|
241 |
"<strong>" + source + "</strong></p>" + |
|
|
242 |
"<pre>" + |
|
|
243 |
data.replace(/&/g,"&amp;") |
|
|
244 |
.replace(/</g,"&lt;") |
|
|
245 |
.replace(/>/g,"&gt;") + |
|
|
246 |
"</pre>"); |
|
|
247 |
}, |
|
|
248 |
failure: function(e){ |
|
|
249 |
var when = Y.DataType.Date.format(new Date(), {format:"%F %r"}), |
|
|
250 |
message = /fields retrieval/.test(e.error.message) ? |
|
|
251 |
"User not found" : e.error.message; |
|
|
252 |
|
|
|
253 |
output.setHTML( |
|
|
254 |
"<p>[" + when + "] Could not retrieve data: " + |
|
|
255 |
"<em>" + message + "</em>" + |
|
|
256 |
"</p>"); |
|
|
257 |
} |
|
|
258 |
}; |
|
|
259 |
|
|
|
260 |
myDataSource.plug(Y.Plugin.DataSourceJSONSchema, { |
|
|
261 |
schema: { |
|
|
262 |
resultListLocator: "data", |
|
|
263 |
resultFields: ["name"] |
|
|
264 |
} |
|
|
265 |
}); |
|
|
266 |
|
|
|
267 |
myDataSource.plug(Y.Plugin.DataSourceCache, { |
|
|
268 |
cache: Y.CacheOffline, |
|
|
269 |
expires: 60000, // 1 min. |
|
|
270 |
max:5 |
|
|
271 |
}); |
|
|
272 |
myDataSource.cache.on("retrieve", function(){ |
|
|
273 |
source = "cache"; |
|
|
274 |
}); |
|
|
275 |
|
|
|
276 |
Y.one("#demo_cache_clear").on("click", function(){ |
|
|
277 |
var when = Y.DataType.Date.format(new Date(), {format:"%F %r"}); |
|
|
278 |
|
|
|
279 |
myDataSource.cache.flush(); |
|
|
280 |
output.setHTML("<p>[" + when + "] Cache cleared.</p>"); |
|
|
281 |
}); |
|
|
282 |
|
|
|
283 |
Y.on("submit", function(e){ |
|
|
284 |
e.halt(); |
|
|
285 |
var query = encodeURIComponent( |
|
|
286 |
Y.one("#demo_input_query") |
|
|
287 |
.get("value") |
|
|
288 |
.replace(/"/g,'\\"') |
|
|
289 |
.replace(/\W/g, '')); |
|
|
290 |
|
|
|
291 |
if(query) { |
|
|
292 |
source = "remote source"; |
|
|
293 |
myDataSource.sendRequest({ |
|
|
294 |
request:query, |
|
|
295 |
callback:callback |
|
|
296 |
}); |
|
|
297 |
} else { |
|
|
298 |
output.setHTML("<p>Please enter a query.</p>"); |
|
|
299 |
} |
|
|
300 |
}, "#demo"); |
|
|
301 |
}); |
|
|
302 |
</script></pre> |
|
|
303 |
|
|
|
304 |
</div> |
|
|
305 |
</div> |
|
|
306 |
</div> |
|
|
307 |
|
|
|
308 |
<div class="yui3-u-1-4"> |
|
|
309 |
<div class="sidebar"> |
|
|
310 |
|
|
|
311 |
|
|
|
312 |
|
|
|
313 |
<div class="sidebox"> |
|
|
314 |
<div class="hd"> |
|
|
315 |
<h2 class="no-toc">Examples</h2> |
|
|
316 |
</div> |
|
|
317 |
|
|
|
318 |
<div class="bd"> |
|
|
319 |
<ul class="examples"> |
|
|
320 |
|
|
|
321 |
|
|
|
322 |
<li data-description="The Local DataSource manages retrieval of in-page data, from JavaScript arrays and objects to DOM elements."> |
|
|
323 |
<a href="datasource-local.html">DataSource.Local</a> |
|
|
324 |
</li> |
|
|
325 |
|
|
|
326 |
|
|
|
327 |
|
|
|
328 |
<li data-description="The Get DataSource, which manages retrieval of data from remote sources via the Get Utility, can be useful for accessing data from cross-domain servers without the need for a proxy."> |
|
|
329 |
<a href="datasource-get.html">DataSource.Get</a> |
|
|
330 |
</li> |
|
|
331 |
|
|
|
332 |
|
|
|
333 |
|
|
|
334 |
<li data-description="The IO DataSource manages retrieval of data from remote sources, via the IO Utility."> |
|
|
335 |
<a href="datasource-io.html">DataSource.IO</a> |
|
|
336 |
</li> |
|
|
337 |
|
|
|
338 |
|
|
|
339 |
|
|
|
340 |
<li data-description="The Function DataSource, which manages retrieval of data from a JavaScript function, provides a highly customizeable mechanism for implementer-defined data retrieval algorithms"> |
|
|
341 |
<a href="datasource-function.html">DataSource.Function</a> |
|
|
342 |
</li> |
|
|
343 |
|
|
|
344 |
|
|
|
345 |
|
|
|
346 |
<li data-description="Use the DataSourceCache plugin to enable caching and reduce server calls to remote sources."> |
|
|
347 |
<a href="datasource-caching.html">DataSource with Caching</a> |
|
|
348 |
</li> |
|
|
349 |
|
|
|
350 |
|
|
|
351 |
|
|
|
352 |
<li data-description="The DataSourceCache plugin supports offline caching so that cached data persists across browser sessions."> |
|
|
353 |
<a href="datasource-offlinecache.html">DataSource with Offline Cache</a> |
|
|
354 |
</li> |
|
|
355 |
|
|
|
356 |
|
|
|
357 |
|
|
|
358 |
<li data-description="Use the Pollable extension to enable polling in your DataSource."> |
|
|
359 |
<a href="datasource-polling.html">DataSource with Polling</a> |
|
|
360 |
</li> |
|
|
361 |
|
|
|
362 |
|
|
|
363 |
|
|
|
364 |
|
|
|
365 |
</ul> |
|
|
366 |
</div> |
|
|
367 |
</div> |
|
|
368 |
|
|
|
369 |
|
|
|
370 |
|
|
|
371 |
<div class="sidebox"> |
|
|
372 |
<div class="hd"> |
|
|
373 |
<h2 class="no-toc">Examples That Use This Component</h2> |
|
|
374 |
</div> |
|
|
375 |
|
|
|
376 |
<div class="bd"> |
|
|
377 |
<ul class="examples"> |
|
|
378 |
|
|
|
379 |
|
|
|
380 |
|
|
|
381 |
|
|
|
382 |
|
|
|
383 |
|
|
|
384 |
|
|
|
385 |
|
|
|
386 |
|
|
|
387 |
|
|
|
388 |
|
|
|
389 |
|
|
|
390 |
|
|
|
391 |
|
|
|
392 |
|
|
|
393 |
|
|
|
394 |
<li data-description="How to provide autocomplete suggestions using a DataSource instance."> |
|
|
395 |
<a href="../autocomplete/ac-datasource.html">Remote Data via DataSource</a> |
|
|
396 |
</li> |
|
|
397 |
|
|
|
398 |
|
|
|
399 |
</ul> |
|
|
400 |
</div> |
|
|
401 |
</div> |
|
|
402 |
|
|
|
403 |
</div> |
|
|
404 |
</div> |
|
|
405 |
</div> |
|
|
406 |
</div> |
|
|
407 |
|
|
|
408 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
409 |
<script>prettyPrint();</script> |
|
|
410 |
|
|
|
411 |
<script> |
|
|
412 |
YUI.Env.Tests = { |
|
|
413 |
examples: [], |
|
|
414 |
project: '../assets', |
|
|
415 |
assets: '../assets/datasource', |
|
|
416 |
name: 'datasource-offlinecache', |
|
|
417 |
title: 'DataSource with Offline Cache', |
|
|
418 |
newWindow: '', |
|
|
419 |
auto: false |
|
|
420 |
}; |
|
|
421 |
YUI.Env.Tests.examples.push('datasource-local'); |
|
|
422 |
YUI.Env.Tests.examples.push('datasource-get'); |
|
|
423 |
YUI.Env.Tests.examples.push('datasource-io'); |
|
|
424 |
YUI.Env.Tests.examples.push('datasource-function'); |
|
|
425 |
YUI.Env.Tests.examples.push('datasource-caching'); |
|
|
426 |
YUI.Env.Tests.examples.push('datasource-offlinecache'); |
|
|
427 |
YUI.Env.Tests.examples.push('datasource-polling'); |
|
|
428 |
YUI.Env.Tests.examples.push('ac-datasource'); |
|
|
429 |
|
|
|
430 |
</script> |
|
|
431 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
432 |
|
|
|
433 |
|
|
|
434 |
|
|
|
435 |
</body> |
|
|
436 |
</html> |