<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Example: DataSource with Caching</title>
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic">
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css">
<link rel="stylesheet" href="../assets/css/main.css">
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png">
<script src="../../build/yui/yui-min.js"></script>
</head>
<body>
<!--
<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>
-->
<div id="doc">
<div id="hd">
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1>
</div>
<h1>Example: DataSource with Caching</h1>
<div class="yui3-g">
<div class="yui3-u-3-4">
<div id="main">
<div class="content"><style scoped>
/* custom styles for this example */
#demo .output {
margin-bottom:1em;
padding:10px;
border:1px solid #D9D9D9;
}
#demo .output pre {
font-size: 11px;
}
#demo .output strong {
padding: .25em .4em;
background: #333;
color: #fff;
text-shadow: -1px -1px 1px #000;
border-radius: 5px;
}
</style>
<div class="intro">
<p>The DataSourceCache plugin enables caching on any DataSource to reduce high-latency calls to remote sources and to reduce server load. In this example, the Cache's <code>max</code> value has been set to <code>3</code>.
</div>
<div class="example yui3-skin-sam">
<form id="demo" action="http://search.yahoo.com/search">
<h6>Look up github repositories by username (e.g., davglass, lsmith or rgrove):</h6>
<input type="input" id="demo_input_query" name="p">
<input type="submit" id="demo_query_retrieve" value="Retrieve data">
<input type="button" id="demo_cache_clear" value="Clear cache">
<div id="demo_output_response" class="output"></div>
</form>
<script type="text/javascript">
YUI().use("json-stringify","node", "datasource-get", "datasource-jsonschema", "datasource-cache", "datatype-date", function (Y) {
var output = Y.one("#demo_output_response"),
source = "remote source",
myDataSource = new Y.DataSource.Get({
source:"https://api.github.com/users/",
generateRequestCallback: function (guid) {
return '/repos?callback=YUI.Env.DataSource.callbacks.' + guid;
}
}),
callback = {
success: function(e){
var when = Y.DataType.Date.format(new Date(), {format:"%F %r"}),
data = Y.JSON.stringify(e.response, null, 2);
output.setHTML(
"<p>[" + when + "] Retrieved from " +
"<strong>" + source + "</strong></p>" +
"<pre>" +
data.replace(/&/g,"&")
.replace(/</g,"<")
.replace(/>/g,">") +
"</pre>");
},
failure: function(e){
var when = Y.DataType.Date.format(new Date(), {format:"%F %r"}),
message = /fields retrieval/.test(e.error.message) ?
"User not found" : e.error.message;
output.setHTML(
"<p>[" + when + "] Could not retrieve data: " +
"<em>" + message + "</em>" +
"</p>");
}
};
myDataSource.plug(Y.Plugin.DataSourceJSONSchema, {
schema: {
resultListLocator: "data",
resultFields: ["name"]
}
});
myDataSource.plug(Y.Plugin.DataSourceCache, { max: 3 });
myDataSource.cache.on("retrieve", function(){
source = "cache";
});
Y.one("#demo_cache_clear").on("click", function(){
var when = Y.DataType.Date.format(new Date(), {format:"%F %r"});
myDataSource.cache.flush();
output.setHTML("<p>[" + when + "] Cache cleared.</p>");
});
Y.on("submit", function(e){
e.halt();
var query = encodeURIComponent(
Y.one("#demo_input_query")
.get("value")
.replace(/"/g,'\\"')
.replace(/\W/g, ''));
if(query) {
source = "remote source";
myDataSource.sendRequest({
request:query,
callback:callback
});
} else {
output.setHTML("<p>Please enter a query.</p>");
}
}, "#demo");
});
</script>
</div>
<p>Use the <code>plug()</code> method to initialize the
<code>DataSourceCache</code> plugin and pass in the configuration value
<code>max</code> to set the maximum size.</p>
<pre class="code prettyprint">YUI().use("datasource", "dataschema", "cache", function (Y) {
var callback = {
success: function (e) { /* output to screen */ },
failure: function (e) { /* output to screen */ }
},
myDataSource = new Y.DataSource.Get({
source: "https://api.github.com/users/",
// this is only needed because the query appends the url
// rather than the url's query params
generateRequestCallback: function (guid) {
return '/repos?callback=YUI.Env.DataSource.callbacks.' + guid;
}
}),
myDataSource.plug(Y.Plugin.DataSourceJSONSchema, {
schema: {
resultListLocator: "data",
resultFields: ["name"]
}
});
myDataSource.plug(Y.Plugin.DataSourceCache, { max: 3 });
// Adds to cache
myDataSource.sendRequest({
request : "lsmith",
callback: callback
});
// Adds to cache
myDataSource.sendRequest({
request : "davglass",
callback: callback
});
// Retrieves from cache
myDataSource.sendRequest({
request : "lsmith",
callback: callback
});
});</pre>
<h3 id="fullsource">Full Example Source Listing</h3>
<pre class="code prettyprint"><form id="demo" action="http://search.yahoo.com/search">
<h6>Look up github repositories by username (e.g., davglass, lsmith or rgrove):</h6>
<input type="input" id="demo_input_query" name="p">
<input type="submit" id="demo_query_retrieve" value="Retrieve data">
<input type="button" id="demo_cache_clear" value="Clear cache">
<div id="demo_output_response" class="output"></div>
</form>
<script type="text/javascript">
YUI().use("json-stringify","node", "datasource-get", "datasource-jsonschema", "datasource-cache", "datatype-date", function (Y) {
var output = Y.one("#demo_output_response"),
source = "remote source",
myDataSource = new Y.DataSource.Get({
source:"https://api.github.com/users/",
generateRequestCallback: function (guid) {
return '/repos?callback=YUI.Env.DataSource.callbacks.' + guid;
}
}),
callback = {
success: function(e){
var when = Y.DataType.Date.format(new Date(), {format:"%F %r"}),
data = Y.JSON.stringify(e.response, null, 2);
output.setHTML(
"<p>[" + when + "] Retrieved from " +
"<strong>" + source + "</strong></p>" +
"<pre>" +
data.replace(/&/g,"&amp;")
.replace(/</g,"&lt;")
.replace(/>/g,"&gt;") +
"</pre>");
},
failure: function(e){
var when = Y.DataType.Date.format(new Date(), {format:"%F %r"}),
message = /fields retrieval/.test(e.error.message) ?
"User not found" : e.error.message;
output.setHTML(
"<p>[" + when + "] Could not retrieve data: " +
"<em>" + message + "</em>" +
"</p>");
}
};
myDataSource.plug(Y.Plugin.DataSourceJSONSchema, {
schema: {
resultListLocator: "data",
resultFields: ["name"]
}
});
myDataSource.plug(Y.Plugin.DataSourceCache, { max: 3 });
myDataSource.cache.on("retrieve", function(){
source = "cache";
});
Y.one("#demo_cache_clear").on("click", function(){
var when = Y.DataType.Date.format(new Date(), {format:"%F %r"});
myDataSource.cache.flush();
output.setHTML("<p>[" + when + "] Cache cleared.</p>");
});
Y.on("submit", function(e){
e.halt();
var query = encodeURIComponent(
Y.one("#demo_input_query")
.get("value")
.replace(/"/g,'\\"')
.replace(/\W/g, ''));
if(query) {
source = "remote source";
myDataSource.sendRequest({
request:query,
callback:callback
});
} else {
output.setHTML("<p>Please enter a query.</p>");
}
}, "#demo");
});
</script></pre>
</div>
</div>
</div>
<div class="yui3-u-1-4">
<div class="sidebar">
<div class="sidebox">
<div class="hd">
<h2 class="no-toc">Examples</h2>
</div>
<div class="bd">
<ul class="examples">
<li data-description="The Local DataSource manages retrieval of in-page data, from JavaScript arrays and objects to DOM elements.">
<a href="datasource-local.html">DataSource.Local</a>
</li>
<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.">
<a href="datasource-get.html">DataSource.Get</a>
</li>
<li data-description="The IO DataSource manages retrieval of data from remote sources, via the IO Utility.">
<a href="datasource-io.html">DataSource.IO</a>
</li>
<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">
<a href="datasource-function.html">DataSource.Function</a>
</li>
<li data-description="Use the DataSourceCache plugin to enable caching and reduce server calls to remote sources.">
<a href="datasource-caching.html">DataSource with Caching</a>
</li>
<li data-description="The DataSourceCache plugin supports offline caching so that cached data persists across browser sessions.">
<a href="datasource-offlinecache.html">DataSource with Offline Cache</a>
</li>
<li data-description="Use the Pollable extension to enable polling in your DataSource.">
<a href="datasource-polling.html">DataSource with Polling</a>
</li>
</ul>
</div>
</div>
<div class="sidebox">
<div class="hd">
<h2 class="no-toc">Examples That Use This Component</h2>
</div>
<div class="bd">
<ul class="examples">
<li data-description="How to provide autocomplete suggestions using a DataSource instance.">
<a href="../autocomplete/ac-datasource.html">Remote Data via DataSource</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>
<script>
YUI.Env.Tests = {
examples: [],
project: '../assets',
assets: '../assets/datasource',
name: 'datasource-caching',
title: 'DataSource with Caching',
newWindow: '',
auto: false
};
YUI.Env.Tests.examples.push('datasource-local');
YUI.Env.Tests.examples.push('datasource-get');
YUI.Env.Tests.examples.push('datasource-io');
YUI.Env.Tests.examples.push('datasource-function');
YUI.Env.Tests.examples.push('datasource-caching');
YUI.Env.Tests.examples.push('datasource-offlinecache');
YUI.Env.Tests.examples.push('datasource-polling');
YUI.Env.Tests.examples.push('ac-datasource');
</script>
<script src="../assets/yui/test-runner.js"></script>
</body>
</html>