datasource-cache.js revision 6608a5406cae6b74c06f0a501e189d1fc173dc21
150N/A/**
150N/A * Extends DataSource with caching functionality.
150N/A *
150N/A * @module datasource
150N/A */
150N/A
150N/A/**
150N/A * Adds cacheability to the YUI DataSource utility.
150N/A * @class DataSourceCache
150N/A * @extends Cache
150N/A */
150N/Avar DataSourceCache = function() {
150N/A DataSourceCache.superclass.constructor.apply(this, arguments);
150N/A};
150N/A
150N/AY.mix(DataSourceCache, {
150N/A /**
150N/A * The namespace for the plugin. This will be the property on the host which
150N/A * references the plugin instance.
150N/A *
150N/A * @property NS
150N/A * @type String
150N/A * @static
150N/A * @final
* @value "cache"
*/
NS: "cache",
/**
* Class name.
*
* @property NAME
* @type String
* @static
* @final
* @value "DataSourceCache"
*/
NAME: "DataSourceCache",
/////////////////////////////////////////////////////////////////////////////
//
// DataSourceCache Attributes
//
/////////////////////////////////////////////////////////////////////////////
ATTRS: {
}
});
Y.extend(DataSourceCache, Y.Cache, {
/**
* @method initializer
* @description Internal init() handler.
* @private
*/
initializer: function(config) {
this.doBefore("_defRequestFn", this._beforeDefRequestFn);
this.doBefore("_defResponseFn", this._beforeDefResponseFn);
},
/**
* 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?
var entry = (this.retrieve(o.request)) || null;
if(entry && entry.response) {
this._owner.fire("response", null, Y.mix(o, entry.response));
return new Y.Do.Halt("DataSourceCache plugin halted _defRequestFn");
}
},
/**
* 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
this.add(o.request, o, (o.callback && o.callback.argument));
}
});
Y.namespace('plugin');
Y.plugin.DataSourceCache = DataSourceCache;