1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<div class="intro">
<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>
</div>
<div class="example yui3-skin-sam">
{{>datasource-local-source}}
</div>
<p>If you are working with local array data, use the DataSourceArraySchema plugin to normalize and filter the data into a consistent format:</p>
```
YUI().use("datasource-local", "datasource-arrayschema", function(Y) {
var myData = [
{name:"abc",id:123,extra:"foo"},
{name:"def",id:456,extra:"bar"},
{name:"ghi",id:789,extra:"baz"}
],
myDataSource = new Y.DataSource.Local({source:myData}),
myCallback = {
success: function(e){
alert(e.response);
},
failure: function(e){
alert("Could not retrieve data: " + e.error.message);
}
};
schema: {
resultFields: ["name","id"]
}
});
myDataSource.sendRequest({callback:myCallback});
});
```
<p>Use the DataSourceJSONSchema plugin to normalize JSON data:</p>
```
YUI().use("datasource-local", "datasource-jsonschema", function(Y) {
var myData = {
"profile":{
"current":160,
"target":150
},
"reference": [
{
...
},
{
"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": [
...
]
},
myDataSource = new Y.DataSource.Local({source:myData}),
myCallback = {
success: function(e){
alert(e.response);
},
failure: function(e){
alert("Could not retrieve data: " + e.error.message);
}
};
schema: {
resultListLocator: "reference[1].fruit",
resultFields: ["name","calories"]
}
});
myDataSource.sendRequest({callback:myCallback});
});
```
<p>You can use the DataSourceXMLSchema plugin to work with DOM elements:</p>
```
YUI().use("datasource-local", "datasource-xmlschema", function(Y) {
var myTable = Y.Node.getDOMNode(Y.one("#myTable")),
myDataSource = new Y.DataSource.Local({source:myTable}),
myCallback = {
success: function(e){
alert(e.response);
},
failure: function(e){
alert("Could not retrieve data: " + e.error.message);
}
};
schema: {
resultListLocator: "tr",
resultFields: [
{key:"beverage", locator:"td[1]"},
{key:"price", locator:"td[2]"}
]
}
});
myDataSource.sendRequest({callback:myCallback});
});
```