~andreserl/maas/packaging_precise_rebase

« back to all changes in this revision

Viewing changes to debian/extras/jslibs/yui/datasource-cache/datasource-cache.js

  • Committer: Andres Rodriguez
  • Date: 2013-03-20 18:12:30 UTC
  • mfrom: (145.2.22 precise.sru)
  • Revision ID: andreserl@ubuntu.com-20130320181230-6l5guc0nhlv2z4p7
Re-base againts latest quantal released branch towards SRU

Show diffs side-by-side

added added

removed removed

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