datasource.js revision 2d05ae30a932507d8c42cef6b78b46e0876a5669
/**
* The DataSource utility provides a common configurable interface for widgets to
* access a variety of data, from JavaScript arrays to online database servers.
*
* @module datasource
*/
/**
* Base class for the YUI DataSource utility.
* @class DataSource.Local
* @extends Base
* @constructor
*/
DSLocal = function() {
};
/////////////////////////////////////////////////////////////////////////////
//
// DataSource static properties
//
/////////////////////////////////////////////////////////////////////////////
/**
* Class name.
*
* @property NAME
* @type String
* @static
* @final
* @value "DataSource.Local"
*/
NAME: "DataSource.Local",
/////////////////////////////////////////////////////////////////////////////
//
// DataSource Attributes
//
/////////////////////////////////////////////////////////////////////////////
ATTRS: {
/**
* @attribute source
* @description Pointer to live data.
* @type MIXED
* @default null
*/
source: {
value: null
}
},
/**
* Global transaction counter.
*
* @property DataSource._tId
* @type Number
* @static
* @private
* @default 0
*/
_tId: 0,
/**
* Executes a given callback. The third param determines whether to execute
*
* @method DataSource.issueCallback
* @param callback {Object} The callback object.
* @param params {Array} params to be passed to the callback method
* @param error {Boolean} whether an error occurred
* @static
*/
issueCallback: function (response) {
if (callbackFunc) {
}
}
}
});
/**
* @method initializer
* @description Internal init() handler.
* @private
*/
initializer: function() {
this._initEvents();
},
/**
* @method destructor
* @description Internal destroy() handler.
* @private
*/
destructor: function() {
},
/**
* @method _createEvents
* @description This method creates all the events for this module
* Target and publishes them so we get Event Bubbling.
* @private
*/
_initEvents: function() {
/**
* Fired when a data request is received.
*
* @event request
* @param e {Event.Facade} Event Facade.
* @param o {Object} Object with the following properties:
* <dl>
* <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
* <dt>request (Object)</dt> <dd>The request.</dd>
* <dt>callback (Object)</dt> <dd>The callback object.</dd>
* </dl>
* @preventable _defRequestFn
*/
//this.publish("request", {defaultFn: this._defRequestFn});
this._defRequestFn(e, o);
}});
/**
* Fired when raw data is received.
*
* @event data
* @param e {Event.Facade} Event Facade.
* @param o {Object} Object with the following properties:
* <dl>
* <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
* <dt>request (Object)</dt> <dd>The request.</dd>
* <dt>callback (Object)</dt> <dd>The callback object.</dd>
* <dt>data (Object)</dt> <dd>The raw data.</dd>
* </dl>
* @preventable _defDataFn
*/
//this.publish("data", {defaultFn: this._defDataFn});
this._defDataFn(e, o);
}});
/**
* Fired when response is returned.
*
* @event response
* @param e {Event.Facade} Event Facade.
* @param o {Object} Object with the following properties:
* <dl>
* <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
* <dt>request (Object)</dt> <dd>The request.</dd>
* <dt>callback (Object)</dt> <dd>The callback object.</dd>
* <dt>data (Object)</dt> <dd>The raw data.</dd>
* <dt>results (Object)</dt> <dd>Parsed results.</dd>
* <dt>meta (Object)</dt> <dd>Parsed meta results data.</dd>
* <dt>error (Boolean)</dt> <dd>Error flag.</dd>
* </dl>
* @preventable _defResponseFn
*/
//this.publish("response", {defaultFn: this._defResponseFn});
this._defResponseFn(e, o);
}});
/**
* Fired when an error is encountered.
*
* @event error
* @param e {Event.Facade} Event Facade.
* @param o {Object} Object with the following properties:
* <dl>
* <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
* <dt>request (Object)</dt> <dd>The request.</dd>
* <dt>callback (Object)</dt> <dd>The callback object.</dd>
* <dt>data (Object)</dt> <dd>The raw data (if available).</dd>
* <dt>results (Object)</dt> <dd>Parsed results (if available).</dd>
* <dt>meta (Object)</dt> <dd>Parsed meta results data (if available).</dd>
* <dt>error (Boolean)</dt> <dd>Error flag.</dd>
* </dl>
*/
},
/**
* event when response is received. This method should be implemented by
* subclasses to achieve more complex behavior such as accessing remote data.
*
* @method _defRequestFn
* @param e {Event.Facade} Event Facade.
* @param o {Object} Object with the following properties:
* <dl>
* <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
* <dt>request (Object)</dt> <dd>The request.</dd>
* <dt>callback (Object)</dt> <dd>The callback object.</dd>
* </dl>
* @protected
*/
_defRequestFn: function(e, o) {
// Problematic data
o.error = true;
}
if(o.error) {
this.fire("error", null, o);
}
},
/**
* Normalizes raw data into a response that includes results and meta properties.
*
* @method _defDataFn
* @param e {Event.Facade} Event Facade.
* @param o {Object} Object with the following properties:
* <dl>
* <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
* <dt>request (Object)</dt> <dd>The request.</dd>
* <dt>callback (Object)</dt> <dd>The callback object.</dd>
* <dt>data (Object)</dt> <dd>The raw response data.</dd>
* </dl>
* @protected
*/
_defDataFn: function(e, o) {
// Pass through data as-is
// Normalize
if(!o.results) {
o.results = [];
}
if(!o.meta) {
o.meta = {};
}
this.fire("response", null, o);
},
/**
* Sends data as a normalized response to callback.
*
* @method _defResponseFn
* @param e {Event.Facade} Event Facade.
* @param o {Object} Object with the following properties:
* <dl>
* <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
* <dt>request (Object)</dt> <dd>The request.</dd>
* <dt>callback (Object)</dt> <dd>The callback object.</dd>
* <dt>data (Object)</dt> <dd>Raw data.</dd>
* <dt>results (Object)</dt> <dd>Parsed results.</dd>
* <dt>meta (Object)</dt> <dd>Parsed meta data.</dd>
* </dl>
* @protected
*/
_defResponseFn: function(e, o) {
// Send the response back to the callback
DSLocal.issueCallback(o);
},
/**
* Generates a unique transaction ID and fires <code>request</code> event.
*
* @method sendRequest
* @param request {Object} Request.
* @param callback {Object} An object literal with the following properties:
* <dl>
* <dt><code>success</code></dt>
* <dd>The function to call when the data is ready.</dd>
* <dt><code>failure</code></dt>
* <dd>The function to call upon a response failure condition.</dd>
* <dt><code>scope</code></dt>
* <dd>The object to serve as the scope for the success and failure handlers.</dd>
* <dt><code>argument</code></dt>
* <dd>Arbitrary data payload that will be passed back to the success and failure handlers.</dd>
* </dl>
* @return {Number} Transaction ID.
*/
return tId;
}
});
Y.namespace("DataSource");
/**
* The DataSource utility provides a common configurable interface for widgets to
* access a variety of data, from JavaScript arrays to online database servers.
*
* @module datasource
*/
/**
* XHR subclass for the YUI DataSource utility.
* @class DataSource.XHR
* @extends DataSource.Local
* @constructor
*/
var DSXHR = function() {
};
/////////////////////////////////////////////////////////////////////////////
//
// DataSource.XHR static properties
//
/////////////////////////////////////////////////////////////////////////////
/**
* Class name.
*
* @property NAME
* @type String
* @static
* @final
* @value "DataSource.XHR"
*/
NAME: "DataSource.XHR",
/////////////////////////////////////////////////////////////////////////////
//
// DataSource.XHR Attributes
//
/////////////////////////////////////////////////////////////////////////////
ATTRS: {
/**
* Pointer to IO Utility.
*
* @attribute io
* @type Y.io
* @default Y.io
*/
io: {
}
}
});
/**
* @method initializer
* @description Internal init() handler.
* @private
*/
initializer: function() {
},
/**
* @property _queue
* cycles enabled if queue needs to be managed (asyncMode/xhrConnMode):
<dl>
<dt>interval {Number}</dt>
<dd>Interval ID of in-progress queue.</dd>
<dt>conn</dt>
<dd>In-progress connection identifier (if applicable).</dd>
<dt>requests {Object[]}</dt>
<dd>Array of queued request objects: {request:oRequest, callback:_xhrCallback}.</dd>
</dl>
* @type Object
* @default {interval:null, conn:null, requests:[]}
* @private
*/
_queue: null,
/**
* Passes query string to IO. Fires <code>response</code> event when
* response is received asynchronously.
*
* @method _defRequestFn
* @param e {Event.Facade} Event Facade.
* @param o {Object} Object with the following properties:
* <dl>
* <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
* <dt>request (Object)</dt> <dd>The request.</dd>
* <dt>callback (Object)</dt> <dd>The callback object.</dd>
* </dl>
* @protected
*/
_defRequestFn: function(e, o) {
cfg = {
on: {
//{tId:args.tId, request:args.request, callback:args.callback, response:response}
//this.handleResponse(args.tId, args.request, args.callback, response);
},
o.error = true;
//{tId:args.tId, request:args.request, callback:args.callback, response:response}
//this.handleResponse(args.tId, args.request, args.callback, response);
}
},
context: this,
arguments: {
}
};
return o.tId;
}
});
/**
* Extends DataSource with caching functionality.
*
* @module datasource
*/
/**
* Adds cacheability to the YUI DataSource utility.
* @class DataSourceCache
* @extends Cache
*/
var DataSourceCache = function() {
};
Y.mix(DataSourceCache, {
/**
* The namespace for the plugin. This will be the property on the host which
* references the plugin instance.
*
* @property NS
* @type String
* @static
* @final
* @value "cache"
*/
NS: "cache",
/**
* Class name.
*
* @property NAME
* @type String
* @static
* @final
* @value "DataSourceCache"
*/
NAME: "DataSourceCache",
/////////////////////////////////////////////////////////////////////////////
//
// DataSourceCache Attributes
//
/////////////////////////////////////////////////////////////////////////////
ATTRS: {
}
});
/**
* @method initializer
* @description Internal init() handler.
* @private
*/
initializer: function(config) {
},
/**
* First look for cached response, then send request to live data.
*
* @method _beforeDefRequestFn
* @param e {Event.Facade} Event Facade.
* @param o {Object} Object with the following properties:
* <dl>
* <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
* <dt>request (Object)</dt> <dd>The request.</dd>
* <dt>callback (Object)</dt> <dd>The callback object.</dd>
* </dl>
* @protected
*/
_beforeDefRequestFn: function(e, o) {
// Is response already in the Cache?
}
},
/**
* Adds data to cache before returning data.
*
* @method _beforeResponse
* @param e {Event.Facade} Event Facade.
* @param o {Object} Object with the following properties:
* <dl>
* <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
* <dt>request (Object)</dt> <dd>The request.</dd>
* <dt>callback (Object)</dt> <dd>The callback object.</dd>
* <dt>data (Object)</dt> <dd>Raw data.</dd>
* <dt>results (Object)</dt> <dd>Parsed results.</dd>
* <dt>meta (Object)</dt> <dd>Parsed meta data.</dd>
* </dl>
* @protected
*/
_beforeDefResponseFn: function(e, o) {
// Add to Cache before returning
}
});
Y.namespace('plugin');
/**
* Extends DataSource with schema-based JSON parsing functionality.
*
* @module datasource
*/
/**
* Adds parsability to the YUI DataSource utility.
* @class DataSourceJSONParser
* @extends Plugin
*/
var DataSourceJSONParser = function() {
};
Y.mix(DataSourceJSONParser, {
/**
* The namespace for the plugin. This will be the property on the host which
* references the plugin instance.
*
* @property NS
* @type String
* @static
* @final
* @value "parser"
*/
NS: "parser",
/**
* Class name.
*
* @property NAME
* @type String
* @static
* @final
* @value "DataSourceJSONParser"
*/
NAME: "DataSourceJSONParser",
/////////////////////////////////////////////////////////////////////////////
//
// DataSourceCache Attributes
//
/////////////////////////////////////////////////////////////////////////////
ATTRS: {
parser: {
readOnly: true,
useRef: true
},
schema: {
//value: {}
}
}
});
/**
* @method initializer
* @description Internal init() handler.
* @private
*/
initializer: function(config) {
},
/**
* Parses raw data into a normalized response.
*
* @method _beforeDefDataFn
* @param e {Event.Facade} Event Facade.
* @param o {Object} Object with the following properties:
* <dl>
* <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
* <dt>request (Object)</dt> <dd>The request.</dd>
* <dt>callback (Object)</dt> <dd>The callback object.</dd>
* <dt>data (Object)</dt> <dd>The raw response.</dd>
* </dl>
* @protected
*/
_beforeDefDataFn: function(e, o) {
if(!response) {
response = {
meta: {},
};
}
}
});
Y.namespace('plugin');
/**
* Extends DataSource with polling functionality.
*
* @module datasource
*/
/**
* Adds polling to the YUI DataSource utility.
* @class Pollable
* @extends DataSource.Local
*/
Pollable = function() {};
/**
* @property _intervals
* @description Array of polling interval IDs that have been enabled,
* stored here to be able to clear all intervals.
* @private
*/
_intervals: null,
/**
* Sets up a polling mechanism to send requests at set intervals and forward
* responses to given callback.
*
* @method setInterval
* @param msec {Number} Length of interval in milliseconds.
* @param request {Object} Request object.
* @param callback {Object} An object literal with the following properties:
* <dl>
* <dt><code>success</code></dt>
* <dd>The function to call when the data is ready.</dd>
* <dt><code>failure</code></dt>
* <dd>The function to call upon a response failure condition.</dd>
* <dt><code>scope</code></dt>
* <dd>The object to serve as the scope for the success and failure handlers.</dd>
* <dt><code>argument</code></dt>
* <dd>Arbitrary data that will be passed back to the success and failure handlers.</dd>
* </dl>
* @return {Number} Interval ID.
*/
var self = this,
id = setInterval(function() {
//self._makeConnection(request, callback);
}, msec);
if(!this._intervals) {
this._intervals = [];
}
return id;
}
else {
}
},
/**
* Disables polling mechanism associated with the given interval ID.
*
* @method clearInterval
* @param id {Number} Interval ID.
*/
clearInterval: function(id) {
// Remove from tracker if there
var tracker = this._intervals || [],
for(; i>-1; i--) {
}
}
}
};
YUI.add('datasource', function(Y){}, '@VERSION@' ,{use:['datasource-local','datasource-xhr','datasource-cache','datasource-jsonparser','datasource-polling']});