2 Copyright (c) 2009, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.net/yui/license.txt
8 YUI.add('datasource-cache', function(Y) {
11 * Extends DataSource with caching functionality.
14 * @submodule datasource-cache
18 * Adds cacheability to the DataSource Utility.
19 * @class DataSourceCache
22 var DataSourceCache = function() {
23 DataSourceCache.superclass.constructor.apply(this, arguments);
26 Y.mix(DataSourceCache, {
28 * The namespace for the plugin. This will be the property on the host which
29 * references the plugin instance.
46 * @value "dataSourceCache"
48 NAME: "dataSourceCache",
50 /////////////////////////////////////////////////////////////////////////////
52 // DataSourceCache Attributes
54 /////////////////////////////////////////////////////////////////////////////
61 Y.extend(DataSourceCache, Y.Cache, {
63 * Internal init() handler.
66 * @param config {Object} Config object.
69 initializer: function(config) {
70 this.doBefore("_defRequestFn", this._beforeDefRequestFn);
71 this.doBefore("_defResponseFn", this._beforeDefResponseFn);
75 * First look for cached response, then send request to live data.
77 * @method _beforeDefRequestFn
78 * @param e {Event.Facade} Event Facade with the following properties:
80 * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
81 * <dt>request (Object)</dt> <dd>The request.</dd>
82 * <dt>callback (Object)</dt> <dd>The callback object.</dd>
83 * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
87 _beforeDefRequestFn: function(e) {
88 // Is response already in the Cache?
89 var entry = (this.retrieve(e.request)) || null;
90 if(entry && entry.response) {
91 this.get("host").fire("response", Y.mix({response: entry.response}, e));
92 return new Y.Do.Halt("DataSourceCache plugin halted _defRequestFn");
97 * Adds data to cache before returning data.
99 * @method _beforeDefResponseFn
100 * @param e {Event.Facade} Event Facade with the following properties:
102 * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
103 * <dt>request (Object)</dt> <dd>The request.</dd>
104 * <dt>callback (Object)</dt> <dd>The callback object with the following properties:
106 * <dt>success (Function)</dt> <dd>Success handler.</dd>
107 * <dt>failure (Function)</dt> <dd>Failure handler.</dd>
110 * <dt>data (Object)</dt> <dd>Raw data.</dd>
111 * <dt>response (Object)</dt> <dd>Normalized response object with the following properties:
113 * <dt>cached (Object)</dt> <dd>True when response is cached.</dd>
114 * <dt>results (Object)</dt> <dd>Parsed results.</dd>
115 * <dt>meta (Object)</dt> <dd>Parsed meta data.</dd>
116 * <dt>error (Object)</dt> <dd>Error object.</dd>
119 * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
123 _beforeDefResponseFn: function(e) {
124 // Add to Cache before returning
125 if(e.response && !e.response.cached) {
126 e.response.cached = true;
127 this.add(e.request, e.response, (e.callback && e.callback.argument));
132 Y.namespace('Plugin').DataSourceCache = DataSourceCache;
136 }, '3.0.0' ,{requires:['datasource-local', 'cache']});