The DataSource Utility provides a common configurable interface for other components to fetch tabular data from a variety of local or remote sources, from simple JavaScript arrays to online servers over XHR. It is most often used in combination with the DataTable Control or the Charts Control to retrieve the data.
The DataSource will fetch data and then return that data to a callback function. It has the capability of going deep into the hierarchy of the source data, selecting specified fields from the raw input data, parsing them as indicated and calling the provided callback function when finished.
The DataSource has an optional cache to store retrieved and parsed data. It can also be set to periodically poll for data.
Notes:
YAHOO.util.DataSource, documented here, is intended to be the foundational class for the library. YAHOO.widget.DataSource, which is distributed as part of the AutoComplete Control, will be deprecated in favor of YAHOO.util.DataSource in a future release.Users new to DataSource can skip this section and proceed directly to the Getting Started section. Implementers who are upgrading from previous versions of DataSource should note the following changes as of version 2.5.0:
While much care has been taken to support backward compatibility whenever possible, implementers are advised to read through this user guide carefully, and upgrade their implementation code at their earliest convenience.
When defining JSON schemas, the value for resultsList must
now be of type String.
Calling myDataSource.sendRequest() no longer requires sending the tandem values oCallback (HTMLFunction) and oCaller (Object) values. Instead, DataSource's sendRequest() method now accepts an oCallback object literal with the following members:
success {HTMLFunction} Handler function that executes in success case. failure {HTMLFunction} Handler function that executes in failure case.argument {MIXED} Arbitrary data which will get passed along to handler function.scope {Object} Scope in which to execute handler function.As a result of the change to the DataSource's callback value, the following APIs have been changed:
handleResponse(oRequest, oRawResponse, oCallback, oCaller, tId)
makeConnection(oRequest, oCallback, oCaller)
sendRequest(oRequest, oCallback, oCaller)
setInterval(nMsec, oRequest, oCallback, oCaller) - In each of these methods, the oCallback argument is now an object literal, and the oCaller argument has been deprecated in favor of oCallback.scope.cacheRequestEvent
dataErrorEvent
getCachedResponseEvent
requestEvent
responseCacheEvent
responseEvent
responseParseEvent - In each of these Custom Events, the oArgs.callback argument is now an object literal, and the oArgs.caller argument is now deprecated in favor of oArgs.callback.scope.oFullResponse As of version 2.5.0, implementers will now have access to the entire data response after it as been type-converted from oRawResponse, but before it has been schema-parsed into oParsedResponse, and it is referred to as oFullResponse. In order to support this access to oFullResponse, the following APIs have been changed:
doBeforeCallback(oRequest, oFullResponse, oParsedResponse)
doBeforeParseData(oRequest, oFullResponse)
parseArrayData(oRequest, oFullResponse)
parseHTMLTableData(oRequest, oFullResponse)
parseJsonData(oRequest, oFullResponse)
parseTextData(oRequest, oFullResponse)
parseXMLData(oRequest, oFullResponse) - The second argument has been changed from oRawResponse to oFullResponse. oParsedResponse Signature When DataSource's handleResponse() method returns the schema-parsed response object to the given callback function, it will now have the following members:
tId {Number} - Unique transaction ID.results {Array} - Array of schema-parsed data results.error {Boolean} - True if there was an error.meta {Object} - A collection of (optional) additional field values.The DataSource code must be included before any control can use it:
Instead of copying and pasting the filepaths above, try letting the YUI dependency Configurator determine the optimal file list for your desired components; the Configurator uses YUI Loader write out the full HTML for including the precise files you need for your implementation.
Note: If you wish to include this component via the YUI Loader, its module name is datasource. (Click here for the full list of module names for YUI Loader.)
Where these files come from: The files included using the text above will be served from Yahoo! servers; see "Serving YUI Files from Yahoo!" for important information about this service. JavaScript files are minified, meaning that comments and white space have been removed to make them more efficient to download. To use the full, commented versions or the -debug versions of YUI JavaScript files, please download the library distribution and host the files on your own server.
Order matters: As is the case generally with JavaScript and CSS, order matters; these files should be included in the order specified above. If you include files in the wrong order, errors may result.
To create a DataSource instance on your page, pass a pointer to your live data to the constructor and set any appropriate configuration properties. The first mandatory argument to instantiate a DataSource is the location from which it should fetch data. It can be any one of several types:
<table> HTML elementThe second optional argument, if present, should be a configuration object; this object allows you to preconfigure the properties of the DataSource in a single step at instantiation. All properties are accessible here, though they can later be modified by explicit assignments.
When providing a URL for XHR DataSources, it is important that it has a trailing ? or / so that the arguments of the query (first argument of sendRequest; see below) can be appended (in the case of a GET request). If set to do POST requests, such trailing characters will be innocuous, so it is better to have them there just in case you switch methods.
Once created and configured, the DataSource can be used to retrieve data directly or it can be passed to another component (i.e., the DataTable Control or Charts Control) that will use it to fetch the data it needs.
connMethodPost This boolean property is only meaningful when the data comes from a remote source. By default, it is set to false and the HTTP request will be sent as a GET request. If set to true, the request will be sent as a POST.
responseType responseType tells the DataSource the type of data that is to be read. It is particularly important for remote connections or when the data is provided by a function, as such sources could return data in any of the supported formats. It should be set to one of the following constants (all of which are static members of YAHOO.util.DataSource):
TYPE_HTMLTABLE TYPE_JSARRAY TYPE_JSON TYPE_TEXT TYPE_XML These constants, plus others, are internally assigned by DataSource to the dataType property, according to the first argument of the constructor.
responseSchema The responseSchema property is an object literal containing an assortment of properties describing the incoming data.
resultsList TYPE_JSON)resultNode TYPE_XML)recordDelim, fieldDelim TYPE_TEXTfields fields property is required for all responseTypes. An array of field names (e.g., "Key") or object literals of the format {key:"Key", parser:parseFn} to indicate the name of each field and the parser to be used (if applicable). The parser property is a reference to either a built-in parse function or custom function that receives a single argument (which is the raw field value to be parsed) and returns a parsed value, which is especially useful for converting Strings into Numbers and Dates, etc. When dealing with name/value pairs (i.e., RESPONSE_TYPE = TYPE_JSARRAY), each field key value should map to an identifier in the data, but when dealing with unnamed data (i.e., RESPONSE_TYPE = TYPE_JSARRAY), each field key value will be mapped in the given order to the array data.metaFields oParsedResponse.meta collection. Use this to identify any additional information that would be beneficial to your processing code. Some widgets may look in this collection for "magic" meta used to propagate state or UI changes automatically. An example of this is DataTable's use of totalRecords, sortKey, sortDir, and some pagination related keys. You can retrieve data by calling the sendRequest method. It requires two arguments:
oRequest GET request) or anything valid to be sent in the document body section of the request for a POST request if connMethodPost = true. initialRequest value of the DataTable component will go. oCallback successfailurescopeargumentWhen the response returns, the handler function will get called with the following arguments:
oRequest sendRequestoParsedResponse oFullResponse per the locations described in responseSchema.metaFieldsoPayload argument of the oCallback object literalParse functions can be designated for individual fields in the fields array of the responseSchema. The DataSource will call the parser for each record and will pass it the raw value of that field and expects the parsed value in return. You can use the built-in parsers, parseDate which will handle any date that the native JavaScript Date.parse() method handles (notice not all date formats are supported) or parseNumber which will try to parse a number whether an integer or float.
The setInterval method can be used to repeatedly poll for data. The first argument is the number of milliseconds to wait between requests, and the following three arguments are the same as those of sendRequest. This method returns a numeric identifier that can be used to cancel the polling.
The polling can be cancelled by calling clearInterval and passing it the identifier returned by the call to setInterval. All polling can be cancelled by calling clearAllIntervals.
The DataSource component can cache data. You can set the maxCacheEntries property to the number of responses you want cached. When this number is exceeded, the oldest cached response will be dropped. The data in the cache is already fully processed, extracted, parsed and arranged ready to be returned to the caller. On a cache hit, those events or overridable methods meant to signal or process the retrieval of live data will not be fired or called.
The cache will be updated when polling but won't be used for retrieval.
Cache entries are indexed by the oRequest (first) argument of sendRequest. There will be one entry per request
up to the total of maxCachedEntries.
For JSON and XML response types, the important data is likely to be nested somewhere in the parsed response's data structure. To extract the values from this structure, the responseSchema uses location keys for the following properties:
Location keys are strings that describe where to look for a value in the parsed data structure.
Location keys for XML response types should be valid XML node names or attribute names.
Location keys for JSON response types should be valid JavaScript identifiers, including dot notation or square bracket notation for array indices or quoted strings ("this.is[4]['valid identifier']", but "this-is.not valid"). For convenience, the leading dot can be omitted for dot notation location keys (".foo" is equivalent to "foo" is equivalent to "['foo']"). Use of dot notation is encouraged.
For this example data...
...you might use a responseSchema like this:
Several events can be used to monitor the progress of the data request or its response. There are also overridable methods and plug-in parsers that are meant to transform the data before it is returned to the requesting application.
responseEventdoBeforeParseDataoRequest value as was passed in the first argument to sendRequest and the type-converted data before any schema-parsing, oFullResponse. You may want to use this method to parse out metadata from the response or munge the data if it is formatted in a way that the DataSource would be unable to parse it with the schema. Implementers should be sure to return type-converted data that is ready for schema parsing.doBeforeCallbackdoBeforeParseData. The third is the already parsed response. Whatever this method returns is what the callback function will get.When overriding these two doBefore methods, care should be taken not to tamper with the data in any manner that leaves it unusable in the next step.
The DataTable provides three functions that can be used as callbacks on a call to sendRequest:
onDataReturnInitializeTableonDataReturnInsertRowsonDataReturnAppendRowsonDataReturnSetRowsThe YUI Library and related topics are discussed on the on the ydn-javascript mailing list.
In addition, please visit the YUIBlog for updates and articles about the YUI Library written by the library's developers.
The YUI Library's public bug tracking and feature request repositories are located on the YUI SourceForge project site. Before filing new feature requests or bug reports, please review our reporting guidelines.














Copyright © 2008 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Copyright Policy - Job Openings