YUI 3.x Home -

YUI Library Examples: DataSource Utility: DataSource.Get

DataSource Utility: DataSource.Get

DataSource.Get uses the Get Utility to retrieve data, even cross-domain resources, via a dynamically created script node. A DataSchema 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 scriptCallbackParam.

JSON

Data
{
    "query":{
        "count":"10",
        "created":"2009-06-04T05:15:06Z",
        "lang":"en-US",
        "updated":"2009-06-04T05:15:06Z",
        "uri":"http://query.yahooapis.com/v1/yql?q=...query%3D%22pizza%22",
        ...
        "results":{
            "result":[
                {
                    ...
                    "dispurl":"www.<b>pizzahut.com<\/b>",
                    "size":"16217",
                    "title":"<b>Pizza<\/b> Hut",
                    "url":"http://www.pizzahut.com/"
                },
                {
                    ...
                    "dispurl":"www.<b>dominos.com<\/b>",
                    "size":"29739",
                    "title":"Domino's <b>Pizza<\/b>",
                    "url":"http://www.dominos.com/"
                },
                {
                    ...
                    "dispurl":"<b>mobile.papajohns.com<\/b>",
                    "size":"2315",
                    "title":"Papa John's Mobile Web",
                    "url":"https://mobile.papajohns.com/"
                },
                ...
            ]
        }
    }
}
    
Schema
{
    resultListLocator: "query.results.result",
    resultFields: ["title"]
}
    
Normalized data

Use a DataSourceJSONSchema plugin to parse the data against a schema that you provide:

  1. YUI().use("datasource-get", "datasource-jsonschema", function(Y) {
  2. var myDataSource = new Y.DataSource.Get({
  3. source:"http://query.yahooapis.com/v1/public/yql?format=json&"}),
  4. myCallback = {
  5. success: function(e){
  6. alert(e.response);
  7. },
  8. failure: function(e){
  9. alert("Could not retrieve data: " + e.error.message);
  10. }
  11. };
  12.  
  13. myDataSource.plug(Y.Plugin.DataSourceJSONSchema, {
  14. schema: {
  15. resultListLocator: "query.results.result",
  16. resultFields: ["title"]
  17. }
  18. });
  19.  
  20. myDataSource.sendRequest("
  21. q=select%20*%20from%20search.web%20where%20query%3D%22pizza%22", myCallback);
  22. });
YUI().use("datasource-get", "datasource-jsonschema", function(Y) {
    var myDataSource = new Y.DataSource.Get({
        source:"http://query.yahooapis.com/v1/public/yql?format=json&"}),
        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: "query.results.result",
            resultFields: ["title"]
        }
    });
 
    myDataSource.sendRequest("
        q=select%20*%20from%20search.web%20where%20query%3D%22pizza%22", myCallback);
});

Copyright © 2009 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings