|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Example: DataSource.IO</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.IO</h1> |
|
|
25 |
<div class="yui3-g"> |
|
|
26 |
<div class="yui3-u-3-4"> |
|
|
27 |
<div id="main"> |
|
|
28 |
<div class="content"><div class="intro"> |
|
|
29 |
<p>Accessing data from a server is easy with DataSource.IO, which uses the IO Utility to retrieve data over HTTP. A <a href="../dataschema/">DataSchema</a> plugin is used to normalize incoming data into a known format for consistency of usage by other components.</p> |
|
|
30 |
</div> |
|
|
31 |
|
|
|
32 |
<div class="example yui3-skin-sam"> |
|
|
33 |
<style scoped> |
|
|
34 |
/* custom styles for this example */ |
|
|
35 |
#demo .output {margin-bottom:1em; padding:10px; border:1px solid #D9D9D9;} |
|
|
36 |
</style> |
|
|
37 |
|
|
|
38 |
<form id="demo"> |
|
|
39 |
<h4>JSON</h4> |
|
|
40 |
<h6>Data</h6> |
|
|
41 |
<pre> |
|
|
42 |
{ |
|
|
43 |
"ResultSet": { |
|
|
44 |
"type":"web", |
|
|
45 |
"totalResultsAvailable":391000000, |
|
|
46 |
"totalResultsReturned":10, |
|
|
47 |
"firstResultPosition":1, |
|
|
48 |
... |
|
|
49 |
"Result":[ |
|
|
50 |
{"Title":"Madonna","Summary":"Official site of pop diva |
|
|
51 |
Madonna, with news, music, media, and fan club.", |
|
|
52 |
"Url":"http:\/\/www.madonna.com\/", ..., |
|
|
53 |
"Size":"145030"}}, |
|
|
54 |
{"Title":"Madonna - MySpace","Summary":"Madonna MySpace |
|
|
55 |
page features news, blog, music downloads, desktops, |
|
|
56 |
wallpapers, and more.", |
|
|
57 |
"Url":"http:\/\/www.myspace.com\/madonna", |
|
|
58 |
..., "Size":"110365"}}, |
|
|
59 |
{"Title":"YouTube - madonna's Channel", |
|
|
60 |
"Summary":"The Official Madonna YouTube Channel. |
|
|
61 |
Want to Subscribe? ... http:\/\/www.youtube.com\/Madonna. |
|
|
62 |
Sharing Options There are 3 ways to share this channel.", |
|
|
63 |
"Url":"http:\/\/youtube.com\/madonna", ..., |
|
|
64 |
"Size":"49955"}}, |
|
|
65 |
... |
|
|
66 |
] |
|
|
67 |
} |
|
|
68 |
} |
|
|
69 |
</pre> |
|
|
70 |
|
|
|
71 |
<h6>Schema</h6> |
|
|
72 |
<pre> |
|
|
73 |
{ |
|
|
74 |
resultListLocator: "ResultSet.Result", |
|
|
75 |
resultFields: ["Title"] |
|
|
76 |
} |
|
|
77 |
</pre> |
|
|
78 |
|
|
|
79 |
<h6>Normalized data</h6> |
|
|
80 |
<input type="button" id="demo_json" value="Retrieve data"> |
|
|
81 |
<div id="demo_output_json" class="output"></div> |
|
|
82 |
|
|
|
83 |
<h4>XML</h4> |
|
|
84 |
<h6>Data</h6> |
|
|
85 |
<pre> |
|
|
86 |
<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" |
|
|
87 |
yahoo:count="10" ...> |
|
|
88 |
<diagnostics> |
|
|
89 |
... |
|
|
90 |
</diagnostics> |
|
|
91 |
<results> |
|
|
92 |
<result xmlns="http://www.inktomi.com/"> |
|
|
93 |
... |
|
|
94 |
<dispurl> |
|
|
95 |
<![CDATA[www.<b>madonna.com</b>]]> |
|
|
96 |
</dispurl> |
|
|
97 |
<size>144947</size> |
|
|
98 |
<title> |
|
|
99 |
<![CDATA[<b>madonna</b>.com home]]> |
|
|
100 |
</title> |
|
|
101 |
<url>http://www.madonna.com/</url> |
|
|
102 |
</result> |
|
|
103 |
<result xmlns="http://www.inktomi.com/"> |
|
|
104 |
... |
|
|
105 |
<dispurl> |
|
|
106 |
<![CDATA[<b>en.wikipedia.org</b>/wiki/<wbr> |
|
|
107 |
<b>Madonna</b>_(entertainer)]]> |
|
|
108 |
</dispurl> |
|
|
109 |
<size>450316</size> |
|
|
110 |
<title> |
|
|
111 |
<![CDATA[<b>Madonna</b> (Entertainer) - Wikipedia]]> |
|
|
112 |
</title> |
|
|
113 |
<url>http://en.wikipedia.org/wiki/Madonna_(entertainer)</url> |
|
|
114 |
</result> |
|
|
115 |
<result xmlns="http://www.inktomi.com/"> |
|
|
116 |
... |
|
|
117 |
<dispurl> |
|
|
118 |
<![CDATA[www.<b>myspace.com</b>/<b>madonna</b>]]> |
|
|
119 |
</dispurl> |
|
|
120 |
<size>110851</size> |
|
|
121 |
<title> |
|
|
122 |
<![CDATA[<b>Madonna</b> - MySpace]]> |
|
|
123 |
</title> |
|
|
124 |
<url>http://www.myspace.com/madonna</url> |
|
|
125 |
</result> |
|
|
126 |
... |
|
|
127 |
</results> |
|
|
128 |
</query> |
|
|
129 |
</pre> |
|
|
130 |
|
|
|
131 |
<h6>Schema</h6> |
|
|
132 |
<pre> |
|
|
133 |
{ |
|
|
134 |
resultListLocator: "result", |
|
|
135 |
resultFields: [{key:"title", locator:"*[local-name() ='title']"}] |
|
|
136 |
} |
|
|
137 |
</pre> |
|
|
138 |
|
|
|
139 |
<h6>Normalized data</h6> |
|
|
140 |
<input type="button" id="demo_xml" value="Retrieve data"> |
|
|
141 |
<div id="demo_output_xml" class="output"></div> |
|
|
142 |
</form> |
|
|
143 |
|
|
|
144 |
<script type="text/javascript"> |
|
|
145 |
YUI().use("dump", "node", "datasource-io", "datasource-jsonschema", "datasource-xmlschema", function (Y) { |
|
|
146 |
var myDataSourceJSON = new Y.DataSource.IO({source:"../assets/datasource/ysearch.json"}), |
|
|
147 |
myCallbackJSON = { |
|
|
148 |
success: function(e){ |
|
|
149 |
Y.one("#demo_output_json").setHTML(Y.dump(e.response)); |
|
|
150 |
}, |
|
|
151 |
failure: function(e){ |
|
|
152 |
Y.one("#demo_output_json").setHTML("Could not retrieve data: " + e.error.message); |
|
|
153 |
} |
|
|
154 |
}; |
|
|
155 |
|
|
|
156 |
myDataSourceJSON.plug(Y.Plugin.DataSourceJSONSchema, { |
|
|
157 |
schema: { |
|
|
158 |
resultListLocator: "ResultSet.Result", |
|
|
159 |
resultFields: ["Title"] |
|
|
160 |
} |
|
|
161 |
}); |
|
|
162 |
|
|
|
163 |
Y.on("click", function(e){ |
|
|
164 |
myDataSourceJSON.sendRequest({ |
|
|
165 |
request:"?output=json", |
|
|
166 |
callback:myCallbackJSON |
|
|
167 |
}); |
|
|
168 |
}, "#demo_json"); |
|
|
169 |
|
|
|
170 |
var myDataSourceXML = new Y.DataSource.IO({source:"../assets/datasource/ysearch.xml"}), |
|
|
171 |
myCallbackXML = { |
|
|
172 |
success: function(e){ |
|
|
173 |
Y.one("#demo_output_xml").setHTML(Y.dump(e.response).replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")); |
|
|
174 |
}, |
|
|
175 |
failure: function(e){ |
|
|
176 |
Y.one("#demo_output_xml").setHTML("Could not retrieve data: " + e.error.message); |
|
|
177 |
} |
|
|
178 |
}; |
|
|
179 |
|
|
|
180 |
myDataSourceXML.plug(Y.Plugin.DataSourceXMLSchema, { |
|
|
181 |
schema: { |
|
|
182 |
resultListLocator: "result", |
|
|
183 |
resultFields: [{key:"title", locator:"*[local-name() ='title']"}] |
|
|
184 |
} |
|
|
185 |
}); |
|
|
186 |
|
|
|
187 |
Y.on("click", function(e){ |
|
|
188 |
myDataSourceXML.sendRequest({ |
|
|
189 |
request:"?output=xml", |
|
|
190 |
callback:myCallbackXML |
|
|
191 |
}); |
|
|
192 |
}, "#demo_xml"); |
|
|
193 |
}); |
|
|
194 |
</script> |
|
|
195 |
|
|
|
196 |
</div> |
|
|
197 |
|
|
|
198 |
<p>If your server returns JSON data, use a DataSourceJSONSchema plugin to parse the data against a schema that you provide:</p> |
|
|
199 |
|
|
|
200 |
<pre class="code prettyprint">YUI().use("datasource-io", "datasource-jsonschema", function(Y) { |
|
|
201 |
var myDataSource = new Y.DataSource.IO({source:"ysearch_json_madonna.php"}), |
|
|
202 |
myCallback = { |
|
|
203 |
success: function(e){ |
|
|
204 |
alert(e.response); |
|
|
205 |
}, |
|
|
206 |
failure: function(e){ |
|
|
207 |
alert("Could not retrieve data: " + e.error.message); |
|
|
208 |
} |
|
|
209 |
}; |
|
|
210 |
|
|
|
211 |
myDataSource.plug(Y.Plugin.DataSourceJSONSchema, { |
|
|
212 |
schema: { |
|
|
213 |
resultListLocator: "ResultSet.Result", |
|
|
214 |
resultFields: ["Title"] |
|
|
215 |
} |
|
|
216 |
}); |
|
|
217 |
|
|
|
218 |
// This request string will get appended to the URI |
|
|
219 |
myDataSource.sendRequest({ |
|
|
220 |
request:"?output=json", |
|
|
221 |
callback:myCallback |
|
|
222 |
}); |
|
|
223 |
});</pre> |
|
|
224 |
|
|
|
225 |
|
|
|
226 |
<p>On the other hand, a DataSourceXMLSchema plugin can be used to parse XML data coming from your server:</p> |
|
|
227 |
|
|
|
228 |
<pre class="code prettyprint">YUI().use("datasource-io", "datasource-xmlschema", function(Y) { |
|
|
229 |
var myDataSource = new Y.DataSource.IO({source:"ysearch_xml_madonna.php"}), |
|
|
230 |
myCallback = { |
|
|
231 |
success: function(e){ |
|
|
232 |
alert(e.response); |
|
|
233 |
}, |
|
|
234 |
failure: function(e){ |
|
|
235 |
alert("Could not retrieve data: " + e.error.message); |
|
|
236 |
} |
|
|
237 |
}; |
|
|
238 |
|
|
|
239 |
myDataSource.plug(Y.Plugin.DataSourceXMLSchema, { |
|
|
240 |
schema: { |
|
|
241 |
resultListLocator: "result", |
|
|
242 |
resultFields: [{key:"title", locator:"*[local-name() ='title']"}] |
|
|
243 |
} |
|
|
244 |
}); |
|
|
245 |
|
|
|
246 |
myDataSource.sendRequest({ |
|
|
247 |
request:"?output=xml", |
|
|
248 |
callback:myCallback |
|
|
249 |
}); |
|
|
250 |
});</pre> |
|
|
251 |
|
|
|
252 |
</div> |
|
|
253 |
</div> |
|
|
254 |
</div> |
|
|
255 |
|
|
|
256 |
<div class="yui3-u-1-4"> |
|
|
257 |
<div class="sidebar"> |
|
|
258 |
|
|
|
259 |
|
|
|
260 |
|
|
|
261 |
<div class="sidebox"> |
|
|
262 |
<div class="hd"> |
|
|
263 |
<h2 class="no-toc">Examples</h2> |
|
|
264 |
</div> |
|
|
265 |
|
|
|
266 |
<div class="bd"> |
|
|
267 |
<ul class="examples"> |
|
|
268 |
|
|
|
269 |
|
|
|
270 |
<li data-description="The Local DataSource manages retrieval of in-page data, from JavaScript arrays and objects to DOM elements."> |
|
|
271 |
<a href="datasource-local.html">DataSource.Local</a> |
|
|
272 |
</li> |
|
|
273 |
|
|
|
274 |
|
|
|
275 |
|
|
|
276 |
<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."> |
|
|
277 |
<a href="datasource-get.html">DataSource.Get</a> |
|
|
278 |
</li> |
|
|
279 |
|
|
|
280 |
|
|
|
281 |
|
|
|
282 |
<li data-description="The IO DataSource manages retrieval of data from remote sources, via the IO Utility."> |
|
|
283 |
<a href="datasource-io.html">DataSource.IO</a> |
|
|
284 |
</li> |
|
|
285 |
|
|
|
286 |
|
|
|
287 |
|
|
|
288 |
<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"> |
|
|
289 |
<a href="datasource-function.html">DataSource.Function</a> |
|
|
290 |
</li> |
|
|
291 |
|
|
|
292 |
|
|
|
293 |
|
|
|
294 |
<li data-description="Use the DataSourceCache plugin to enable caching and reduce server calls to remote sources."> |
|
|
295 |
<a href="datasource-caching.html">DataSource with Caching</a> |
|
|
296 |
</li> |
|
|
297 |
|
|
|
298 |
|
|
|
299 |
|
|
|
300 |
<li data-description="The DataSourceCache plugin supports offline caching so that cached data persists across browser sessions."> |
|
|
301 |
<a href="datasource-offlinecache.html">DataSource with Offline Cache</a> |
|
|
302 |
</li> |
|
|
303 |
|
|
|
304 |
|
|
|
305 |
|
|
|
306 |
<li data-description="Use the Pollable extension to enable polling in your DataSource."> |
|
|
307 |
<a href="datasource-polling.html">DataSource with Polling</a> |
|
|
308 |
</li> |
|
|
309 |
|
|
|
310 |
|
|
|
311 |
|
|
|
312 |
|
|
|
313 |
</ul> |
|
|
314 |
</div> |
|
|
315 |
</div> |
|
|
316 |
|
|
|
317 |
|
|
|
318 |
|
|
|
319 |
<div class="sidebox"> |
|
|
320 |
<div class="hd"> |
|
|
321 |
<h2 class="no-toc">Examples That Use This Component</h2> |
|
|
322 |
</div> |
|
|
323 |
|
|
|
324 |
<div class="bd"> |
|
|
325 |
<ul class="examples"> |
|
|
326 |
|
|
|
327 |
|
|
|
328 |
|
|
|
329 |
|
|
|
330 |
|
|
|
331 |
|
|
|
332 |
|
|
|
333 |
|
|
|
334 |
|
|
|
335 |
|
|
|
336 |
|
|
|
337 |
|
|
|
338 |
|
|
|
339 |
|
|
|
340 |
|
|
|
341 |
|
|
|
342 |
<li data-description="How to provide autocomplete suggestions using a DataSource instance."> |
|
|
343 |
<a href="../autocomplete/ac-datasource.html">Remote Data via DataSource</a> |
|
|
344 |
</li> |
|
|
345 |
|
|
|
346 |
|
|
|
347 |
</ul> |
|
|
348 |
</div> |
|
|
349 |
</div> |
|
|
350 |
|
|
|
351 |
</div> |
|
|
352 |
</div> |
|
|
353 |
</div> |
|
|
354 |
</div> |
|
|
355 |
|
|
|
356 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
357 |
<script>prettyPrint();</script> |
|
|
358 |
|
|
|
359 |
<script> |
|
|
360 |
YUI.Env.Tests = { |
|
|
361 |
examples: [], |
|
|
362 |
project: '../assets', |
|
|
363 |
assets: '../assets/datasource', |
|
|
364 |
name: 'datasource-io', |
|
|
365 |
title: 'DataSource.IO', |
|
|
366 |
newWindow: '', |
|
|
367 |
auto: false |
|
|
368 |
}; |
|
|
369 |
YUI.Env.Tests.examples.push('datasource-local'); |
|
|
370 |
YUI.Env.Tests.examples.push('datasource-get'); |
|
|
371 |
YUI.Env.Tests.examples.push('datasource-io'); |
|
|
372 |
YUI.Env.Tests.examples.push('datasource-function'); |
|
|
373 |
YUI.Env.Tests.examples.push('datasource-caching'); |
|
|
374 |
YUI.Env.Tests.examples.push('datasource-offlinecache'); |
|
|
375 |
YUI.Env.Tests.examples.push('datasource-polling'); |
|
|
376 |
YUI.Env.Tests.examples.push('ac-datasource'); |
|
|
377 |
|
|
|
378 |
</script> |
|
|
379 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
380 |
|
|
|
381 |
|
|
|
382 |
|
|
|
383 |
</body> |
|
|
384 |
</html> |