Cross Reference: /yui3/src/datasource/docs/datasource-io.mustache
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
<div class="intro">
<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>
</div>
<div class="example yui3-skin-sam">
{{>datasource-io-source}}
</div>
<p>If your server returns JSON data, use a DataSourceJSONSchema plugin to parse the data against a schema that you provide:</p>
```
YUI().use("datasource-io", "datasource-jsonschema", function(Y) {
var myDataSource = new Y.DataSource.IO({source:"ysearch_json_madonna.php"}),
myCallback = {
success: function(e){
alert(e.response);
},
failure: function(e){
alert("Could not retrieve data: " + e.error.message);
}
};
myDataSource.plug(Y.Plugin.DataSourceJSONSchema, {
schema: {
resultListLocator: "ResultSet.Result",
resultFields: ["Title"]
}
});
// This request string will get appended to the URI
myDataSource.sendRequest({
request:"?output=json",
callback:myCallback
});
});
```
<p>On the other hand, a DataSourceXMLSchema plugin can be used to parse XML data coming from your server:</p>
```
YUI().use("datasource-io", "datasource-xmlschema", function(Y) {
var myDataSource = new Y.DataSource.IO({source:"ysearch_xml_madonna.php"}),
myCallback = {
success: function(e){
alert(e.response);
},
failure: function(e){
alert("Could not retrieve data: " + e.error.message);
}
};
myDataSource.plug(Y.Plugin.DataSourceXMLSchema, {
schema: {
resultListLocator: "result",
resultFields: [{key:"title", locator:"*[local-name() ='title']"}]
}
});
myDataSource.sendRequest({
request:"?output=xml",
callback:myCallback
});
});
```