76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav GlassYUI.add('datasource-cache', function(Y) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass/**
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * Plugs DataSource with caching functionality.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass *
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @module datasource
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @submodule datasource-cache
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass */
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass/**
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * DataSourceCache extension binds Cache to DataSource.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @class DataSourceCacheExtension
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass */
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glassvar DataSourceCacheExtension = function() {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass};
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav GlassY.mix(DataSourceCacheExtension, {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass /**
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * The namespace for the plugin. This will be the property on the host which
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * references the plugin instance.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass *
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @property NS
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @type String
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @static
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @final
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @value "cache"
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass */
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass NS: "cache",
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass /**
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * Class name.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass *
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @property NAME
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @type String
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @static
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @final
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @value "dataSourceCacheExtension"
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass */
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass NAME: "dataSourceCacheExtension"
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass});
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav GlassDataSourceCacheExtension.prototype = {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass /**
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * Internal init() handler.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass *
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @method initializer
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @param config {Object} Config object.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @private
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass */
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass initializer: function(config) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass this.doBefore("_defRequestFn", this._beforeDefRequestFn);
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass this.doBefore("_defResponseFn", this._beforeDefResponseFn);
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass },
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass /**
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * First look for cached response, then send request to live data.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass *
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @method _beforeDefRequestFn
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @param e {Event.Facade} Event Facade with the following properties:
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * <dl>
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * <dt>request (Object)</dt> <dd>The request.</dd>
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * <dt>callback (Object)</dt> <dd>The callback object.</dd>
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * </dl>
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @protected
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass */
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass _beforeDefRequestFn: function(e) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // Is response already in the Cache?
2fe13ddab136a6eb6239d89e5e064e09d9e1bb92Luke Smith var entry = (this.retrieve(e.request)) || null,
2fe13ddab136a6eb6239d89e5e064e09d9e1bb92Luke Smith payload = e.details[0];
2fe13ddab136a6eb6239d89e5e064e09d9e1bb92Luke Smith
2fe13ddab136a6eb6239d89e5e064e09d9e1bb92Luke Smith if (entry && entry.response) {
2fe13ddab136a6eb6239d89e5e064e09d9e1bb92Luke Smith payload.cached = entry.cached;
2fe13ddab136a6eb6239d89e5e064e09d9e1bb92Luke Smith payload.response = entry.response;
2fe13ddab136a6eb6239d89e5e064e09d9e1bb92Luke Smith payload.data = entry.data;
2fe13ddab136a6eb6239d89e5e064e09d9e1bb92Luke Smith
2fe13ddab136a6eb6239d89e5e064e09d9e1bb92Luke Smith this.get("host").fire("response", payload);
2fe13ddab136a6eb6239d89e5e064e09d9e1bb92Luke Smith
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass return new Y.Do.Halt("DataSourceCache extension halted _defRequestFn");
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass },
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass /**
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * Adds data to cache before returning data.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass *
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @method _beforeDefResponseFn
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @param e {Event.Facade} Event Facade with the following properties:
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * <dl>
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * <dt>request (Object)</dt> <dd>The request.</dd>
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * <dt>callback (Object)</dt> <dd>The callback object with the following properties:
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * <dl>
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * <dt>success (Function)</dt> <dd>Success handler.</dd>
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * <dt>failure (Function)</dt> <dd>Failure handler.</dd>
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * </dl>
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * </dd>
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * <dt>data (Object)</dt> <dd>Raw data.</dd>
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * <dt>response (Object)</dt> <dd>Normalized response object with the following properties:
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * <dl>
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * <dt>cached (Object)</dt> <dd>True when response is cached.</dd>
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * <dt>results (Object)</dt> <dd>Parsed results.</dd>
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * <dt>meta (Object)</dt> <dd>Parsed meta data.</dd>
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * <dt>error (Object)</dt> <dd>Error object.</dd>
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * </dl>
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * </dd>
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * </dl>
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @protected
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass */
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass _beforeDefResponseFn: function(e) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // Add to Cache before returning
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass if(e.response && !e.cached) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass this.add(e.request, e.response);
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass};
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav GlassY.namespace("Plugin").DataSourceCacheExtension = DataSourceCacheExtension;
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass/**
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * DataSource plugin adds cache functionality.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @class DataSourceCache
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @extends Cache
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @uses Plugin.Base, DataSourceCachePlugin
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass */
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glassfunction DataSourceCache(config) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass var cache = config && config.cache ? config.cache : Y.Cache,
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass tmpclass = Y.Base.create("dataSourceCache", cache, [Y.Plugin.Base, Y.Plugin.DataSourceCacheExtension]),
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass tmpinstance = new tmpclass(config);
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass tmpclass.NS = "tmpClass";
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass return tmpinstance;
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass}
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav GlassY.mix(DataSourceCache, {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass /**
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * The namespace for the plugin. This will be the property on the host which
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * references the plugin instance.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass *
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @property NS
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @type String
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @static
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @final
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @value "cache"
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass */
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass NS: "cache",
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass /**
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * Class name.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass *
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @property NAME
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @type String
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @static
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @final
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @value "dataSourceCache"
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass */
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass NAME: "dataSourceCache"
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass});
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav GlassY.namespace("Plugin").DataSourceCache = DataSourceCache;
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass}, '@VERSION@' ,{requires:['datasource-local', 'cache-base', 'plugin']});