]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/yui3/build/datasource/datasource-cache.js
Release 6.5.0
[Github/sugarcrm.git] / jssource / src_files / include / javascript / yui3 / build / datasource / datasource-cache.js
1 /*
2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
5 version: 3.3.0
6 build: 3167
7 */
8 YUI.add('datasource-cache', function(Y) {
9
10 /**
11  * Plugs DataSource with caching functionality.
12  *
13  * @module datasource
14  * @submodule datasource-cache
15  */
16
17 /**
18  * DataSourceCache extension binds Cache to DataSource.
19  * @class DataSourceCacheExtension
20  */
21 var DataSourceCacheExtension = function() {
22 };
23
24 Y.mix(DataSourceCacheExtension, {
25     /**
26      * The namespace for the plugin. This will be the property on the host which
27      * references the plugin instance.
28      *
29      * @property NS
30      * @type String
31      * @static
32      * @final
33      * @value "cache"
34      */
35     NS: "cache",
36
37     /**
38      * Class name.
39      *
40      * @property NAME
41      * @type String
42      * @static
43      * @final
44      * @value "dataSourceCacheExtension"
45      */
46     NAME: "dataSourceCacheExtension"
47 });
48
49 DataSourceCacheExtension.prototype = {
50     /**
51     * Internal init() handler.
52     *
53     * @method initializer
54     * @param config {Object} Config object.
55     * @private
56     */
57     initializer: function(config) {
58         this.doBefore("_defRequestFn", this._beforeDefRequestFn);
59         this.doBefore("_defResponseFn", this._beforeDefResponseFn);
60     },
61
62     /**
63      * First look for cached response, then send request to live data.
64      *
65      * @method _beforeDefRequestFn
66      * @param e {Event.Facade} Event Facade with the following properties:
67      * <dl>
68      * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
69      * <dt>request (Object)</dt> <dd>The request.</dd>
70      * <dt>callback (Object)</dt> <dd>The callback object.</dd>
71      * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
72      * </dl>
73      * @protected
74      */
75     _beforeDefRequestFn: function(e) {
76         // Is response already in the Cache?
77         var entry = (this.retrieve(e.request)) || null;
78         if(entry && entry.response) {
79             this.get("host").fire("response", Y.mix(entry, e));
80             return new Y.Do.Halt("DataSourceCache extension halted _defRequestFn");
81         }
82     },
83
84     /**
85      * Adds data to cache before returning data.
86      *
87      * @method _beforeDefResponseFn
88      * @param e {Event.Facade} Event Facade with the following properties:
89      * <dl>
90      * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
91      * <dt>request (Object)</dt> <dd>The request.</dd>
92      * <dt>callback (Object)</dt> <dd>The callback object with the following properties:
93      *     <dl>
94      *         <dt>success (Function)</dt> <dd>Success handler.</dd>
95      *         <dt>failure (Function)</dt> <dd>Failure handler.</dd>
96      *     </dl>
97      * </dd>
98      * <dt>data (Object)</dt> <dd>Raw data.</dd>
99      * <dt>response (Object)</dt> <dd>Normalized response object with the following properties:
100      *     <dl>
101      *         <dt>cached (Object)</dt> <dd>True when response is cached.</dd>
102      *         <dt>results (Object)</dt> <dd>Parsed results.</dd>
103      *         <dt>meta (Object)</dt> <dd>Parsed meta data.</dd>
104      *         <dt>error (Object)</dt> <dd>Error object.</dd>
105      *     </dl>
106      * </dd>
107      * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
108      * </dl>
109      * @protected
110      */
111      _beforeDefResponseFn: function(e) {
112         // Add to Cache before returning
113         if(e.response && !e.cached) {
114             this.add(e.request, e.response);
115         }
116      }
117 };
118
119 Y.namespace("Plugin").DataSourceCacheExtension = DataSourceCacheExtension;
120
121
122
123 /**
124  * DataSource plugin adds cache functionality.
125  * @class DataSourceCache
126  * @extends Cache
127  * @uses Plugin.Base, DataSourceCachePlugin
128  */
129 function DataSourceCache(config) {
130     var cache = config && config.cache ? config.cache : Y.Cache,
131         tmpclass = Y.Base.create("dataSourceCache", cache, [Y.Plugin.Base, Y.Plugin.DataSourceCacheExtension]),
132         tmpinstance = new tmpclass(config);
133     tmpclass.NS = "tmpClass";
134     return tmpinstance;
135 }
136
137 Y.mix(DataSourceCache, {
138     /**
139      * The namespace for the plugin. This will be the property on the host which
140      * references the plugin instance.
141      *
142      * @property NS
143      * @type String
144      * @static
145      * @final
146      * @value "cache"
147      */
148     NS: "cache",
149
150     /**
151      * Class name.
152      *
153      * @property NAME
154      * @type String
155      * @static
156      * @final
157      * @value "dataSourceCache"
158      */
159     NAME: "dataSourceCache"
160 });
161
162
163 Y.namespace("Plugin").DataSourceCache = DataSourceCache;
164
165
166
167 }, '3.3.0' ,{requires:['datasource-local', 'cache-base']});