Back to the index of articles and examples

YQL Query

Thanks to Jonathan LeBlanc's article on the YUI Blog I thought that it would be cool to have a YQLDataSource to make it easier to use YQL data in DataTable, Charts and AutoComplete. The table below is built using such a DataSource.

All the code is visible in the source of this same page. Further comments below.

The YQLDataSource does not require any parameters. It will automatically use the Get utility to access the YQL servers using JSON format and providing a suitable callback. The DataSource just needs to be instanced like:

var ds = new YAHOO.util.YQLDataSource();

There is no need to indicate the server, responseType or responseSchema, all of these will be taken care of. In a DataTable, the YQL query has to be set in the initialRequest configuration attribute:

initialRequest:'select * from where text = "YDN"'

All the fields returned in the results will be copied in the RecordSet, care should be taken if using Select * without any limits in the number of fields or the number of records, it might take lots of memory. The responseType will always be of type JSON and the responseSchema.resultsList will always point to the very first repeating element under query.results

Unfortunately, the YQL response provides no clue as to the data type of the individual fields. By default, all fields will be read as strings but if any field needs to be parsed, you can specify those fields in the usual responseSchema.fields = [ ..., {key:"xxxx",parser;"date"},...] or whichever the type might be. All fields coming in the response will be read in anyway, you only need to specify those that need a parser, all the rest will be added to the fields array and read as strings.

The new YQLDataSource derives from ScriptNodeDataSource.

Thanks to Jonathan for the idea