|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Example: DataSource.Local</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.Local</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>Use DataSource.Local to manage retrieval of local data, from JavaScript arrays and objects to DOM elements. 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>Array</h4> |
|
|
40 |
<h6>Data</h6> |
|
|
41 |
<pre> |
|
|
42 |
[ |
|
|
43 |
{name:"abc",id:123,extra:"foo"}, |
|
|
44 |
{name:"def",id:456,extra:"bar"}, |
|
|
45 |
{name:"ghi",id:789,extra:"baz"} |
|
|
46 |
] |
|
|
47 |
</pre> |
|
|
48 |
|
|
|
49 |
<h6>Schema</h6> |
|
|
50 |
<pre> |
|
|
51 |
{ |
|
|
52 |
resultFields: ["name","id"] |
|
|
53 |
} |
|
|
54 |
</pre> |
|
|
55 |
|
|
|
56 |
<h6>Normalized data</h6> |
|
|
57 |
<input type="button" id="demo_array" value="Retrieve data"> |
|
|
58 |
<div id="demo_output_array" class="output"></div> |
|
|
59 |
|
|
|
60 |
<h4>JSON</h4> |
|
|
61 |
<h6>Data</h6> |
|
|
62 |
<pre> |
|
|
63 |
{ |
|
|
64 |
"profile":{ |
|
|
65 |
"current":160, |
|
|
66 |
"target":150 |
|
|
67 |
}, |
|
|
68 |
"reference": [ |
|
|
69 |
{ |
|
|
70 |
"category":"exercise", |
|
|
71 |
"type":"expenditure", |
|
|
72 |
"activities":[ |
|
|
73 |
{"name":"biking", "calories":550}, |
|
|
74 |
{"name":"golf", "calories":1000}, |
|
|
75 |
{"name":"running", "calories":650}, |
|
|
76 |
{"name":"swimming", "calories":650}, |
|
|
77 |
{"name":"walking", "calories":225} |
|
|
78 |
] |
|
|
79 |
}, |
|
|
80 |
{ |
|
|
81 |
"category":"nutrition", |
|
|
82 |
"type":"intake", |
|
|
83 |
"fruit":[ |
|
|
84 |
{"name":"apple", "calories":70}, |
|
|
85 |
{"name":"banana", "calories":70}, |
|
|
86 |
{"name":"orange", "calories":90}, |
|
|
87 |
], |
|
|
88 |
"vegetables":[ |
|
|
89 |
{"name":"baked potato", "calories":150}, |
|
|
90 |
{"name":"broccoli", "calories":50}, |
|
|
91 |
{"name":"green beans", "calories":30} |
|
|
92 |
] |
|
|
93 |
} |
|
|
94 |
], |
|
|
95 |
"program": [ |
|
|
96 |
{ |
|
|
97 |
"category":"exercise", |
|
|
98 |
"schedule":[ |
|
|
99 |
{"day":"sunday", "activity":"swimming"}, |
|
|
100 |
{"day":"monday", "activity":"running"}, |
|
|
101 |
{"day":"tuesday", "activity":"biking"}, |
|
|
102 |
{"day":"wednesday", "activity":"running"}, |
|
|
103 |
{"day":"thursday", "activity":"swimming"}, |
|
|
104 |
{"day":"friday", "activity":"running"}, |
|
|
105 |
{"day":"saturday", "activity":"golf"} |
|
|
106 |
] |
|
|
107 |
}, |
|
|
108 |
{ |
|
|
109 |
"category":"diet", |
|
|
110 |
"schedule":[ |
|
|
111 |
] |
|
|
112 |
} |
|
|
113 |
] |
|
|
114 |
} |
|
|
115 |
</pre> |
|
|
116 |
|
|
|
117 |
<h6>Schema</h6> |
|
|
118 |
<pre> |
|
|
119 |
{ |
|
|
120 |
resultListLocator: "reference[1].fruit", |
|
|
121 |
resultFields: ["name","calories"] |
|
|
122 |
} |
|
|
123 |
</pre> |
|
|
124 |
|
|
|
125 |
<h6>Normalized data</h6> |
|
|
126 |
<input type="button" id="demo_json" value="Retrieve data"> |
|
|
127 |
<div id="demo_output_json" class="output"></div> |
|
|
128 |
|
|
|
129 |
<h4>HTML Table (XML)</h4> |
|
|
130 |
<h6>Data</h6> |
|
|
131 |
<table id="myTable"> |
|
|
132 |
<tr> |
|
|
133 |
<td>coffee</td> |
|
|
134 |
<td>1.25</td> |
|
|
135 |
</tr> |
|
|
136 |
<tr> |
|
|
137 |
<td>juice</td> |
|
|
138 |
<td>2.00</td> |
|
|
139 |
</tr> |
|
|
140 |
<tr> |
|
|
141 |
<td>tea</td> |
|
|
142 |
<td>1.25</td> |
|
|
143 |
</tr> |
|
|
144 |
<tr> |
|
|
145 |
<td>soda</td> |
|
|
146 |
<td>1.00</td> |
|
|
147 |
</tr> |
|
|
148 |
</table> |
|
|
149 |
|
|
|
150 |
<h6>Schema</h6> |
|
|
151 |
<pre> |
|
|
152 |
{ |
|
|
153 |
resultListLocator: "tr", |
|
|
154 |
resultFields: [{key:"beverage", locator:"td[1]"}, {key:"price", locator:"td[2]"}] |
|
|
155 |
} |
|
|
156 |
</pre> |
|
|
157 |
|
|
|
158 |
<h6>Normalized data</h6> |
|
|
159 |
<input type="button" id="demo_table" value="Retrieve data"> |
|
|
160 |
<div id="demo_output_table" class="output"></div> |
|
|
161 |
</form> |
|
|
162 |
|
|
|
163 |
<script type="text/javascript"> |
|
|
164 |
YUI().use("dump", "node", "datasource-local", "datasource-arrayschema", "datasource-jsonschema", "datasource-xmlschema", function (Y) { |
|
|
165 |
var myDataArray = [ |
|
|
166 |
{name:"abc",id:123,extra:"foo"}, |
|
|
167 |
{name:"def",id:456,extra:"bar"}, |
|
|
168 |
{name:"ghi",id:789,extra:"baz"} |
|
|
169 |
], |
|
|
170 |
myDataSourceArray = new Y.DataSource.Local({source:myDataArray}), |
|
|
171 |
myCallbackArray = { |
|
|
172 |
success: function(e){ |
|
|
173 |
Y.one("#demo_output_array").setHTML(Y.dump(e.response)); |
|
|
174 |
}, |
|
|
175 |
failure: function(e){ |
|
|
176 |
Y.one("#demo_output_array").setHTML("Could not retrieve data: " + e.error.message); |
|
|
177 |
} |
|
|
178 |
}; |
|
|
179 |
|
|
|
180 |
myDataSourceArray.plug(Y.Plugin.DataSourceArraySchema, { |
|
|
181 |
schema: { |
|
|
182 |
resultFields: ["name","id"] |
|
|
183 |
} |
|
|
184 |
}); |
|
|
185 |
|
|
|
186 |
Y.on("click", function(e){ |
|
|
187 |
myDataSourceArray.sendRequest({callback:myCallbackArray}); |
|
|
188 |
}, "#demo_array"); |
|
|
189 |
|
|
|
190 |
var myDataJSON = {"profile":{"current":160,"target":150},"reference": [{"category":"exercise","type":"expenditure","activities":[{"name":"biking", "calories":550},{"name":"golf", "calories":1000},{"name":"running", "calories":650},{"name":"swimming", "calories":650},{"name":"walking", "calories":225}]},{"category":"nutrition","type":"intake","fruit":[{"name":"apple", "calories":70},{"name":"banana", "calories":70},{"name":"orange", "calories":90},],"vegetables":[{"name":"baked potato", "calories":150},{"name":"broccoli", "calories":50},{"name":"green beans", "calories":30}]}],"program": [{"category":"exercise","schedule":[{"day":"sunday", "activity":"swimming"},{"day":"monday", "activity":"running"},{"day":"tuesday", "activity":"biking"},{"day":"wednesday", "activity":"running"},{"day":"thursday", "activity":"swimming"},{"day":"friday", "activity":"running"},{"day":"saturday", "activity":"golf"}]},{"category":"diet","schedule":[]}]}, |
|
|
191 |
myDataSourceJSON = new Y.DataSource.Local({source:myDataJSON}), |
|
|
192 |
myCallbackJSON = { |
|
|
193 |
success: function(e){ |
|
|
194 |
Y.one("#demo_output_json").setHTML(Y.dump(e.response)); |
|
|
195 |
}, |
|
|
196 |
failure: function(e){ |
|
|
197 |
Y.one("#demo_output_json").setHTML("Could not retrieve data: " + e.error.message); |
|
|
198 |
} |
|
|
199 |
}; |
|
|
200 |
|
|
|
201 |
myDataSourceJSON.plug(Y.Plugin.DataSourceJSONSchema, { |
|
|
202 |
schema: { |
|
|
203 |
resultListLocator: "reference[1].fruit", |
|
|
204 |
resultFields: ["name","calories"] |
|
|
205 |
} |
|
|
206 |
}); |
|
|
207 |
|
|
|
208 |
Y.on("click", function(e){ |
|
|
209 |
myDataSourceJSON.sendRequest({callback:myCallbackJSON}); |
|
|
210 |
}, "#demo_json"); |
|
|
211 |
|
|
|
212 |
|
|
|
213 |
var myTable = Y.Node.getDOMNode(Y.one("#myTable")), |
|
|
214 |
myDataSourceTable = new Y.DataSource.Local({source:myTable}), |
|
|
215 |
myCallbackTable = { |
|
|
216 |
success: function(e){ |
|
|
217 |
Y.one("#demo_output_table").setHTML(Y.dump(e.response)); |
|
|
218 |
}, |
|
|
219 |
failure: function(e){ |
|
|
220 |
Y.one("#demo_output_table").setHTML("Could not retrieve data: " + e.error.message); |
|
|
221 |
} |
|
|
222 |
}; |
|
|
223 |
|
|
|
224 |
myDataSourceTable.plug(Y.Plugin.DataSourceXMLSchema, { |
|
|
225 |
schema: { |
|
|
226 |
resultListLocator: "tr", |
|
|
227 |
resultFields: [{key:"beverage", locator:"td[1]"}, {key:"price", locator:"td[2]"}] |
|
|
228 |
} |
|
|
229 |
}); |
|
|
230 |
|
|
|
231 |
Y.on("click", function(e){ |
|
|
232 |
myDataSourceTable.sendRequest({callback:myCallbackTable}); |
|
|
233 |
}, "#demo_table"); |
|
|
234 |
}); |
|
|
235 |
|
|
|
236 |
</script> |
|
|
237 |
|
|
|
238 |
</div> |
|
|
239 |
|
|
|
240 |
<p>If you are working with local array data, use the DataSourceArraySchema plugin to normalize and filter the data into a consistent format:</p> |
|
|
241 |
|
|
|
242 |
<pre class="code prettyprint">YUI().use("datasource-local", "datasource-arrayschema", function(Y) { |
|
|
243 |
var myData = [ |
|
|
244 |
{name:"abc",id:123,extra:"foo"}, |
|
|
245 |
{name:"def",id:456,extra:"bar"}, |
|
|
246 |
{name:"ghi",id:789,extra:"baz"} |
|
|
247 |
], |
|
|
248 |
myDataSource = new Y.DataSource.Local({source:myData}), |
|
|
249 |
myCallback = { |
|
|
250 |
success: function(e){ |
|
|
251 |
alert(e.response); |
|
|
252 |
}, |
|
|
253 |
failure: function(e){ |
|
|
254 |
alert("Could not retrieve data: " + e.error.message); |
|
|
255 |
} |
|
|
256 |
}; |
|
|
257 |
|
|
|
258 |
myDataSource.plug(Y.Plugin.DataSourceArraySchema, { |
|
|
259 |
schema: { |
|
|
260 |
resultFields: ["name","id"] |
|
|
261 |
} |
|
|
262 |
}); |
|
|
263 |
|
|
|
264 |
myDataSource.sendRequest({callback:myCallback}); |
|
|
265 |
});</pre> |
|
|
266 |
|
|
|
267 |
|
|
|
268 |
<p>Use the DataSourceJSONSchema plugin to normalize JSON data:</p> |
|
|
269 |
|
|
|
270 |
<pre class="code prettyprint">YUI().use("datasource-local", "datasource-jsonschema", function(Y) { |
|
|
271 |
var myData = { |
|
|
272 |
"profile":{ |
|
|
273 |
"current":160, |
|
|
274 |
"target":150 |
|
|
275 |
}, |
|
|
276 |
"reference": [ |
|
|
277 |
{ |
|
|
278 |
... |
|
|
279 |
}, |
|
|
280 |
{ |
|
|
281 |
"category":"nutrition", |
|
|
282 |
"type":"intake", |
|
|
283 |
"fruit":[ |
|
|
284 |
{"name":"apple", "calories":70}, |
|
|
285 |
{"name":"banana", "calories":70}, |
|
|
286 |
{"name":"orange", "calories":90}, |
|
|
287 |
], |
|
|
288 |
"vegetables":[ |
|
|
289 |
{"name":"baked potato", "calories":150}, |
|
|
290 |
{"name":"broccoli", "calories":50}, |
|
|
291 |
{"name":"green beans", "calories":30} |
|
|
292 |
] |
|
|
293 |
} |
|
|
294 |
], |
|
|
295 |
"program": [ |
|
|
296 |
... |
|
|
297 |
] |
|
|
298 |
}, |
|
|
299 |
myDataSource = new Y.DataSource.Local({source:myData}), |
|
|
300 |
myCallback = { |
|
|
301 |
success: function(e){ |
|
|
302 |
alert(e.response); |
|
|
303 |
}, |
|
|
304 |
failure: function(e){ |
|
|
305 |
alert("Could not retrieve data: " + e.error.message); |
|
|
306 |
} |
|
|
307 |
}; |
|
|
308 |
|
|
|
309 |
myDataSource.plug(Y.Plugin.DataSourceJSONSchema, { |
|
|
310 |
schema: { |
|
|
311 |
resultListLocator: "reference[1].fruit", |
|
|
312 |
resultFields: ["name","calories"] |
|
|
313 |
} |
|
|
314 |
}); |
|
|
315 |
|
|
|
316 |
myDataSource.sendRequest({callback:myCallback}); |
|
|
317 |
});</pre> |
|
|
318 |
|
|
|
319 |
|
|
|
320 |
<p>You can use the DataSourceXMLSchema plugin to work with DOM elements:</p> |
|
|
321 |
|
|
|
322 |
<pre class="code prettyprint">YUI().use("datasource-local", "datasource-xmlschema", function(Y) { |
|
|
323 |
var myTable = Y.Node.getDOMNode(Y.one("#myTable")), |
|
|
324 |
myDataSource = new Y.DataSource.Local({source:myTable}), |
|
|
325 |
myCallback = { |
|
|
326 |
success: function(e){ |
|
|
327 |
alert(e.response); |
|
|
328 |
}, |
|
|
329 |
failure: function(e){ |
|
|
330 |
alert("Could not retrieve data: " + e.error.message); |
|
|
331 |
} |
|
|
332 |
}; |
|
|
333 |
|
|
|
334 |
myDataSource.plug(Y.Plugin.DataSourceXMLSchema, { |
|
|
335 |
schema: { |
|
|
336 |
resultListLocator: "tr", |
|
|
337 |
resultFields: [ |
|
|
338 |
{key:"beverage", locator:"td[1]"}, |
|
|
339 |
{key:"price", locator:"td[2]"} |
|
|
340 |
] |
|
|
341 |
} |
|
|
342 |
}); |
|
|
343 |
|
|
|
344 |
myDataSource.sendRequest({callback:myCallback}); |
|
|
345 |
});</pre> |
|
|
346 |
|
|
|
347 |
</div> |
|
|
348 |
</div> |
|
|
349 |
</div> |
|
|
350 |
|
|
|
351 |
<div class="yui3-u-1-4"> |
|
|
352 |
<div class="sidebar"> |
|
|
353 |
|
|
|
354 |
|
|
|
355 |
|
|
|
356 |
<div class="sidebox"> |
|
|
357 |
<div class="hd"> |
|
|
358 |
<h2 class="no-toc">Examples</h2> |
|
|
359 |
</div> |
|
|
360 |
|
|
|
361 |
<div class="bd"> |
|
|
362 |
<ul class="examples"> |
|
|
363 |
|
|
|
364 |
|
|
|
365 |
<li data-description="The Local DataSource manages retrieval of in-page data, from JavaScript arrays and objects to DOM elements."> |
|
|
366 |
<a href="datasource-local.html">DataSource.Local</a> |
|
|
367 |
</li> |
|
|
368 |
|
|
|
369 |
|
|
|
370 |
|
|
|
371 |
<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."> |
|
|
372 |
<a href="datasource-get.html">DataSource.Get</a> |
|
|
373 |
</li> |
|
|
374 |
|
|
|
375 |
|
|
|
376 |
|
|
|
377 |
<li data-description="The IO DataSource manages retrieval of data from remote sources, via the IO Utility."> |
|
|
378 |
<a href="datasource-io.html">DataSource.IO</a> |
|
|
379 |
</li> |
|
|
380 |
|
|
|
381 |
|
|
|
382 |
|
|
|
383 |
<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"> |
|
|
384 |
<a href="datasource-function.html">DataSource.Function</a> |
|
|
385 |
</li> |
|
|
386 |
|
|
|
387 |
|
|
|
388 |
|
|
|
389 |
<li data-description="Use the DataSourceCache plugin to enable caching and reduce server calls to remote sources."> |
|
|
390 |
<a href="datasource-caching.html">DataSource with Caching</a> |
|
|
391 |
</li> |
|
|
392 |
|
|
|
393 |
|
|
|
394 |
|
|
|
395 |
<li data-description="The DataSourceCache plugin supports offline caching so that cached data persists across browser sessions."> |
|
|
396 |
<a href="datasource-offlinecache.html">DataSource with Offline Cache</a> |
|
|
397 |
</li> |
|
|
398 |
|
|
|
399 |
|
|
|
400 |
|
|
|
401 |
<li data-description="Use the Pollable extension to enable polling in your DataSource."> |
|
|
402 |
<a href="datasource-polling.html">DataSource with Polling</a> |
|
|
403 |
</li> |
|
|
404 |
|
|
|
405 |
|
|
|
406 |
|
|
|
407 |
|
|
|
408 |
</ul> |
|
|
409 |
</div> |
|
|
410 |
</div> |
|
|
411 |
|
|
|
412 |
|
|
|
413 |
|
|
|
414 |
<div class="sidebox"> |
|
|
415 |
<div class="hd"> |
|
|
416 |
<h2 class="no-toc">Examples That Use This Component</h2> |
|
|
417 |
</div> |
|
|
418 |
|
|
|
419 |
<div class="bd"> |
|
|
420 |
<ul class="examples"> |
|
|
421 |
|
|
|
422 |
|
|
|
423 |
|
|
|
424 |
|
|
|
425 |
|
|
|
426 |
|
|
|
427 |
|
|
|
428 |
|
|
|
429 |
|
|
|
430 |
|
|
|
431 |
|
|
|
432 |
|
|
|
433 |
|
|
|
434 |
|
|
|
435 |
|
|
|
436 |
|
|
|
437 |
<li data-description="How to provide autocomplete suggestions using a DataSource instance."> |
|
|
438 |
<a href="../autocomplete/ac-datasource.html">Remote Data via DataSource</a> |
|
|
439 |
</li> |
|
|
440 |
|
|
|
441 |
|
|
|
442 |
</ul> |
|
|
443 |
</div> |
|
|
444 |
</div> |
|
|
445 |
|
|
|
446 |
</div> |
|
|
447 |
</div> |
|
|
448 |
</div> |
|
|
449 |
</div> |
|
|
450 |
|
|
|
451 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
452 |
<script>prettyPrint();</script> |
|
|
453 |
|
|
|
454 |
<script> |
|
|
455 |
YUI.Env.Tests = { |
|
|
456 |
examples: [], |
|
|
457 |
project: '../assets', |
|
|
458 |
assets: '../assets/datasource', |
|
|
459 |
name: 'datasource-local', |
|
|
460 |
title: 'DataSource.Local', |
|
|
461 |
newWindow: '', |
|
|
462 |
auto: false |
|
|
463 |
}; |
|
|
464 |
YUI.Env.Tests.examples.push('datasource-local'); |
|
|
465 |
YUI.Env.Tests.examples.push('datasource-get'); |
|
|
466 |
YUI.Env.Tests.examples.push('datasource-io'); |
|
|
467 |
YUI.Env.Tests.examples.push('datasource-function'); |
|
|
468 |
YUI.Env.Tests.examples.push('datasource-caching'); |
|
|
469 |
YUI.Env.Tests.examples.push('datasource-offlinecache'); |
|
|
470 |
YUI.Env.Tests.examples.push('datasource-polling'); |
|
|
471 |
YUI.Env.Tests.examples.push('ac-datasource'); |
|
|
472 |
|
|
|
473 |
</script> |
|
|
474 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
475 |
|
|
|
476 |
|
|
|
477 |
|
|
|
478 |
</body> |
|
|
479 |
</html> |