|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Example: DataSource.Get</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.Get</h1> |
|
|
25 |
<div class="yui3-g"> |
|
|
26 |
<div class="yui3-u-3-4"> |
|
|
27 |
<div id="main"> |
|
|
28 |
<div class="content"><style scoped> |
|
|
29 |
/* custom styles for this example */ |
|
|
30 |
#demo .output {margin-bottom:1em; padding:10px; border:1px solid #D9D9D9;} |
|
|
31 |
</style> |
|
|
32 |
|
|
|
33 |
<div class="intro"> |
|
|
34 |
<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> |
|
|
35 |
</div> |
|
|
36 |
|
|
|
37 |
<div class="example yui3-skin-sam"> |
|
|
38 |
<form id="demo"> |
|
|
39 |
<h4>JSON</h4> |
|
|
40 |
<h6>Data</h6> |
|
|
41 |
<pre> |
|
|
42 |
{ |
|
|
43 |
"query":{ |
|
|
44 |
"count":"10", |
|
|
45 |
"created":"2011-06-15T04:36:13Z", |
|
|
46 |
"lang":"en-US", |
|
|
47 |
"results":{ |
|
|
48 |
"event":[ |
|
|
49 |
{ |
|
|
50 |
"id": "6826736", |
|
|
51 |
"name": "PGA Fall Expo 2011", |
|
|
52 |
"start_date": "2011-08-01", |
|
|
53 |
"venue_name": "The Venetian Congress and Sands Expo Convention Center", |
|
|
54 |
... |
|
|
55 |
}, |
|
|
56 |
{ |
|
|
57 |
"id": "7228592", |
|
|
58 |
"name": "NAHC 30th Annual Meeting & Exposition", |
|
|
59 |
"start_date": "2011-10-01", |
|
|
60 |
"venue_name": "Mandalay Bay Resort", |
|
|
61 |
... |
|
|
62 |
}, |
|
|
63 |
{ |
|
|
64 |
"id": "7238009", |
|
|
65 |
"name": "Water and Society 2011-First International Conference on Water and Society", |
|
|
66 |
"start_date": "2011-12-05", |
|
|
67 |
"venue_name": "Richard Tam Alumni Center", |
|
|
68 |
... |
|
|
69 |
}, |
|
|
70 |
... |
|
|
71 |
] |
|
|
72 |
} |
|
|
73 |
} |
|
|
74 |
} |
|
|
75 |
</pre> |
|
|
76 |
|
|
|
77 |
<h6>Schema</h6> |
|
|
78 |
<pre> |
|
|
79 |
{ |
|
|
80 |
resultListLocator: "query.results.event", |
|
|
81 |
resultFields: ["name"] |
|
|
82 |
} |
|
|
83 |
</pre> |
|
|
84 |
|
|
|
85 |
<h6>Normalized data</h6> |
|
|
86 |
<input type="button" id="demo_json" value="Retrieve data"> |
|
|
87 |
<div id="demo_output_json" class="output"></div> |
|
|
88 |
</form> |
|
|
89 |
|
|
|
90 |
<script type="text/javascript"> |
|
|
91 |
YUI().use("json-stringify", "node", "datasource-get", "datasource-jsonschema", function (Y) { |
|
|
92 |
var myDataSource = new Y.DataSource.Get({ |
|
|
93 |
source: "http://query.yahooapis.com/v1/public/yql?format=json&" |
|
|
94 |
}); |
|
|
95 |
|
|
|
96 |
myDataSource.plug(Y.Plugin.DataSourceJSONSchema, { |
|
|
97 |
schema: { |
|
|
98 |
resultListLocator: "query.results.event", |
|
|
99 |
resultFields: ["name"] |
|
|
100 |
} |
|
|
101 |
}); |
|
|
102 |
|
|
|
103 |
Y.on("click", function(e){ |
|
|
104 |
// See what's happening in Las Vegas (woeid 2436704) |
|
|
105 |
myDataSource.sendRequest({ |
|
|
106 |
request: "q=select%20*%20from%20upcoming.events.bestinplace%20where%20woeid=2436704", |
|
|
107 |
callback: { |
|
|
108 |
success: function(e){ |
|
|
109 |
Y.one("#demo_output_json") |
|
|
110 |
.setHTML("<pre>" + |
|
|
111 |
Y.JSON.stringify(e.response, null, 4) |
|
|
112 |
.replace(/&/g,"&") |
|
|
113 |
.replace(/</g,"<") |
|
|
114 |
.replace(/>/g,">") + |
|
|
115 |
"</pre>"); |
|
|
116 |
}, |
|
|
117 |
failure: function(e){ |
|
|
118 |
Y.one("#demo_output_json") |
|
|
119 |
.setHTML("Could not retrieve data: " + e.error.message); |
|
|
120 |
} |
|
|
121 |
} |
|
|
122 |
}); |
|
|
123 |
}, "#demo_json"); |
|
|
124 |
}); |
|
|
125 |
</script> |
|
|
126 |
|
|
|
127 |
|
|
|
128 |
</div> |
|
|
129 |
|
|
|
130 |
<p>Use a DataSourceJSONSchema plugin to parse the data against a schema that you provide:</p> |
|
|
131 |
|
|
|
132 |
<pre class="code prettyprint">YUI().use("datasource-get", "datasource-jsonschema", function(Y) { |
|
|
133 |
var myDataSource = new Y.DataSource.Get({ |
|
|
134 |
source: "http://query.yahooapis.com/v1/public/yql?format=json&" |
|
|
135 |
}); |
|
|
136 |
|
|
|
137 |
myDataSource.plug(Y.Plugin.DataSourceJSONSchema, { |
|
|
138 |
schema: { |
|
|
139 |
resultListLocator: "query.results.result", |
|
|
140 |
resultFields: ["title"] |
|
|
141 |
} |
|
|
142 |
}); |
|
|
143 |
|
|
|
144 |
myDataSource.sendRequest({ |
|
|
145 |
request: "q=select%20*%20from%20upcoming.events.bestinplace...", |
|
|
146 |
callback: { |
|
|
147 |
success: function (e) { /* output to screen */ }, |
|
|
148 |
failure: function (e) { /* output to screen */ } |
|
|
149 |
} |
|
|
150 |
}); |
|
|
151 |
|
|
|
152 |
});</pre> |
|
|
153 |
|
|
|
154 |
|
|
|
155 |
</div> |
|
|
156 |
</div> |
|
|
157 |
</div> |
|
|
158 |
|
|
|
159 |
<div class="yui3-u-1-4"> |
|
|
160 |
<div class="sidebar"> |
|
|
161 |
|
|
|
162 |
|
|
|
163 |
|
|
|
164 |
<div class="sidebox"> |
|
|
165 |
<div class="hd"> |
|
|
166 |
<h2 class="no-toc">Examples</h2> |
|
|
167 |
</div> |
|
|
168 |
|
|
|
169 |
<div class="bd"> |
|
|
170 |
<ul class="examples"> |
|
|
171 |
|
|
|
172 |
|
|
|
173 |
<li data-description="The Local DataSource manages retrieval of in-page data, from JavaScript arrays and objects to DOM elements."> |
|
|
174 |
<a href="datasource-local.html">DataSource.Local</a> |
|
|
175 |
</li> |
|
|
176 |
|
|
|
177 |
|
|
|
178 |
|
|
|
179 |
<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."> |
|
|
180 |
<a href="datasource-get.html">DataSource.Get</a> |
|
|
181 |
</li> |
|
|
182 |
|
|
|
183 |
|
|
|
184 |
|
|
|
185 |
<li data-description="The IO DataSource manages retrieval of data from remote sources, via the IO Utility."> |
|
|
186 |
<a href="datasource-io.html">DataSource.IO</a> |
|
|
187 |
</li> |
|
|
188 |
|
|
|
189 |
|
|
|
190 |
|
|
|
191 |
<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"> |
|
|
192 |
<a href="datasource-function.html">DataSource.Function</a> |
|
|
193 |
</li> |
|
|
194 |
|
|
|
195 |
|
|
|
196 |
|
|
|
197 |
<li data-description="Use the DataSourceCache plugin to enable caching and reduce server calls to remote sources."> |
|
|
198 |
<a href="datasource-caching.html">DataSource with Caching</a> |
|
|
199 |
</li> |
|
|
200 |
|
|
|
201 |
|
|
|
202 |
|
|
|
203 |
<li data-description="The DataSourceCache plugin supports offline caching so that cached data persists across browser sessions."> |
|
|
204 |
<a href="datasource-offlinecache.html">DataSource with Offline Cache</a> |
|
|
205 |
</li> |
|
|
206 |
|
|
|
207 |
|
|
|
208 |
|
|
|
209 |
<li data-description="Use the Pollable extension to enable polling in your DataSource."> |
|
|
210 |
<a href="datasource-polling.html">DataSource with Polling</a> |
|
|
211 |
</li> |
|
|
212 |
|
|
|
213 |
|
|
|
214 |
|
|
|
215 |
|
|
|
216 |
</ul> |
|
|
217 |
</div> |
|
|
218 |
</div> |
|
|
219 |
|
|
|
220 |
|
|
|
221 |
|
|
|
222 |
<div class="sidebox"> |
|
|
223 |
<div class="hd"> |
|
|
224 |
<h2 class="no-toc">Examples That Use This Component</h2> |
|
|
225 |
</div> |
|
|
226 |
|
|
|
227 |
<div class="bd"> |
|
|
228 |
<ul class="examples"> |
|
|
229 |
|
|
|
230 |
|
|
|
231 |
|
|
|
232 |
|
|
|
233 |
|
|
|
234 |
|
|
|
235 |
|
|
|
236 |
|
|
|
237 |
|
|
|
238 |
|
|
|
239 |
|
|
|
240 |
|
|
|
241 |
|
|
|
242 |
|
|
|
243 |
|
|
|
244 |
|
|
|
245 |
<li data-description="How to provide autocomplete suggestions using a DataSource instance."> |
|
|
246 |
<a href="../autocomplete/ac-datasource.html">Remote Data via DataSource</a> |
|
|
247 |
</li> |
|
|
248 |
|
|
|
249 |
|
|
|
250 |
</ul> |
|
|
251 |
</div> |
|
|
252 |
</div> |
|
|
253 |
|
|
|
254 |
</div> |
|
|
255 |
</div> |
|
|
256 |
</div> |
|
|
257 |
</div> |
|
|
258 |
|
|
|
259 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
260 |
<script>prettyPrint();</script> |
|
|
261 |
|
|
|
262 |
<script> |
|
|
263 |
YUI.Env.Tests = { |
|
|
264 |
examples: [], |
|
|
265 |
project: '../assets', |
|
|
266 |
assets: '../assets/datasource', |
|
|
267 |
name: 'datasource-get', |
|
|
268 |
title: 'DataSource.Get', |
|
|
269 |
newWindow: '', |
|
|
270 |
auto: false |
|
|
271 |
}; |
|
|
272 |
YUI.Env.Tests.examples.push('datasource-local'); |
|
|
273 |
YUI.Env.Tests.examples.push('datasource-get'); |
|
|
274 |
YUI.Env.Tests.examples.push('datasource-io'); |
|
|
275 |
YUI.Env.Tests.examples.push('datasource-function'); |
|
|
276 |
YUI.Env.Tests.examples.push('datasource-caching'); |
|
|
277 |
YUI.Env.Tests.examples.push('datasource-offlinecache'); |
|
|
278 |
YUI.Env.Tests.examples.push('datasource-polling'); |
|
|
279 |
YUI.Env.Tests.examples.push('ac-datasource'); |
|
|
280 |
|
|
|
281 |
</script> |
|
|
282 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
283 |
|
|
|
284 |
|
|
|
285 |
|
|
|
286 |
</body> |
|
|
287 |
</html> |