]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/yui3/build/datasource/datasource-cache.js
Release 6.2.0beta4
[Github/sugarcrm.git] / jssource / src_files / include / javascript / yui3 / build / datasource / datasource-cache.js
1 /*
2 Copyright (c) 2009, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.net/yui/license.txt
5 version: 3.0.0
6 build: 1549
7 */
8 YUI.add('datasource-cache', function(Y) {
9
10 /**
11  * Extends DataSource with caching functionality.
12  *
13  * @module datasource
14  * @submodule datasource-cache
15  */
16
17 /**
18  * Adds cacheability to the DataSource Utility.
19  * @class DataSourceCache
20  * @extends Cache
21  */    
22 var DataSourceCache = function() {
23     DataSourceCache.superclass.constructor.apply(this, arguments);
24 };
25
26 Y.mix(DataSourceCache, {
27     /**
28      * The namespace for the plugin. This will be the property on the host which
29      * references the plugin instance.
30      *
31      * @property NS
32      * @type String
33      * @static
34      * @final
35      * @value "cache"
36      */
37     NS: "cache",
38
39     /**
40      * Class name.
41      *
42      * @property NAME
43      * @type String
44      * @static
45      * @final
46      * @value "dataSourceCache"
47      */
48     NAME: "dataSourceCache",
49
50     /////////////////////////////////////////////////////////////////////////////
51     //
52     // DataSourceCache Attributes
53     //
54     /////////////////////////////////////////////////////////////////////////////
55
56     ATTRS: {
57
58     }
59 });
60
61 Y.extend(DataSourceCache, Y.Cache, {
62     /**
63     * Internal init() handler.
64     *
65     * @method initializer
66     * @param config {Object} Config object.
67     * @private
68     */
69     initializer: function(config) {
70         this.doBefore("_defRequestFn", this._beforeDefRequestFn);
71         this.doBefore("_defResponseFn", this._beforeDefResponseFn);
72     },
73
74     /**
75      * First look for cached response, then send request to live data.
76      *
77      * @method _beforeDefRequestFn
78      * @param e {Event.Facade} Event Facade with the following properties:
79      * <dl>
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>
84      * </dl>
85      * @protected
86      */
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");
93         }
94     },
95     
96     /**
97      * Adds data to cache before returning data.
98      *
99      * @method _beforeDefResponseFn
100      * @param e {Event.Facade} Event Facade with the following properties:
101      * <dl>
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:
105      *     <dl>
106      *         <dt>success (Function)</dt> <dd>Success handler.</dd>
107      *         <dt>failure (Function)</dt> <dd>Failure handler.</dd>
108      *     </dl>
109      * </dd>
110      * <dt>data (Object)</dt> <dd>Raw data.</dd>
111      * <dt>response (Object)</dt> <dd>Normalized response object with the following properties:
112      *     <dl>
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>
117      *     </dl>
118      * </dd>
119      * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
120      * </dl>
121      * @protected
122      */
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));
128         }
129      }
130 });
131
132 Y.namespace('Plugin').DataSourceCache = DataSourceCache;
133
134
135
136 }, '3.0.0' ,{requires:['datasource-local', 'cache']});