datasource-base.js revision 1bc27dc58fd882eb8902cdefa2dde4a8ee2a5bbd
71c8a528203c289a8585ce10ac6bafc4274058c6vboxsync * The DataSource utility provides a common configurable interface for widgets to
71c8a528203c289a8585ce10ac6bafc4274058c6vboxsync * access a variety of data, from JavaScript arrays to online database servers.
aa0553becec2abc2e781f839ba1d399c31c2c07fvboxsync * @module datasource
71c8a528203c289a8585ce10ac6bafc4274058c6vboxsync * @requires base
71c8a528203c289a8585ce10ac6bafc4274058c6vboxsync * @title DataSource Utility
71c8a528203c289a8585ce10ac6bafc4274058c6vboxsync * Base class for the YUI DataSource utility.
71c8a528203c289a8585ce10ac6bafc4274058c6vboxsync * @class DataSource.Base
71c8a528203c289a8585ce10ac6bafc4274058c6vboxsync * @extends Base
71c8a528203c289a8585ce10ac6bafc4274058c6vboxsync * @constructor
71c8a528203c289a8585ce10ac6bafc4274058c6vboxsync DSBase = function() {
71c8a528203c289a8585ce10ac6bafc4274058c6vboxsync DSBase.superclass.constructor.apply(this, arguments);
23179f1443b03947d85eccc81cbc6b5153a4abf3vboxsync /////////////////////////////////////////////////////////////////////////////
71c8a528203c289a8585ce10ac6bafc4274058c6vboxsync // DataSource static properties
71c8a528203c289a8585ce10ac6bafc4274058c6vboxsync /////////////////////////////////////////////////////////////////////////////
71c8a528203c289a8585ce10ac6bafc4274058c6vboxsync * Global transaction counter.
71c8a528203c289a8585ce10ac6bafc4274058c6vboxsync * @property DataSource._tId
147e101bcd061b5e085e4a2c0cc9fc35546ff1aavboxsync * @type Number
71c8a528203c289a8585ce10ac6bafc4274058c6vboxsync * @default 0
71c8a528203c289a8585ce10ac6bafc4274058c6vboxsync * Indicates null data response.
71c8a528203c289a8585ce10ac6bafc4274058c6vboxsync * @property DataSource.ERROR_DATANULL
71c8a528203c289a8585ce10ac6bafc4274058c6vboxsync * @type Number
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync * @default 0
b304856b23107864c9c594a80cebca6006623f31vboxsync * Indicates invalid data response.
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync * @property DataSource.ERROR_DATAINVALID
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync * @type Number
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync * @default 1
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync /////////////////////////////////////////////////////////////////////////////
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync // DataSource.Base static properties
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync /////////////////////////////////////////////////////////////////////////////
db0ecde8f28fdb4525bc6d94056166c70b02ebb8vboxsync * Class name.
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync * @property NAME
9353e321b583ed6f2b42414257a5212885575b5cvboxsync * @type String
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync * @value "DataSource.Base"
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync /////////////////////////////////////////////////////////////////////////////
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync // DataSource.Base Attributes
2a5e5a032e6f23f8937718e4ee4d6979188bdd19vboxsync /////////////////////////////////////////////////////////////////////////////
2a5e5a032e6f23f8937718e4ee4d6979188bdd19vboxsync * @attribute source
2a5e5a032e6f23f8937718e4ee4d6979188bdd19vboxsync * @description Pointer to live data.
2a5e5a032e6f23f8937718e4ee4d6979188bdd19vboxsync * @type MIXED
2a5e5a032e6f23f8937718e4ee4d6979188bdd19vboxsync * @default null
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync * @attribute ERROR_DATAINVALID
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync * @description Error message for invalid data responses.
aa0553becec2abc2e781f839ba1d399c31c2c07fvboxsync * @type String
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync * @default "Invalid data"
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync * @attribute ERROR_DATANULL
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync * @description Error message for null data responses.
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync * @type String
aa0553becec2abc2e781f839ba1d399c31c2c07fvboxsync * @default "Null data"
cebc93936b5bb4d867e1c086dd1b206db33c31dcvboxsync * Executes a given callback. The third param determines whether to execute
cebc93936b5bb4d867e1c086dd1b206db33c31dcvboxsync * @method issueCallback
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync * @param callback {Object} The callback object.
19cb1f8699e352d590c4946caee33863a5157241vboxsync * @param params {Array} params to be passed to the callback method
19cb1f8699e352d590c4946caee33863a5157241vboxsync * @param error {Boolean} whether an error occurred
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync callbackFunc = (response.error && response.callback.failure) || response.callback.success;
a72b5355eb89aafe6bfcc8912cf02645d7cccceavboxsync * @property _queue
a72b5355eb89aafe6bfcc8912cf02645d7cccceavboxsync * @description Object literal to manage asynchronous request/response
3933885bc0c2c93436d858a14564c6179ec72872vboxsync * cycles enabled if queue needs to be managed (asyncMode/xhrConnMode):
3933885bc0c2c93436d858a14564c6179ec72872vboxsync <dt>interval {Number}</dt>
3933885bc0c2c93436d858a14564c6179ec72872vboxsync <dd>Interval ID of in-progress queue.</dd>
3933885bc0c2c93436d858a14564c6179ec72872vboxsync <dt>conn</dt>
3933885bc0c2c93436d858a14564c6179ec72872vboxsync <dd>In-progress connection identifier (if applicable).</dd>
9ca017ceee656f9d33f2cb6652e401b5f17fcfb7vboxsync <dt>requests {Object[]}</dt>
3933885bc0c2c93436d858a14564c6179ec72872vboxsync <dd>Array of queued request objects: {request:oRequest, callback:_xhrCallback}.</dd>
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync * @type Object
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync * @default {interval:null, conn:null, requests:[]}
dbabc9de5bf52ce5eb77cf82b038e9a6166c5a04vboxsync * @method initializer
4090390866c02d5d0ad061151cdb298b9a173e86vboxsync * @description Internal init() handler.
40dce69ff1c2949a489337922f30f1021d62d864vboxsync initializer: function() {
40dce69ff1c2949a489337922f30f1021d62d864vboxsync this._queue = {interval:null, conn:null, requests:[]};
a1b4fb3917412d2632d358ff8989f1ec971f2d5bvboxsync * @method destructor
a1b4fb3917412d2632d358ff8989f1ec971f2d5bvboxsync * @description Internal destroy() handler.
7a29aa5ce149ccd344a2929d2815b8e212690b92vboxsync destructor: function() {
147e101bcd061b5e085e4a2c0cc9fc35546ff1aavboxsync * @method _createEvents
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync * @description This method creates all the events for this module
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync * Target and publishes them so we get Event Bubbling.
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync _initEvents: function() {
261b44f7fa60a1d4bb4102142d3aa44188908484vboxsync * Fired when a data request is received.
261b44f7fa60a1d4bb4102142d3aa44188908484vboxsync * @event request
261b44f7fa60a1d4bb4102142d3aa44188908484vboxsync * @param e {Event.Facade} Event Facade.
261b44f7fa60a1d4bb4102142d3aa44188908484vboxsync * @param o {Object} Object with the following properties:
261b44f7fa60a1d4bb4102142d3aa44188908484vboxsync * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
c33fc49611f2444dade533488bf431e29eb88bcdvboxsync * <dt>request (Object)</dt> <dd>The request.</dd>
261b44f7fa60a1d4bb4102142d3aa44188908484vboxsync * <dt>callback (Object)</dt> <dd>The callback object.</dd>
db0ecde8f28fdb4525bc6d94056166c70b02ebb8vboxsync this.publish("request", {defaultFn: this._handleRequest});
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync * Fired when raw data is received.
657b2c9f6d33f08001e5fa6f6e0572dcf0391013vboxsync * @event data
e2760cdc84c692bc46cfaf5018d313db2f122acavboxsync * @param e {Event.Facade} Event Facade.
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync * @param o {Object} Object with the following properties:
9353e321b583ed6f2b42414257a5212885575b5cvboxsync * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
261b44f7fa60a1d4bb4102142d3aa44188908484vboxsync * <dt>request (Object)</dt> <dd>The request.</dd>
9353e321b583ed6f2b42414257a5212885575b5cvboxsync * <dt>callback (Object)</dt> <dd>The callback object.</dd>
261b44f7fa60a1d4bb4102142d3aa44188908484vboxsync * <dt>data (Object)</dt> <dd>The raw data.</dd>
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync this.publish("data", {defaultFn: this._handleData});
261b44f7fa60a1d4bb4102142d3aa44188908484vboxsync * Fired when response is returned.
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync * @event response
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync * @param e {Event.Facade} Event Facade.
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync * @param o {Object} Object with the following properties:
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync * <dt>request (Object)</dt> <dd>The request.</dd>
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync * <dt>callback (Object)</dt> <dd>The callback object.</dd>
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync * <dt>data (Object)</dt> <dd>The raw data.</dd>
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync * <dt>results (Object)</dt> <dd>Parsed results.</dd>
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync * <dt>meta (Object)</dt> <dd>Parsed meta results data.</dd>
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync * <dt>error (Boolean)</dt> <dd>Error flag.</dd>
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync this.publish("response", {defaultFn: this._handleResponse});
aa0553becec2abc2e781f839ba1d399c31c2c07fvboxsync * Fired when an error is encountered.
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync * @event error
f62342e2cc901a67e27fa69c0e712ee35e9c4c68vboxsync * @param e {Event.Facade} Event Facade.
b604fbf16eda38d14b4999c245f032bfaa5aa85avboxsync * @param o {Object} Object with the following properties:
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
261b44f7fa60a1d4bb4102142d3aa44188908484vboxsync * <dt>request (Object)</dt> <dd>The request.</dd>
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync * <dt>callback (Object)</dt> <dd>The callback object.</dd>
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync * <dt>data (Object)</dt> <dd>The raw data (if available).</dd>
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync * <dt>results (Object)</dt> <dd>Parsed results (if available).</dd>
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync * <dt>meta (Object)</dt> <dd>Parsed meta results data (if available).</dd>
aa0553becec2abc2e781f839ba1d399c31c2c07fvboxsync * <dt>error (Boolean)</dt> <dd>Error flag.</dd>
cebc93936b5bb4d867e1c086dd1b206db33c31dcvboxsync * Overridable default <code>request</code> event handler manages request/response
cebc93936b5bb4d867e1c086dd1b206db33c31dcvboxsync * transaction. Must fire <code>response</code> event when response is received. This
cebc93936b5bb4d867e1c086dd1b206db33c31dcvboxsync * method should be implemented by subclasses to achieve more complex
cebc93936b5bb4d867e1c086dd1b206db33c31dcvboxsync * behavior such as accessing remote data.
19cb1f8699e352d590c4946caee33863a5157241vboxsync * @method _handleRequest
19cb1f8699e352d590c4946caee33863a5157241vboxsync * @protected
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync * @param e {Event.Facade} Event Facade.
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync * @param o {Object} Object with the following properties:
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync * <dt>request (Object)</dt> <dd>The request.</dd>
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync * <dt>callback (Object)</dt> <dd>The callback object.</dd>
3933885bc0c2c93436d858a14564c6179ec72872vboxsync _handleRequest: function(e, o) {
3933885bc0c2c93436d858a14564c6179ec72872vboxsync // Problematic data
9ca017ceee656f9d33f2cb6652e401b5f17fcfb7vboxsync * Overridable default <code>data</code> event handler normalizes raw data
3933885bc0c2c93436d858a14564c6179ec72872vboxsync * into a response that includes results and meta properties.
261b44f7fa60a1d4bb4102142d3aa44188908484vboxsync * @method _handleData
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync * @protected
f106b549ead77cab51ff1e2c116060aaabb90d5evboxsync * @param e {Event.Facade} Event Facade.
f106b549ead77cab51ff1e2c116060aaabb90d5evboxsync * @param o {Object} Object with the following properties:
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
9e4ea89b1085fdaa5861e45a729d9c978db1a8f1vboxsync * <dt>request (Object)</dt> <dd>The request.</dd>
ad9e5a61fea617d40d07390ff1737277d6aef869vboxsync * <dt>callback (Object)</dt> <dd>The callback object.</dd>
dbabc9de5bf52ce5eb77cf82b038e9a6166c5a04vboxsync * <dt>data (Object)</dt> <dd>The raw response data.</dd>
40dce69ff1c2949a489337922f30f1021d62d864vboxsync _handleData: function(e, o) {
40dce69ff1c2949a489337922f30f1021d62d864vboxsync // Pass through data as-is
40dce69ff1c2949a489337922f30f1021d62d864vboxsync // Normalize
3933885bc0c2c93436d858a14564c6179ec72872vboxsync * Overridable default <code>response</code> event handler returns data as a
a1b4fb3917412d2632d358ff8989f1ec971f2d5bvboxsync * normalized response to callabck.
a1b4fb3917412d2632d358ff8989f1ec971f2d5bvboxsync * @method _handleResponse
a1b4fb3917412d2632d358ff8989f1ec971f2d5bvboxsync * @protected
a1b4fb3917412d2632d358ff8989f1ec971f2d5bvboxsync * @param e {Event.Facade} Event Facade.
a1b4fb3917412d2632d358ff8989f1ec971f2d5bvboxsync * @param o {Object} Object with the following properties:
a1b4fb3917412d2632d358ff8989f1ec971f2d5bvboxsync * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
a1b4fb3917412d2632d358ff8989f1ec971f2d5bvboxsync * <dt>request (Object)</dt> <dd>The request.</dd>
a1b4fb3917412d2632d358ff8989f1ec971f2d5bvboxsync * <dt>callback (Object)</dt> <dd>The callback object.</dd>
aa0553becec2abc2e781f839ba1d399c31c2c07fvboxsync * <dt>data (Object)</dt> <dd>Raw data.</dd>
a1b4fb3917412d2632d358ff8989f1ec971f2d5bvboxsync * <dt>results (Object)</dt> <dd>Parsed results.</dd>
a1b4fb3917412d2632d358ff8989f1ec971f2d5bvboxsync * <dt>meta (Object)</dt> <dd>Parsed meta data.</dd>
a1b4fb3917412d2632d358ff8989f1ec971f2d5bvboxsync _handleResponse: function(e, o) {
a1b4fb3917412d2632d358ff8989f1ec971f2d5bvboxsync // Send the response back to the callback
a1b4fb3917412d2632d358ff8989f1ec971f2d5bvboxsync * Generates a unique transaction ID and fires <code>request</code> event.
71c8a528203c289a8585ce10ac6bafc4274058c6vboxsync * @method sendRequest
71c8a528203c289a8585ce10ac6bafc4274058c6vboxsync * @param request {Object} Request.
147e101bcd061b5e085e4a2c0cc9fc35546ff1aavboxsync * @param callback {Object} An object literal with the following properties:
71c8a528203c289a8585ce10ac6bafc4274058c6vboxsync * <dt><code>success</code></dt>
2823fbb1428e982169f04923472d7c94e7ed8385vboxsync * <dd>The function to call when the data is ready.</dd>
2823fbb1428e982169f04923472d7c94e7ed8385vboxsync * <dt><code>failure</code></dt>
147e101bcd061b5e085e4a2c0cc9fc35546ff1aavboxsync * <dd>The function to call upon a response failure condition.</dd>
147e101bcd061b5e085e4a2c0cc9fc35546ff1aavboxsync * <dt><code>scope</code></dt>
71c8a528203c289a8585ce10ac6bafc4274058c6vboxsync * <dd>The object to serve as the scope for the success and failure handlers.</dd>
71c8a528203c289a8585ce10ac6bafc4274058c6vboxsync * <dt><code>argument</code></dt>
71c8a528203c289a8585ce10ac6bafc4274058c6vboxsync * <dd>Arbitrary data payload that will be passed back to the success and failure handlers.</dd>
71c8a528203c289a8585ce10ac6bafc4274058c6vboxsync * @return {Number} Transaction ID.