|
0
|
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.0b1 |
|
|
6 |
build: 1163 |
|
|
7 |
*/ |
|
|
8 |
YUI.add('datasource-get', function(Y) { |
|
|
9 |
|
|
|
10 |
/** |
|
|
11 |
* Provides a DataSource implementation which can be used to retrieve data via the Get Utility. |
|
|
12 |
* |
|
|
13 |
* @module datasource |
|
|
14 |
* @submodule datasource-get |
|
|
15 |
*/ |
|
|
16 |
|
|
|
17 |
/** |
|
|
18 |
* Get Utility subclass for the DataSource Utility. |
|
|
19 |
* @class DataSource.Get |
|
|
20 |
* @extends DataSource.Local |
|
|
21 |
* @constructor |
|
|
22 |
*/ |
|
|
23 |
var DSGet = function() { |
|
|
24 |
DSGet.superclass.constructor.apply(this, arguments); |
|
|
25 |
}; |
|
|
26 |
|
|
|
27 |
|
|
|
28 |
///////////////////////////////////////////////////////////////////////////// |
|
|
29 |
// |
|
|
30 |
// DataSource.Get static properties |
|
|
31 |
// |
|
|
32 |
///////////////////////////////////////////////////////////////////////////// |
|
|
33 |
Y.mix(DSGet, { |
|
|
34 |
/** |
|
|
35 |
* Class name. |
|
|
36 |
* |
|
|
37 |
* @property NAME |
|
|
38 |
* @type String |
|
|
39 |
* @static |
|
|
40 |
* @final |
|
|
41 |
* @value "dataSourceGet" |
|
|
42 |
*/ |
|
|
43 |
NAME: "dataSourceGet", |
|
|
44 |
|
|
|
45 |
|
|
|
46 |
///////////////////////////////////////////////////////////////////////////// |
|
|
47 |
// |
|
|
48 |
// DataSource.Get Attributes |
|
|
49 |
// |
|
|
50 |
///////////////////////////////////////////////////////////////////////////// |
|
|
51 |
|
|
|
52 |
ATTRS: { |
|
|
53 |
/** |
|
|
54 |
* Pointer to Get Utility. |
|
|
55 |
* |
|
|
56 |
* @attribute get |
|
|
57 |
* @type Y.Get |
|
|
58 |
* @default Y.Get |
|
|
59 |
*/ |
|
|
60 |
get: { |
|
|
61 |
value: Y.Get, |
|
|
62 |
cloneDefaultValue: false |
|
|
63 |
}, |
|
|
64 |
|
|
|
65 |
/** |
|
|
66 |
* Defines request/response management in the following manner: |
|
|
67 |
* <dl> |
|
|
68 |
* <!--<dt>queueRequests</dt> |
|
|
69 |
* <dd>If a request is already in progress, wait until response is returned before sending the next request.</dd> |
|
|
70 |
* <dt>cancelStaleRequests</dt> |
|
|
71 |
* <dd>If a request is already in progress, cancel it before sending the next request.</dd>--> |
|
|
72 |
* <dt>ignoreStaleResponses</dt> |
|
|
73 |
* <dd>Send all requests, but handle only the response for the most recently sent request.</dd> |
|
|
74 |
* <dt>allowAll</dt> |
|
|
75 |
* <dd>Send all requests and handle all responses.</dd> |
|
|
76 |
* </dl> |
|
|
77 |
* |
|
|
78 |
* @attribute asyncMode |
|
|
79 |
* @type String |
|
|
80 |
* @default "allowAll" |
|
|
81 |
*/ |
|
|
82 |
asyncMode: { |
|
|
83 |
value: "allowAll" |
|
|
84 |
}, |
|
|
85 |
|
|
|
86 |
/** |
|
|
87 |
* Callback string parameter name sent to the remote script. By default, |
|
|
88 |
* requests are sent to |
|
|
89 |
* <URI>?<scriptCallbackParam>=callbackFunction |
|
|
90 |
* |
|
|
91 |
* @attribute scriptCallbackParam |
|
|
92 |
* @type String |
|
|
93 |
* @default "callback" |
|
|
94 |
*/ |
|
|
95 |
scriptCallbackParam : { |
|
|
96 |
value: "callback" |
|
|
97 |
}, |
|
|
98 |
|
|
|
99 |
/** |
|
|
100 |
* Accepts the DataSource instance and a callback ID, and returns a callback |
|
|
101 |
* param/value string that gets appended to the script URI. Implementers |
|
|
102 |
* can customize this string to match their server's query syntax. |
|
|
103 |
* |
|
|
104 |
* @attribute generateRequestCallback |
|
|
105 |
* @type Function |
|
|
106 |
*/ |
|
|
107 |
generateRequestCallback : { |
|
|
108 |
value: function(self, id) { |
|
|
109 |
return "&" + self.get("scriptCallbackParam") + "=YUI.Env.DataSource.callbacks["+id+"]" ; |
|
|
110 |
} |
|
|
111 |
} |
|
|
112 |
|
|
|
113 |
|
|
|
114 |
|
|
|
115 |
|
|
|
116 |
|
|
|
117 |
}, |
|
|
118 |
|
|
|
119 |
/** |
|
|
120 |
* Global array of callback functions, one for each request sent. |
|
|
121 |
* |
|
|
122 |
* @property callbacks |
|
|
123 |
* @type Function[] |
|
|
124 |
* @static |
|
|
125 |
*/ |
|
|
126 |
callbacks : [], |
|
|
127 |
|
|
|
128 |
/** |
|
|
129 |
* Unique ID to track requests. |
|
|
130 |
* |
|
|
131 |
* @property _tId |
|
|
132 |
* @type Number |
|
|
133 |
* @private |
|
|
134 |
* @static |
|
|
135 |
*/ |
|
|
136 |
_tId : 0 |
|
|
137 |
}); |
|
|
138 |
|
|
|
139 |
Y.extend(DSGet, Y.DataSource.Local, { |
|
|
140 |
/** |
|
|
141 |
* Passes query string to Get Utility. Fires <code>response</code> event when |
|
|
142 |
* response is received asynchronously. |
|
|
143 |
* |
|
|
144 |
* @method _defRequestFn |
|
|
145 |
* @param e {Event.Facade} Event Facade with the following properties: |
|
|
146 |
* <dl> |
|
|
147 |
* <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd> |
|
|
148 |
* <dt>request (Object)</dt> <dd>The request.</dd> |
|
|
149 |
* <dt>callback (Object)</dt> <dd>The callback object with the following properties: |
|
|
150 |
* <dl> |
|
|
151 |
* <dt>success (Function)</dt> <dd>Success handler.</dd> |
|
|
152 |
* <dt>failure (Function)</dt> <dd>Failure handler.</dd> |
|
|
153 |
* </dl> |
|
|
154 |
* </dd> |
|
|
155 |
* <dt>cfg (Object)</dt> <dd>Configuration object.</dd> |
|
|
156 |
* </dl> |
|
|
157 |
* @protected |
|
|
158 |
*/ |
|
|
159 |
_defRequestFn: function(e) { |
|
|
160 |
var uri = this.get("source"), |
|
|
161 |
get = this.get("get"), |
|
|
162 |
id = DSGet._tId++, |
|
|
163 |
self = this; |
|
|
164 |
|
|
|
165 |
|
|
|
166 |
|
|
|
167 |
|
|
|
168 |
|
|
|
169 |
|
|
|
170 |
|
|
|
171 |
|
|
|
172 |
|
|
|
173 |
|
|
|
174 |
|
|
|
175 |
|
|
|
176 |
|
|
|
177 |
// Dynamically add handler function with a closure to the callback stack |
|
|
178 |
YUI.Env.DataSource.callbacks[id] = Y.rbind(function(response) { |
|
|
179 |
if((self.get("asyncMode") !== "ignoreStaleResponses")|| |
|
|
180 |
(id === DSGet.callbacks.length-1)) { // Must ignore stale responses |
|
|
181 |
|
|
|
182 |
self.fire("data", Y.mix({data:response}, e)); |
|
|
183 |
} |
|
|
184 |
else { |
|
|
185 |
Y.log("DataSource ignored stale response for id " + e.tId + "(" + e.request + ")", "info", "datasource-get"); |
|
|
186 |
} |
|
|
187 |
|
|
|
188 |
delete DSGet.callbacks[id]; |
|
|
189 |
}, this, id); |
|
|
190 |
|
|
|
191 |
// We are now creating a request |
|
|
192 |
uri += e.request + this.get("generateRequestCallback")(this, id); |
|
|
193 |
//uri = this.doBefore(sUri); |
|
|
194 |
Y.log("DataSource is querying URL " + uri, "info", "datasource-get"); |
|
|
195 |
get.script(uri, { |
|
|
196 |
autopurge: true, |
|
|
197 |
// Works in Firefox only.... |
|
|
198 |
onFailure: Y.bind(function(e) { |
|
|
199 |
e.error = new Error("Script node data failure"); |
|
|
200 |
this.fire("error", e); |
|
|
201 |
}, this, e) |
|
|
202 |
}); |
|
|
203 |
|
|
|
204 |
|
|
|
205 |
|
|
|
206 |
|
|
|
207 |
|
|
|
208 |
|
|
|
209 |
|
|
|
210 |
|
|
|
211 |
|
|
|
212 |
|
|
|
213 |
|
|
|
214 |
|
|
|
215 |
|
|
|
216 |
|
|
|
217 |
|
|
|
218 |
return e.tId; |
|
|
219 |
} |
|
|
220 |
}); |
|
|
221 |
|
|
|
222 |
Y.DataSource.Get = DSGet; |
|
|
223 |
YUI.namespace("Env.DataSource.callbacks"); |
|
|
224 |
|
|
|
225 |
|
|
|
226 |
|
|
|
227 |
|
|
|
228 |
}, '3.0.0b1' ,{requires:['datasource-local', 'get']}); |