datasource-cache.js revision 65b755664437543e907b9557027a4a608cc84c31
0N/AYUI.add('datasource-cache', function(Y) {
1204N/A
0N/A/**
0N/A * Extends DataSource with caching functionality.
0N/A *
0N/A * @module datasource
0N/A * @submodule datasource-cache
0N/A */
0N/A
0N/A/**
0N/A * Adds cacheability to the YUI DataSource utility.
0N/A * @class DataSourceCache
0N/A * @extends Cache
0N/A */
0N/Avar DataSourceCache = function() {
0N/A DataSourceCache.superclass.constructor.apply(this, arguments);
0N/A};
0N/A
0N/AY.mix(DataSourceCache, {
0N/A /**
0N/A * The namespace for the plugin. This will be the property on the host which
0N/A * references the plugin instance.
0N/A *
0N/A * @property NS
0N/A * @type String
0N/A * @static
0N/A * @final
0N/A * @value "cache"
0N/A */
0N/A NS: "cache",
0N/A
0N/A /**
0N/A * Class name.
0N/A *
0N/A * @property NAME
0N/A * @type String
0N/A * @static
0N/A * @final
0N/A * @value "DataSourceCache"
0N/A */
0N/A NAME: "DataSourceCache",
0N/A
0N/A /////////////////////////////////////////////////////////////////////////////
0N/A //
0N/A // DataSourceCache Attributes
0N/A //
0N/A /////////////////////////////////////////////////////////////////////////////
0N/A
0N/A ATTRS: {
0N/A
0N/A }
0N/A});
0N/A
0N/AY.extend(DataSourceCache, Y.Cache, {
0N/A /**
0N/A * Internal init() handler.
0N/A *
0N/A * @method initializer
0N/A * @param config {Object} Config object.
0N/A * @private
0N/A */
0N/A initializer: function(config) {
0N/A this.doBefore("_defRequestFn", this._beforeDefRequestFn);
0N/A this.doBefore("_defResponseFn", this._beforeDefResponseFn);
0N/A },
0N/A
0N/A /**
0N/A * First look for cached response, then send request to live data.
0N/A *
0N/A * @method _beforeDefRequestFn
0N/A * @param e {Event.Facade} Event Facade with the following properties:
0N/A * <dl>
0N/A * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
0N/A * <dt>request (Object)</dt> <dd>The request.</dd>
0N/A * <dt>callback (Object)</dt> <dd>The callback object.</dd>
0N/A * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
0N/A * </dl>
0N/A * @protected
0N/A */
0N/A _beforeDefRequestFn: function(e) {
0N/A // Is response already in the Cache?
0N/A var entry = (this.retrieve(e.request)) || null;
0N/A if(entry && entry.response) {
0N/A this.get("host").fire("response", Y.mix({response: entry.response}, e));
0N/A return new Y.Do.Halt("DataSourceCache plugin halted _defRequestFn");
417N/A }
417N/A },
0N/A
417N/A /**
0N/A * Adds data to cache before returning data.
417N/A *
0N/A * @method _beforeDefResponseFn
0N/A * @param e {Event.Facade} Event Facade with the following properties:
0N/A * <dl>
0N/A * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
0N/A * <dt>request (Object)</dt> <dd>The request.</dd>
0N/A * <dt>callback (Object)</dt> <dd>The callback object with the following properties:
0N/A * <dl>
0N/A * <dt>success (Function)</dt> <dd>Success handler.</dd>
0N/A * <dt>failure (Function)</dt> <dd>Failure handler.</dd>
0N/A * <dt>scope (Object)</dt> <dd>Execution context.</dd>
0N/A * </dl>
0N/A * </dd>
0N/A * <dt>data (Object)</dt> <dd>Raw data.</dd>
0N/A * <dt>response (Object)</dt> <dd>Normalized resopnse object with the following properties:
0N/A * <dl>
0N/A * <dt>results (Object)</dt> <dd>Parsed results.</dd>
0N/A * <dt>meta (Object)</dt> <dd>Parsed meta data.</dd>
0N/A * <dt>error (Object)</dt> <dd>Error object.</dd>
1204N/A * </dl>
1204N/A * </dd>
1204N/A * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
1204N/A * </dl>
1204N/A * @protected
0N/A */
0N/A _beforeDefResponseFn: function(e) {
0N/A // Add to Cache before returning
0N/A this.add(e.request, e.response, (e.callback && e.callback.argument));
0N/A }
0N/A});
0N/A
0N/AY.namespace('plugin').DataSourceCache = DataSourceCache;
0N/A
0N/A
0N/A
0N/A}, '@VERSION@' ,{requires:['datasource-local', 'cache']});
0N/A