5
<title>Example: DataSource.Get</title>
6
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Maven+Pro:400,700">
7
<link rel="stylesheet" href="../../build/cssgrids/grids-min.css">
8
<link rel="stylesheet" href="../assets/css/main.css">
9
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
10
<script src="../../build/yui/yui-min.js"></script>
15
<h1>Example: DataSource.Get</h1>
20
<div class="yui3-u-3-4">
22
<div class="content"><style scoped>
23
/* custom styles for this example */
24
#demo .output {margin-bottom:1em; padding:10px; border:1px solid #D9D9D9;}
28
<p>DataSource.Get uses the Get Utility to retrieve data, even cross-domain resources, via a dynamically created script node. A <a href="../dataschema/">DataSchema</a> plugin is used to normalize incoming data into a known format for consistency of usage by other components. Please note that your data resource must support a callback mechanism, which is a function wrapper around the returned data. The name of the callback function is passed to the resource via a query string parameter defined by the DataSource.Get attribute <code>scriptCallbackParam</code>.</p>
31
<div class="example yui3-skin-sam">
39
"created":"2011-06-15T04:36:13Z",
45
"name": "PGA Fall Expo 2011",
46
"start_date": "2011-08-01",
47
"venue_name": "The Venetian Congress and Sands Expo Convention Center",
52
"name": "NAHC 30th Annual Meeting & Exposition",
53
"start_date": "2011-10-01",
54
"venue_name": "Mandalay Bay Resort",
59
"name": "Water and Society 2011-First International Conference on Water and Society",
60
"start_date": "2011-12-05",
61
"venue_name": "Richard Tam Alumni Center",
74
resultListLocator: "query.results.event",
75
resultFields: ["name"]
79
<h6>Normalized data</h6>
80
<input type="button" id="demo_json" value="Retrieve data">
81
<div id="demo_output_json" class="output"></div>
84
<script type="text/javascript">
85
YUI().use("json-stringify", "node", "datasource-get", "datasource-jsonschema", function (Y) {
86
var myDataSource = new Y.DataSource.Get({
87
source: "http://query.yahooapis.com/v1/public/yql?format=json&"
90
myDataSource.plug(Y.Plugin.DataSourceJSONSchema, {
92
resultListLocator: "query.results.event",
93
resultFields: ["name"]
97
Y.on("click", function(e){
98
// See what's happening in Las Vegas (woeid 2436704)
99
myDataSource.sendRequest({
100
request: "q=select%20*%20from%20upcoming.events.bestinplace%20where%20woeid=2436704",
102
success: function(e){
103
Y.one("#demo_output_json")
104
.setContent("<pre>" +
105
Y.JSON.stringify(e.response, null, 4)
106
.replace(/&/g,"&")
107
.replace(/</g,"<")
108
.replace(/>/g,">") +
111
failure: function(e){
112
Y.one("#demo_output_json")
113
.setContent("Could not retrieve data: " + e.error.message);
124
<p>Use a DataSourceJSONSchema plugin to parse the data against a schema that you provide:</p>
126
<pre class="code prettyprint">YUI().use("datasource-get", "datasource-jsonschema", function(Y) {
127
var myDataSource = new Y.DataSource.Get({
128
source: "http://query.yahooapis.com/v1/public/yql?format=json&"
131
myDataSource.plug(Y.Plugin.DataSourceJSONSchema, {
133
resultListLocator: "query.results.result",
134
resultFields: ["title"]
138
myDataSource.sendRequest({
139
request: "q=select%20*%20from%20upcoming.events.bestinplace...",
141
success: function (e) { /* output to screen */ },
142
failure: function (e) { /* output to screen */ }
153
<div class="yui3-u-1-4">
154
<div class="sidebar">
158
<div class="sidebox">
160
<h2 class="no-toc">Examples</h2>
164
<ul class="examples">
167
<li data-description="The Local DataSource manages retrieval of in-page data, from JavaScript arrays and objects to DOM elements.">
168
<a href="datasource-local.html">DataSource.Local</a>
173
<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.">
174
<a href="datasource-get.html">DataSource.Get</a>
179
<li data-description="The IO DataSource manages retrieval of data from remote sources, via the IO Utility.">
180
<a href="datasource-io.html">DataSource.IO</a>
185
<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">
186
<a href="datasource-function.html">DataSource.Function</a>
191
<li data-description="Use the DataSourceCache plugin to enable caching and reduce server calls to remote sources.">
192
<a href="datasource-caching.html">DataSource with Caching</a>
197
<li data-description="The DataSourceCache plugin supports offline caching so that cached data persists across browser sessions.">
198
<a href="datasource-offlinecache.html">DataSource with Offline Cache</a>
203
<li data-description="Use the Pollable extension to enable polling in your DataSource.">
204
<a href="datasource-polling.html">DataSource with Polling</a>
216
<div class="sidebox">
218
<h2 class="no-toc">Examples That Use This Component</h2>
222
<ul class="examples">
239
<li data-description="How to provide autocomplete suggestions using a DataSource instance.">
240
<a href="../autocomplete/ac-datasource.html">Remote Data via DataSource</a>
253
<script src="../assets/vendor/prettify/prettify-min.js"></script>
254
<script>prettyPrint();</script>