|
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-local', function(Y) { |
|
|
9 |
|
|
|
10 |
/** |
|
|
11 |
* The DataSource utility provides a common configurable interface for widgets to |
|
|
12 |
* access a variety of data, from JavaScript arrays to online database servers. |
|
|
13 |
* |
|
|
14 |
* @module datasource |
|
|
15 |
*/ |
|
|
16 |
|
|
|
17 |
/** |
|
|
18 |
* Provides the base DataSource implementation, which can be extended to |
|
|
19 |
* create DataSources for specific data protocols, such as the IO Utility, the |
|
|
20 |
* Get Utility, or custom functions. |
|
|
21 |
* |
|
|
22 |
* @module datasource |
|
|
23 |
* @submodule datasource-local |
|
|
24 |
*/ |
|
|
25 |
|
|
|
26 |
/** |
|
|
27 |
* Base class for the DataSource Utility. |
|
|
28 |
* @class DataSource.Local |
|
|
29 |
* @extends Base |
|
|
30 |
* @constructor |
|
|
31 |
*/ |
|
|
32 |
var LANG = Y.Lang, |
|
|
33 |
|
|
|
34 |
DSLocal = function() { |
|
|
35 |
DSLocal.superclass.constructor.apply(this, arguments); |
|
|
36 |
}; |
|
|
37 |
|
|
|
38 |
///////////////////////////////////////////////////////////////////////////// |
|
|
39 |
// |
|
|
40 |
// DataSource static properties |
|
|
41 |
// |
|
|
42 |
///////////////////////////////////////////////////////////////////////////// |
|
|
43 |
Y.mix(DSLocal, { |
|
|
44 |
/** |
|
|
45 |
* Class name. |
|
|
46 |
* |
|
|
47 |
* @property NAME |
|
|
48 |
* @type String |
|
|
49 |
* @static |
|
|
50 |
* @final |
|
|
51 |
* @value "dataSourceLocal" |
|
|
52 |
*/ |
|
|
53 |
NAME: "dataSourceLocal", |
|
|
54 |
|
|
|
55 |
///////////////////////////////////////////////////////////////////////////// |
|
|
56 |
// |
|
|
57 |
// DataSource Attributes |
|
|
58 |
// |
|
|
59 |
///////////////////////////////////////////////////////////////////////////// |
|
|
60 |
|
|
|
61 |
ATTRS: { |
|
|
62 |
/** |
|
|
63 |
* @attribute source |
|
|
64 |
* @description Pointer to live data. |
|
|
65 |
* @type MIXED |
|
|
66 |
* @default null |
|
|
67 |
*/ |
|
|
68 |
source: { |
|
|
69 |
value: null |
|
|
70 |
} |
|
|
71 |
}, |
|
|
72 |
|
|
|
73 |
/** |
|
|
74 |
* Global transaction counter. |
|
|
75 |
* |
|
|
76 |
* @property DataSource._tId |
|
|
77 |
* @type Number |
|
|
78 |
* @static |
|
|
79 |
* @private |
|
|
80 |
* @default 0 |
|
|
81 |
*/ |
|
|
82 |
_tId: 0, |
|
|
83 |
|
|
|
84 |
/** |
|
|
85 |
* Executes a given callback. The third param determines whether to execute |
|
|
86 |
* |
|
|
87 |
* @method DataSource.issueCallback |
|
|
88 |
* @param callback {Object} The callback object. |
|
|
89 |
* @param params {Array} params to be passed to the callback method |
|
|
90 |
* @param error {Boolean} whether an error occurred |
|
|
91 |
* @static |
|
|
92 |
*/ |
|
|
93 |
issueCallback: function (e) { |
|
|
94 |
if(e.callback) { |
|
|
95 |
var callbackFunc = (e.error && e.callback.failure) || e.callback.success; |
|
|
96 |
if (callbackFunc) { |
|
|
97 |
callbackFunc(e); |
|
|
98 |
} |
|
|
99 |
} |
|
|
100 |
} |
|
|
101 |
}); |
|
|
102 |
|
|
|
103 |
Y.extend(DSLocal, Y.Base, { |
|
|
104 |
/** |
|
|
105 |
* Internal init() handler. |
|
|
106 |
* |
|
|
107 |
* @method initializer |
|
|
108 |
* @param config {Object} Config object. |
|
|
109 |
* @private |
|
|
110 |
*/ |
|
|
111 |
initializer: function(config) { |
|
|
112 |
this._initEvents(); |
|
|
113 |
}, |
|
|
114 |
|
|
|
115 |
/** |
|
|
116 |
* This method creates all the events for this module. |
|
|
117 |
* @method _initEvents |
|
|
118 |
* @private |
|
|
119 |
*/ |
|
|
120 |
_initEvents: function() { |
|
|
121 |
/** |
|
|
122 |
* Fired when a data request is received. |
|
|
123 |
* |
|
|
124 |
* @event request |
|
|
125 |
* @param e {Event.Facade} Event Facade with the following properties: |
|
|
126 |
* <dl> |
|
|
127 |
* <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd> |
|
|
128 |
* <dt>request (Object)</dt> <dd>The request.</dd> |
|
|
129 |
* <dt>callback (Object)</dt> <dd>The callback object.</dd> |
|
|
130 |
* <dt>cfg (Object)</dt> <dd>Configuration object.</dd> |
|
|
131 |
* </dl> |
|
|
132 |
* @preventable _defRequestFn |
|
|
133 |
*/ |
|
|
134 |
this.publish("request", {defaultFn: Y.bind("_defRequestFn", this), queuable:true}); |
|
|
135 |
|
|
|
136 |
/** |
|
|
137 |
* Fired when raw data is received. |
|
|
138 |
* |
|
|
139 |
* @event data |
|
|
140 |
* @param e {Event.Facade} Event Facade with the following properties: |
|
|
141 |
* <dl> |
|
|
142 |
* <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd> |
|
|
143 |
* <dt>request (Object)</dt> <dd>The request.</dd> |
|
|
144 |
* <dt>callback (Object)</dt> <dd>The callback object with the following properties: |
|
|
145 |
* <dl> |
|
|
146 |
* <dt>success (Function)</dt> <dd>Success handler.</dd> |
|
|
147 |
* <dt>failure (Function)</dt> <dd>Failure handler.</dd> |
|
|
148 |
* </dl> |
|
|
149 |
* </dd> |
|
|
150 |
* <dt>cfg (Object)</dt> <dd>Configuration object.</dd> |
|
|
151 |
* <dt>data (Object)</dt> <dd>Raw data.</dd> |
|
|
152 |
* </dl> |
|
|
153 |
* @preventable _defDataFn |
|
|
154 |
*/ |
|
|
155 |
this.publish("data", {defaultFn: Y.bind("_defDataFn", this), queuable:true}); |
|
|
156 |
|
|
|
157 |
/** |
|
|
158 |
* Fired when response is returned. |
|
|
159 |
* |
|
|
160 |
* @event response |
|
|
161 |
* @param e {Event.Facade} Event Facade with the following properties: |
|
|
162 |
* <dl> |
|
|
163 |
* <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd> |
|
|
164 |
* <dt>request (Object)</dt> <dd>The request.</dd> |
|
|
165 |
* <dt>callback (Object)</dt> <dd>The callback object with the following properties: |
|
|
166 |
* <dl> |
|
|
167 |
* <dt>success (Function)</dt> <dd>Success handler.</dd> |
|
|
168 |
* <dt>failure (Function)</dt> <dd>Failure handler.</dd> |
|
|
169 |
* </dl> |
|
|
170 |
* </dd> |
|
|
171 |
* <dt>cfg (Object)</dt> <dd>Configuration object.</dd> |
|
|
172 |
* <dt>data (Object)</dt> <dd>Raw data.</dd> |
|
|
173 |
* <dt>response (Object)</dt> <dd>Normalized response object with the following properties: |
|
|
174 |
* <dl> |
|
|
175 |
* <dt>results (Object)</dt> <dd>Parsed results.</dd> |
|
|
176 |
* <dt>meta (Object)</dt> <dd>Parsed meta data.</dd> |
|
|
177 |
* <dt>error (Boolean)</dt> <dd>Error flag.</dd> |
|
|
178 |
* </dl> |
|
|
179 |
* </dd> |
|
|
180 |
* </dl> |
|
|
181 |
* @preventable _defResponseFn |
|
|
182 |
*/ |
|
|
183 |
this.publish("response", {defaultFn: Y.bind("_defResponseFn", this), queuable:true}); |
|
|
184 |
|
|
|
185 |
/** |
|
|
186 |
* Fired when an error is encountered. |
|
|
187 |
* |
|
|
188 |
* @event error |
|
|
189 |
* @param e {Event.Facade} Event Facade with the following properties: |
|
|
190 |
* <dl> |
|
|
191 |
* <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd> |
|
|
192 |
* <dt>request (Object)</dt> <dd>The request.</dd> |
|
|
193 |
* <dt>callback (Object)</dt> <dd>The callback object with the following properties: |
|
|
194 |
* <dl> |
|
|
195 |
* <dt>success (Function)</dt> <dd>Success handler.</dd> |
|
|
196 |
* <dt>failure (Function)</dt> <dd>Failure handler.</dd> |
|
|
197 |
* </dl> |
|
|
198 |
* </dd> |
|
|
199 |
* <dt>cfg (Object)</dt> <dd>Configuration object.</dd> |
|
|
200 |
* <dt>data (Object)</dt> <dd>Raw data.</dd> |
|
|
201 |
* <dt>response (Object)</dt> <dd>Normalized response object with the following properties: |
|
|
202 |
* <dl> |
|
|
203 |
* <dt>results (Object)</dt> <dd>Parsed results.</dd> |
|
|
204 |
* <dt>meta (Object)</dt> <dd>Parsed meta data.</dd> |
|
|
205 |
* <dt>error (Object)</dt> <dd>Error object.</dd> |
|
|
206 |
* </dl> |
|
|
207 |
* </dd> |
|
|
208 |
* </dl> |
|
|
209 |
*/ |
|
|
210 |
|
|
|
211 |
}, |
|
|
212 |
|
|
|
213 |
/** |
|
|
214 |
* Manages request/response transaction. Must fire <code>response</code> |
|
|
215 |
* event when response is received. This method should be implemented by |
|
|
216 |
* subclasses to achieve more complex behavior such as accessing remote data. |
|
|
217 |
* |
|
|
218 |
* @method _defRequestFn |
|
|
219 |
* @param e {Event.Facade} Event Facadewith the following properties: |
|
|
220 |
* <dl> |
|
|
221 |
* <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd> |
|
|
222 |
* <dt>request (Object)</dt> <dd>The request.</dd> |
|
|
223 |
* <dt>callback (Object)</dt> <dd>The callback object with the following properties: |
|
|
224 |
* <dl> |
|
|
225 |
* <dt>success (Function)</dt> <dd>Success handler.</dd> |
|
|
226 |
* <dt>failure (Function)</dt> <dd>Failure handler.</dd> |
|
|
227 |
* </dl> |
|
|
228 |
* </dd> |
|
|
229 |
* <dt>cfg (Object)</dt> <dd>Configuration object.</dd> |
|
|
230 |
* </dl> |
|
|
231 |
* @protected |
|
|
232 |
*/ |
|
|
233 |
_defRequestFn: function(e) { |
|
|
234 |
var data = this.get("source"); |
|
|
235 |
|
|
|
236 |
// Problematic data |
|
|
237 |
if(LANG.isUndefined(data)) { |
|
|
238 |
e.error = new Error("Local source undefined"); |
|
|
239 |
} |
|
|
240 |
if(e.error) { |
|
|
241 |
this.fire("error", e); |
|
|
242 |
Y.log("Error in response", "error", "datasource-local"); |
|
|
243 |
} |
|
|
244 |
|
|
|
245 |
this.fire("data", Y.mix({data:data}, e)); |
|
|
246 |
Y.log("Transaction " + e.tId + " complete. Request: " + |
|
|
247 |
Y.dump(e.request) + " . Response: " + Y.dump(e.response), "info", "datasource-local"); |
|
|
248 |
}, |
|
|
249 |
|
|
|
250 |
/** |
|
|
251 |
* Normalizes raw data into a response that includes results and meta properties. |
|
|
252 |
* |
|
|
253 |
* @method _defDataFn |
|
|
254 |
* @param e {Event.Facade} Event Facade with the following properties: |
|
|
255 |
* <dl> |
|
|
256 |
* <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd> |
|
|
257 |
* <dt>request (Object)</dt> <dd>The request.</dd> |
|
|
258 |
* <dt>callback (Object)</dt> <dd>The callback object with the following properties: |
|
|
259 |
* <dl> |
|
|
260 |
* <dt>success (Function)</dt> <dd>Success handler.</dd> |
|
|
261 |
* <dt>failure (Function)</dt> <dd>Failure handler.</dd> |
|
|
262 |
* </dl> |
|
|
263 |
* </dd> |
|
|
264 |
* <dt>cfg (Object)</dt> <dd>Configuration object.</dd> |
|
|
265 |
* <dt>data (Object)</dt> <dd>Raw data.</dd> |
|
|
266 |
* </dl> |
|
|
267 |
* @protected |
|
|
268 |
*/ |
|
|
269 |
_defDataFn: function(e) { |
|
|
270 |
var data = e.data, |
|
|
271 |
meta = e.meta, |
|
|
272 |
response = { |
|
|
273 |
results: (LANG.isArray(data)) ? data : [data], |
|
|
274 |
meta: (meta) ? meta : {} |
|
|
275 |
}; |
|
|
276 |
|
|
|
277 |
this.fire("response", Y.mix({response: response}, e)); |
|
|
278 |
}, |
|
|
279 |
|
|
|
280 |
/** |
|
|
281 |
* Sends data as a normalized response to callback. |
|
|
282 |
* |
|
|
283 |
* @method _defResponseFn |
|
|
284 |
* @param e {Event.Facade} Event Facade with the following properties: |
|
|
285 |
* <dl> |
|
|
286 |
* <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd> |
|
|
287 |
* <dt>request (Object)</dt> <dd>The request.</dd> |
|
|
288 |
* <dt>callback (Object)</dt> <dd>The callback object with the following properties: |
|
|
289 |
* <dl> |
|
|
290 |
* <dt>success (Function)</dt> <dd>Success handler.</dd> |
|
|
291 |
* <dt>failure (Function)</dt> <dd>Failure handler.</dd> |
|
|
292 |
* </dl> |
|
|
293 |
* </dd> |
|
|
294 |
* <dt>cfg (Object)</dt> <dd>Configuration object.</dd> |
|
|
295 |
* <dt>data (Object)</dt> <dd>Raw data.</dd> |
|
|
296 |
* <dt>response (Object)</dt> <dd>Normalized response object with the following properties: |
|
|
297 |
* <dl> |
|
|
298 |
* <dt>results (Object)</dt> <dd>Parsed results.</dd> |
|
|
299 |
* <dt>meta (Object)</dt> <dd>Parsed meta data.</dd> |
|
|
300 |
* <dt>error (Boolean)</dt> <dd>Error flag.</dd> |
|
|
301 |
* </dl> |
|
|
302 |
* </dd> |
|
|
303 |
* </dl> |
|
|
304 |
* @protected |
|
|
305 |
*/ |
|
|
306 |
_defResponseFn: function(e) { |
|
|
307 |
// Send the response back to the callback |
|
|
308 |
DSLocal.issueCallback(e); |
|
|
309 |
}, |
|
|
310 |
|
|
|
311 |
/** |
|
|
312 |
* Generates a unique transaction ID and fires <code>request</code> event. |
|
|
313 |
* |
|
|
314 |
* @method sendRequest |
|
|
315 |
* @param request {Object} Request. |
|
|
316 |
* @param callback {Object} An object literal with the following properties: |
|
|
317 |
* <dl> |
|
|
318 |
* <dt><code>success</code></dt> |
|
|
319 |
* <dd>The function to call when the data is ready.</dd> |
|
|
320 |
* <dt><code>failure</code></dt> |
|
|
321 |
* <dd>The function to call upon a response failure condition.</dd> |
|
|
322 |
* <dt><code>argument</code></dt> |
|
|
323 |
* <dd>Arbitrary data payload that will be passed back to the success and failure handlers.</dd> |
|
|
324 |
* </dl> |
|
|
325 |
* @param cfg {Object} Configuration object |
|
|
326 |
* @return {Number} Transaction ID. |
|
|
327 |
*/ |
|
|
328 |
sendRequest: function(request, callback, cfg) { |
|
|
329 |
var tId = DSLocal._tId++; |
|
|
330 |
this.fire("request", {tId:tId, request:request, callback:callback, cfg:cfg || {}}); |
|
|
331 |
Y.log("Transaction " + tId + " sent request: " + Y.dump(request), "info", "datasource-local"); |
|
|
332 |
return tId; |
|
|
333 |
} |
|
|
334 |
}); |
|
|
335 |
|
|
|
336 |
Y.namespace("DataSource").Local = DSLocal; |
|
|
337 |
|
|
|
338 |
|
|
|
339 |
|
|
|
340 |
}, '3.0.0b1' ,{requires:['base']}); |
|
|
341 |
|
|
|
342 |
YUI.add('datasource-io', function(Y) { |
|
|
343 |
|
|
|
344 |
/** |
|
|
345 |
* Provides a DataSource implementation which can be used to retrieve data via the IO Utility. |
|
|
346 |
* |
|
|
347 |
* @module datasource |
|
|
348 |
* @submodule datasource-io |
|
|
349 |
*/ |
|
|
350 |
|
|
|
351 |
/** |
|
|
352 |
* IO subclass for the DataSource Utility. |
|
|
353 |
* @class DataSource.IO |
|
|
354 |
* @extends DataSource.Local |
|
|
355 |
* @constructor |
|
|
356 |
*/ |
|
|
357 |
var DSIO = function() { |
|
|
358 |
DSIO.superclass.constructor.apply(this, arguments); |
|
|
359 |
}; |
|
|
360 |
|
|
|
361 |
|
|
|
362 |
///////////////////////////////////////////////////////////////////////////// |
|
|
363 |
// |
|
|
364 |
// DataSource.IO static properties |
|
|
365 |
// |
|
|
366 |
///////////////////////////////////////////////////////////////////////////// |
|
|
367 |
Y.mix(DSIO, { |
|
|
368 |
/** |
|
|
369 |
* Class name. |
|
|
370 |
* |
|
|
371 |
* @property NAME |
|
|
372 |
* @type String |
|
|
373 |
* @static |
|
|
374 |
* @final |
|
|
375 |
* @value "dataSourceIO" |
|
|
376 |
*/ |
|
|
377 |
NAME: "dataSourceIO", |
|
|
378 |
|
|
|
379 |
|
|
|
380 |
///////////////////////////////////////////////////////////////////////////// |
|
|
381 |
// |
|
|
382 |
// DataSource.IO Attributes |
|
|
383 |
// |
|
|
384 |
///////////////////////////////////////////////////////////////////////////// |
|
|
385 |
|
|
|
386 |
ATTRS: { |
|
|
387 |
/** |
|
|
388 |
* Pointer to IO Utility. |
|
|
389 |
* |
|
|
390 |
* @attribute io |
|
|
391 |
* @type Y.io |
|
|
392 |
* @default Y.io |
|
|
393 |
*/ |
|
|
394 |
io: { |
|
|
395 |
value: Y.io, |
|
|
396 |
cloneDefaultValue: false |
|
|
397 |
} |
|
|
398 |
} |
|
|
399 |
}); |
|
|
400 |
|
|
|
401 |
Y.extend(DSIO, Y.DataSource.Local, { |
|
|
402 |
/** |
|
|
403 |
* Internal init() handler. |
|
|
404 |
* |
|
|
405 |
* @method initializer |
|
|
406 |
* @param config {Object} Config object. |
|
|
407 |
* @private |
|
|
408 |
*/ |
|
|
409 |
initializer: function(config) { |
|
|
410 |
this._queue = {interval:null, conn:null, requests:[]}; |
|
|
411 |
}, |
|
|
412 |
|
|
|
413 |
/** |
|
|
414 |
* @property _queue |
|
|
415 |
* @description Object literal to manage asynchronous request/response |
|
|
416 |
* cycles enabled if queue needs to be managed (asyncMode/ioConnMode): |
|
|
417 |
* <dl> |
|
|
418 |
* <dt>interval {Number}</dt> |
|
|
419 |
* <dd>Interval ID of in-progress queue.</dd> |
|
|
420 |
* <dt>conn</dt> |
|
|
421 |
* <dd>In-progress connection identifier (if applicable).</dd> |
|
|
422 |
* <dt>requests {Object[]}</dt> |
|
|
423 |
* <dd>Array of queued request objects: {request:request, callback:callback}.</dd> |
|
|
424 |
* </dl> |
|
|
425 |
* @type Object |
|
|
426 |
* @default {interval:null, conn:null, requests:[]} |
|
|
427 |
* @private |
|
|
428 |
*/ |
|
|
429 |
_queue: null, |
|
|
430 |
|
|
|
431 |
/** |
|
|
432 |
* Passes query string to IO. Fires <code>response</code> event when |
|
|
433 |
* response is received asynchronously. |
|
|
434 |
* |
|
|
435 |
* @method _defRequestFn |
|
|
436 |
* @param e {Event.Facade} Event Facade with the following properties: |
|
|
437 |
* <dl> |
|
|
438 |
* <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd> |
|
|
439 |
* <dt>request (Object)</dt> <dd>The request.</dd> |
|
|
440 |
* <dt>callback (Object)</dt> <dd>The callback object with the following properties: |
|
|
441 |
* <dl> |
|
|
442 |
* <dt>success (Function)</dt> <dd>Success handler.</dd> |
|
|
443 |
* <dt>failure (Function)</dt> <dd>Failure handler.</dd> |
|
|
444 |
* </dl> |
|
|
445 |
* </dd> |
|
|
446 |
* <dt>cfg (Object)</dt> <dd>Configuration object.</dd> |
|
|
447 |
* </dl> |
|
|
448 |
* @protected |
|
|
449 |
*/ |
|
|
450 |
_defRequestFn: function(e) { |
|
|
451 |
var uri = this.get("source"), |
|
|
452 |
cfg = Y.mix(e.cfg, { |
|
|
453 |
on: { |
|
|
454 |
success: function (id, response, e) { |
|
|
455 |
this.fire("data", Y.mix({data:response}, e)); |
|
|
456 |
Y.log("Received IO data response for \"" + e.request + "\"", "info", "datasource-io"); |
|
|
457 |
}, |
|
|
458 |
failure: function (id, response, e) { |
|
|
459 |
e.error = new Error("IO data failure"); |
|
|
460 |
this.fire("error", Y.mix({data:response}, e)); |
|
|
461 |
this.fire("data", Y.mix({data:response}, e)); |
|
|
462 |
Y.log("Received IO data failure for \"" + e.request + "\"", "info", "datasource-io"); |
|
|
463 |
} |
|
|
464 |
}, |
|
|
465 |
context: this, |
|
|
466 |
arguments: e |
|
|
467 |
}); |
|
|
468 |
|
|
|
469 |
this.get("io")(uri, cfg); |
|
|
470 |
return e.tId; |
|
|
471 |
} |
|
|
472 |
}); |
|
|
473 |
|
|
|
474 |
Y.DataSource.IO = DSIO; |
|
|
475 |
|
|
|
476 |
|
|
|
477 |
|
|
|
478 |
|
|
|
479 |
}, '3.0.0b1' ,{requires:['datasource-local', 'io']}); |
|
|
480 |
|
|
|
481 |
YUI.add('datasource-get', function(Y) { |
|
|
482 |
|
|
|
483 |
/** |
|
|
484 |
* Provides a DataSource implementation which can be used to retrieve data via the Get Utility. |
|
|
485 |
* |
|
|
486 |
* @module datasource |
|
|
487 |
* @submodule datasource-get |
|
|
488 |
*/ |
|
|
489 |
|
|
|
490 |
/** |
|
|
491 |
* Get Utility subclass for the DataSource Utility. |
|
|
492 |
* @class DataSource.Get |
|
|
493 |
* @extends DataSource.Local |
|
|
494 |
* @constructor |
|
|
495 |
*/ |
|
|
496 |
var DSGet = function() { |
|
|
497 |
DSGet.superclass.constructor.apply(this, arguments); |
|
|
498 |
}; |
|
|
499 |
|
|
|
500 |
|
|
|
501 |
///////////////////////////////////////////////////////////////////////////// |
|
|
502 |
// |
|
|
503 |
// DataSource.Get static properties |
|
|
504 |
// |
|
|
505 |
///////////////////////////////////////////////////////////////////////////// |
|
|
506 |
Y.mix(DSGet, { |
|
|
507 |
/** |
|
|
508 |
* Class name. |
|
|
509 |
* |
|
|
510 |
* @property NAME |
|
|
511 |
* @type String |
|
|
512 |
* @static |
|
|
513 |
* @final |
|
|
514 |
* @value "dataSourceGet" |
|
|
515 |
*/ |
|
|
516 |
NAME: "dataSourceGet", |
|
|
517 |
|
|
|
518 |
|
|
|
519 |
///////////////////////////////////////////////////////////////////////////// |
|
|
520 |
// |
|
|
521 |
// DataSource.Get Attributes |
|
|
522 |
// |
|
|
523 |
///////////////////////////////////////////////////////////////////////////// |
|
|
524 |
|
|
|
525 |
ATTRS: { |
|
|
526 |
/** |
|
|
527 |
* Pointer to Get Utility. |
|
|
528 |
* |
|
|
529 |
* @attribute get |
|
|
530 |
* @type Y.Get |
|
|
531 |
* @default Y.Get |
|
|
532 |
*/ |
|
|
533 |
get: { |
|
|
534 |
value: Y.Get, |
|
|
535 |
cloneDefaultValue: false |
|
|
536 |
}, |
|
|
537 |
|
|
|
538 |
/** |
|
|
539 |
* Defines request/response management in the following manner: |
|
|
540 |
* <dl> |
|
|
541 |
* <!--<dt>queueRequests</dt> |
|
|
542 |
* <dd>If a request is already in progress, wait until response is returned before sending the next request.</dd> |
|
|
543 |
* <dt>cancelStaleRequests</dt> |
|
|
544 |
* <dd>If a request is already in progress, cancel it before sending the next request.</dd>--> |
|
|
545 |
* <dt>ignoreStaleResponses</dt> |
|
|
546 |
* <dd>Send all requests, but handle only the response for the most recently sent request.</dd> |
|
|
547 |
* <dt>allowAll</dt> |
|
|
548 |
* <dd>Send all requests and handle all responses.</dd> |
|
|
549 |
* </dl> |
|
|
550 |
* |
|
|
551 |
* @attribute asyncMode |
|
|
552 |
* @type String |
|
|
553 |
* @default "allowAll" |
|
|
554 |
*/ |
|
|
555 |
asyncMode: { |
|
|
556 |
value: "allowAll" |
|
|
557 |
}, |
|
|
558 |
|
|
|
559 |
/** |
|
|
560 |
* Callback string parameter name sent to the remote script. By default, |
|
|
561 |
* requests are sent to |
|
|
562 |
* <URI>?<scriptCallbackParam>=callbackFunction |
|
|
563 |
* |
|
|
564 |
* @attribute scriptCallbackParam |
|
|
565 |
* @type String |
|
|
566 |
* @default "callback" |
|
|
567 |
*/ |
|
|
568 |
scriptCallbackParam : { |
|
|
569 |
value: "callback" |
|
|
570 |
}, |
|
|
571 |
|
|
|
572 |
/** |
|
|
573 |
* Accepts the DataSource instance and a callback ID, and returns a callback |
|
|
574 |
* param/value string that gets appended to the script URI. Implementers |
|
|
575 |
* can customize this string to match their server's query syntax. |
|
|
576 |
* |
|
|
577 |
* @attribute generateRequestCallback |
|
|
578 |
* @type Function |
|
|
579 |
*/ |
|
|
580 |
generateRequestCallback : { |
|
|
581 |
value: function(self, id) { |
|
|
582 |
return "&" + self.get("scriptCallbackParam") + "=YUI.Env.DataSource.callbacks["+id+"]" ; |
|
|
583 |
} |
|
|
584 |
} |
|
|
585 |
|
|
|
586 |
|
|
|
587 |
|
|
|
588 |
|
|
|
589 |
|
|
|
590 |
}, |
|
|
591 |
|
|
|
592 |
/** |
|
|
593 |
* Global array of callback functions, one for each request sent. |
|
|
594 |
* |
|
|
595 |
* @property callbacks |
|
|
596 |
* @type Function[] |
|
|
597 |
* @static |
|
|
598 |
*/ |
|
|
599 |
callbacks : [], |
|
|
600 |
|
|
|
601 |
/** |
|
|
602 |
* Unique ID to track requests. |
|
|
603 |
* |
|
|
604 |
* @property _tId |
|
|
605 |
* @type Number |
|
|
606 |
* @private |
|
|
607 |
* @static |
|
|
608 |
*/ |
|
|
609 |
_tId : 0 |
|
|
610 |
}); |
|
|
611 |
|
|
|
612 |
Y.extend(DSGet, Y.DataSource.Local, { |
|
|
613 |
/** |
|
|
614 |
* Passes query string to Get Utility. Fires <code>response</code> event when |
|
|
615 |
* response is received asynchronously. |
|
|
616 |
* |
|
|
617 |
* @method _defRequestFn |
|
|
618 |
* @param e {Event.Facade} Event Facade with the following properties: |
|
|
619 |
* <dl> |
|
|
620 |
* <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd> |
|
|
621 |
* <dt>request (Object)</dt> <dd>The request.</dd> |
|
|
622 |
* <dt>callback (Object)</dt> <dd>The callback object with the following properties: |
|
|
623 |
* <dl> |
|
|
624 |
* <dt>success (Function)</dt> <dd>Success handler.</dd> |
|
|
625 |
* <dt>failure (Function)</dt> <dd>Failure handler.</dd> |
|
|
626 |
* </dl> |
|
|
627 |
* </dd> |
|
|
628 |
* <dt>cfg (Object)</dt> <dd>Configuration object.</dd> |
|
|
629 |
* </dl> |
|
|
630 |
* @protected |
|
|
631 |
*/ |
|
|
632 |
_defRequestFn: function(e) { |
|
|
633 |
var uri = this.get("source"), |
|
|
634 |
get = this.get("get"), |
|
|
635 |
id = DSGet._tId++, |
|
|
636 |
self = this; |
|
|
637 |
|
|
|
638 |
|
|
|
639 |
|
|
|
640 |
|
|
|
641 |
|
|
|
642 |
|
|
|
643 |
|
|
|
644 |
|
|
|
645 |
|
|
|
646 |
|
|
|
647 |
|
|
|
648 |
|
|
|
649 |
|
|
|
650 |
// Dynamically add handler function with a closure to the callback stack |
|
|
651 |
YUI.Env.DataSource.callbacks[id] = Y.rbind(function(response) { |
|
|
652 |
if((self.get("asyncMode") !== "ignoreStaleResponses")|| |
|
|
653 |
(id === DSGet.callbacks.length-1)) { // Must ignore stale responses |
|
|
654 |
|
|
|
655 |
self.fire("data", Y.mix({data:response}, e)); |
|
|
656 |
} |
|
|
657 |
else { |
|
|
658 |
Y.log("DataSource ignored stale response for id " + e.tId + "(" + e.request + ")", "info", "datasource-get"); |
|
|
659 |
} |
|
|
660 |
|
|
|
661 |
delete DSGet.callbacks[id]; |
|
|
662 |
}, this, id); |
|
|
663 |
|
|
|
664 |
// We are now creating a request |
|
|
665 |
uri += e.request + this.get("generateRequestCallback")(this, id); |
|
|
666 |
//uri = this.doBefore(sUri); |
|
|
667 |
Y.log("DataSource is querying URL " + uri, "info", "datasource-get"); |
|
|
668 |
get.script(uri, { |
|
|
669 |
autopurge: true, |
|
|
670 |
// Works in Firefox only.... |
|
|
671 |
onFailure: Y.bind(function(e) { |
|
|
672 |
e.error = new Error("Script node data failure"); |
|
|
673 |
this.fire("error", e); |
|
|
674 |
}, this, e) |
|
|
675 |
}); |
|
|
676 |
|
|
|
677 |
|
|
|
678 |
|
|
|
679 |
|
|
|
680 |
|
|
|
681 |
|
|
|
682 |
|
|
|
683 |
|
|
|
684 |
|
|
|
685 |
|
|
|
686 |
|
|
|
687 |
|
|
|
688 |
|
|
|
689 |
|
|
|
690 |
|
|
|
691 |
return e.tId; |
|
|
692 |
} |
|
|
693 |
}); |
|
|
694 |
|
|
|
695 |
Y.DataSource.Get = DSGet; |
|
|
696 |
YUI.namespace("Env.DataSource.callbacks"); |
|
|
697 |
|
|
|
698 |
|
|
|
699 |
|
|
|
700 |
|
|
|
701 |
}, '3.0.0b1' ,{requires:['datasource-local', 'get']}); |
|
|
702 |
|
|
|
703 |
YUI.add('datasource-function', function(Y) { |
|
|
704 |
|
|
|
705 |
/** |
|
|
706 |
* Provides a DataSource implementation which can be used to retrieve data from a custom function. |
|
|
707 |
* |
|
|
708 |
* @module datasource |
|
|
709 |
* @submodule datasource-function |
|
|
710 |
*/ |
|
|
711 |
|
|
|
712 |
/** |
|
|
713 |
* Function subclass for the DataSource Utility. |
|
|
714 |
* @class DataSource.Function |
|
|
715 |
* @extends DataSource.Local |
|
|
716 |
* @constructor |
|
|
717 |
*/ |
|
|
718 |
var LANG = Y.Lang, |
|
|
719 |
|
|
|
720 |
DSFn = function() { |
|
|
721 |
DSFn.superclass.constructor.apply(this, arguments); |
|
|
722 |
}; |
|
|
723 |
|
|
|
724 |
|
|
|
725 |
///////////////////////////////////////////////////////////////////////////// |
|
|
726 |
// |
|
|
727 |
// DataSource.Function static properties |
|
|
728 |
// |
|
|
729 |
///////////////////////////////////////////////////////////////////////////// |
|
|
730 |
Y.mix(DSFn, { |
|
|
731 |
/** |
|
|
732 |
* Class name. |
|
|
733 |
* |
|
|
734 |
* @property NAME |
|
|
735 |
* @type String |
|
|
736 |
* @static |
|
|
737 |
* @final |
|
|
738 |
* @value "dataSourceFunction" |
|
|
739 |
*/ |
|
|
740 |
NAME: "dataSourceFunction", |
|
|
741 |
|
|
|
742 |
|
|
|
743 |
///////////////////////////////////////////////////////////////////////////// |
|
|
744 |
// |
|
|
745 |
// DataSource.Function Attributes |
|
|
746 |
// |
|
|
747 |
///////////////////////////////////////////////////////////////////////////// |
|
|
748 |
|
|
|
749 |
ATTRS: { |
|
|
750 |
/** |
|
|
751 |
* @attribute source |
|
|
752 |
* @description Pointer to live data. |
|
|
753 |
* @type MIXED |
|
|
754 |
* @default null |
|
|
755 |
*/ |
|
|
756 |
source: { |
|
|
757 |
validator: LANG.isFunction |
|
|
758 |
} |
|
|
759 |
} |
|
|
760 |
}); |
|
|
761 |
|
|
|
762 |
Y.extend(DSFn, Y.DataSource.Local, { |
|
|
763 |
/** |
|
|
764 |
* Passes query string to IO. Fires <code>response</code> event when |
|
|
765 |
* response is received asynchronously. |
|
|
766 |
* |
|
|
767 |
* @method _defRequestFn |
|
|
768 |
* @param e {Event.Facade} Event Facade with the following properties: |
|
|
769 |
* <dl> |
|
|
770 |
* <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd> |
|
|
771 |
* <dt>request (Object)</dt> <dd>The request.</dd> |
|
|
772 |
* <dt>callback (Object)</dt> <dd>The callback object with the following properties: |
|
|
773 |
* <dl> |
|
|
774 |
* <dt>success (Function)</dt> <dd>Success handler.</dd> |
|
|
775 |
* <dt>failure (Function)</dt> <dd>Failure handler.</dd> |
|
|
776 |
* </dl> |
|
|
777 |
* </dd> |
|
|
778 |
* <dt>cfg (Object)</dt> <dd>Configuration object.</dd> |
|
|
779 |
* </dl> |
|
|
780 |
* @protected |
|
|
781 |
*/ |
|
|
782 |
_defRequestFn: function(e) { |
|
|
783 |
var fn = this.get("source"), |
|
|
784 |
response; |
|
|
785 |
|
|
|
786 |
if(fn) { |
|
|
787 |
response = fn(e.request, this, e); |
|
|
788 |
this.fire("data", Y.mix({data:response}, e)); |
|
|
789 |
} |
|
|
790 |
else { |
|
|
791 |
e.error = new Error("Function data failure"); |
|
|
792 |
this.fire("error", e); |
|
|
793 |
} |
|
|
794 |
|
|
|
795 |
return e.tId; |
|
|
796 |
} |
|
|
797 |
}); |
|
|
798 |
|
|
|
799 |
Y.DataSource.Function = DSFn; |
|
|
800 |
|
|
|
801 |
|
|
|
802 |
|
|
|
803 |
|
|
|
804 |
}, '3.0.0b1' ,{requires:['datasource-local']}); |
|
|
805 |
|
|
|
806 |
YUI.add('datasource-cache', function(Y) { |
|
|
807 |
|
|
|
808 |
/** |
|
|
809 |
* Extends DataSource with caching functionality. |
|
|
810 |
* |
|
|
811 |
* @module datasource |
|
|
812 |
* @submodule datasource-cache |
|
|
813 |
*/ |
|
|
814 |
|
|
|
815 |
/** |
|
|
816 |
* Adds cacheability to the DataSource Utility. |
|
|
817 |
* @class DataSourceCache |
|
|
818 |
* @extends Cache |
|
|
819 |
*/ |
|
|
820 |
var DataSourceCache = function() { |
|
|
821 |
DataSourceCache.superclass.constructor.apply(this, arguments); |
|
|
822 |
}; |
|
|
823 |
|
|
|
824 |
Y.mix(DataSourceCache, { |
|
|
825 |
/** |
|
|
826 |
* The namespace for the plugin. This will be the property on the host which |
|
|
827 |
* references the plugin instance. |
|
|
828 |
* |
|
|
829 |
* @property NS |
|
|
830 |
* @type String |
|
|
831 |
* @static |
|
|
832 |
* @final |
|
|
833 |
* @value "cache" |
|
|
834 |
*/ |
|
|
835 |
NS: "cache", |
|
|
836 |
|
|
|
837 |
/** |
|
|
838 |
* Class name. |
|
|
839 |
* |
|
|
840 |
* @property NAME |
|
|
841 |
* @type String |
|
|
842 |
* @static |
|
|
843 |
* @final |
|
|
844 |
* @value "dataSourceCache" |
|
|
845 |
*/ |
|
|
846 |
NAME: "dataSourceCache", |
|
|
847 |
|
|
|
848 |
///////////////////////////////////////////////////////////////////////////// |
|
|
849 |
// |
|
|
850 |
// DataSourceCache Attributes |
|
|
851 |
// |
|
|
852 |
///////////////////////////////////////////////////////////////////////////// |
|
|
853 |
|
|
|
854 |
ATTRS: { |
|
|
855 |
|
|
|
856 |
} |
|
|
857 |
}); |
|
|
858 |
|
|
|
859 |
Y.extend(DataSourceCache, Y.Cache, { |
|
|
860 |
/** |
|
|
861 |
* Internal init() handler. |
|
|
862 |
* |
|
|
863 |
* @method initializer |
|
|
864 |
* @param config {Object} Config object. |
|
|
865 |
* @private |
|
|
866 |
*/ |
|
|
867 |
initializer: function(config) { |
|
|
868 |
this.doBefore("_defRequestFn", this._beforeDefRequestFn); |
|
|
869 |
this.doBefore("_defResponseFn", this._beforeDefResponseFn); |
|
|
870 |
}, |
|
|
871 |
|
|
|
872 |
/** |
|
|
873 |
* First look for cached response, then send request to live data. |
|
|
874 |
* |
|
|
875 |
* @method _beforeDefRequestFn |
|
|
876 |
* @param e {Event.Facade} Event Facade with the following properties: |
|
|
877 |
* <dl> |
|
|
878 |
* <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd> |
|
|
879 |
* <dt>request (Object)</dt> <dd>The request.</dd> |
|
|
880 |
* <dt>callback (Object)</dt> <dd>The callback object.</dd> |
|
|
881 |
* <dt>cfg (Object)</dt> <dd>Configuration object.</dd> |
|
|
882 |
* </dl> |
|
|
883 |
* @protected |
|
|
884 |
*/ |
|
|
885 |
_beforeDefRequestFn: function(e) { |
|
|
886 |
// Is response already in the Cache? |
|
|
887 |
var entry = (this.retrieve(e.request)) || null; |
|
|
888 |
if(entry && entry.response) { |
|
|
889 |
this.get("host").fire("response", Y.mix({response: entry.response}, e)); |
|
|
890 |
return new Y.Do.Halt("DataSourceCache plugin halted _defRequestFn"); |
|
|
891 |
} |
|
|
892 |
}, |
|
|
893 |
|
|
|
894 |
/** |
|
|
895 |
* Adds data to cache before returning data. |
|
|
896 |
* |
|
|
897 |
* @method _beforeDefResponseFn |
|
|
898 |
* @param e {Event.Facade} Event Facade with the following properties: |
|
|
899 |
* <dl> |
|
|
900 |
* <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd> |
|
|
901 |
* <dt>request (Object)</dt> <dd>The request.</dd> |
|
|
902 |
* <dt>callback (Object)</dt> <dd>The callback object with the following properties: |
|
|
903 |
* <dl> |
|
|
904 |
* <dt>success (Function)</dt> <dd>Success handler.</dd> |
|
|
905 |
* <dt>failure (Function)</dt> <dd>Failure handler.</dd> |
|
|
906 |
* </dl> |
|
|
907 |
* </dd> |
|
|
908 |
* <dt>data (Object)</dt> <dd>Raw data.</dd> |
|
|
909 |
* <dt>response (Object)</dt> <dd>Normalized response object with the following properties: |
|
|
910 |
* <dl> |
|
|
911 |
* <dt>cached (Object)</dt> <dd>True when response is cached.</dd> |
|
|
912 |
* <dt>results (Object)</dt> <dd>Parsed results.</dd> |
|
|
913 |
* <dt>meta (Object)</dt> <dd>Parsed meta data.</dd> |
|
|
914 |
* <dt>error (Object)</dt> <dd>Error object.</dd> |
|
|
915 |
* </dl> |
|
|
916 |
* </dd> |
|
|
917 |
* <dt>cfg (Object)</dt> <dd>Configuration object.</dd> |
|
|
918 |
* </dl> |
|
|
919 |
* @protected |
|
|
920 |
*/ |
|
|
921 |
_beforeDefResponseFn: function(e) { |
|
|
922 |
// Add to Cache before returning |
|
|
923 |
if(e.response && !e.response.cached) { |
|
|
924 |
e.response.cached = true; |
|
|
925 |
this.add(e.request, e.response, (e.callback && e.callback.argument)); |
|
|
926 |
} |
|
|
927 |
} |
|
|
928 |
}); |
|
|
929 |
|
|
|
930 |
Y.namespace('Plugin').DataSourceCache = DataSourceCache; |
|
|
931 |
|
|
|
932 |
|
|
|
933 |
|
|
|
934 |
}, '3.0.0b1' ,{requires:['datasource-local', 'cache']}); |
|
|
935 |
|
|
|
936 |
YUI.add('datasource-jsonschema', function(Y) { |
|
|
937 |
|
|
|
938 |
/** |
|
|
939 |
* Extends DataSource with schema-parsing on JSON data. |
|
|
940 |
* |
|
|
941 |
* @module datasource |
|
|
942 |
* @submodule datasource-jsonschema |
|
|
943 |
*/ |
|
|
944 |
|
|
|
945 |
/** |
|
|
946 |
* Adds schema-parsing to the DataSource Utility. |
|
|
947 |
* @class DataSourceJSONSchema |
|
|
948 |
* @extends Plugin.Base |
|
|
949 |
*/ |
|
|
950 |
var DataSourceJSONSchema = function() { |
|
|
951 |
DataSourceJSONSchema.superclass.constructor.apply(this, arguments); |
|
|
952 |
}; |
|
|
953 |
|
|
|
954 |
Y.mix(DataSourceJSONSchema, { |
|
|
955 |
/** |
|
|
956 |
* The namespace for the plugin. This will be the property on the host which |
|
|
957 |
* references the plugin instance. |
|
|
958 |
* |
|
|
959 |
* @property NS |
|
|
960 |
* @type String |
|
|
961 |
* @static |
|
|
962 |
* @final |
|
|
963 |
* @value "schema" |
|
|
964 |
*/ |
|
|
965 |
NS: "schema", |
|
|
966 |
|
|
|
967 |
/** |
|
|
968 |
* Class name. |
|
|
969 |
* |
|
|
970 |
* @property NAME |
|
|
971 |
* @type String |
|
|
972 |
* @static |
|
|
973 |
* @final |
|
|
974 |
* @value "dataSourceJSONSchema" |
|
|
975 |
*/ |
|
|
976 |
NAME: "dataSourceJSONSchema", |
|
|
977 |
|
|
|
978 |
///////////////////////////////////////////////////////////////////////////// |
|
|
979 |
// |
|
|
980 |
// DataSourceJSONSchema Attributes |
|
|
981 |
// |
|
|
982 |
///////////////////////////////////////////////////////////////////////////// |
|
|
983 |
|
|
|
984 |
ATTRS: { |
|
|
985 |
schema: { |
|
|
986 |
//value: {} |
|
|
987 |
} |
|
|
988 |
} |
|
|
989 |
}); |
|
|
990 |
|
|
|
991 |
Y.extend(DataSourceJSONSchema, Y.Plugin.Base, { |
|
|
992 |
/** |
|
|
993 |
* Internal init() handler. |
|
|
994 |
* |
|
|
995 |
* @method initializer |
|
|
996 |
* @param config {Object} Config object. |
|
|
997 |
* @private |
|
|
998 |
*/ |
|
|
999 |
initializer: function(config) { |
|
|
1000 |
this.doBefore("_defDataFn", this._beforeDefDataFn); |
|
|
1001 |
}, |
|
|
1002 |
|
|
|
1003 |
/** |
|
|
1004 |
* Parses raw data into a normalized response. |
|
|
1005 |
* |
|
|
1006 |
* @method _beforeDefDataFn |
|
|
1007 |
* <dl> |
|
|
1008 |
* <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd> |
|
|
1009 |
* <dt>request (Object)</dt> <dd>The request.</dd> |
|
|
1010 |
* <dt>callback (Object)</dt> <dd>The callback object with the following properties: |
|
|
1011 |
* <dl> |
|
|
1012 |
* <dt>success (Function)</dt> <dd>Success handler.</dd> |
|
|
1013 |
* <dt>failure (Function)</dt> <dd>Failure handler.</dd> |
|
|
1014 |
* </dl> |
|
|
1015 |
* </dd> |
|
|
1016 |
* <dt>data (Object)</dt> <dd>Raw data.</dd> |
|
|
1017 |
* </dl> |
|
|
1018 |
* @protected |
|
|
1019 |
*/ |
|
|
1020 |
_beforeDefDataFn: function(e) { |
|
|
1021 |
var data = (Y.DataSource.IO && (this.get("host") instanceof Y.DataSource.IO) && Y.Lang.isString(e.data.responseText)) ? e.data.responseText : e.data, |
|
|
1022 |
response = Y.DataSchema.JSON.apply(this.get("schema"), data); |
|
|
1023 |
|
|
|
1024 |
// Default |
|
|
1025 |
if(!response) { |
|
|
1026 |
response = { |
|
|
1027 |
meta: {}, |
|
|
1028 |
results: data |
|
|
1029 |
}; |
|
|
1030 |
} |
|
|
1031 |
|
|
|
1032 |
this.get("host").fire("response", Y.mix({response:response}, e)); |
|
|
1033 |
return new Y.Do.Halt("DataSourceJSONSchema plugin halted _defDataFn"); |
|
|
1034 |
} |
|
|
1035 |
}); |
|
|
1036 |
|
|
|
1037 |
Y.namespace('Plugin').DataSourceJSONSchema = DataSourceJSONSchema; |
|
|
1038 |
|
|
|
1039 |
|
|
|
1040 |
|
|
|
1041 |
}, '3.0.0b1' ,{requires:['plugin', 'datasource-local', 'dataschema-json']}); |
|
|
1042 |
|
|
|
1043 |
YUI.add('datasource-xmlschema', function(Y) { |
|
|
1044 |
|
|
|
1045 |
/** |
|
|
1046 |
* Extends DataSource with schema-parsing on XML data. |
|
|
1047 |
* |
|
|
1048 |
* @module datasource |
|
|
1049 |
* @submodule datasource-xmlschema |
|
|
1050 |
*/ |
|
|
1051 |
|
|
|
1052 |
/** |
|
|
1053 |
* Adds schema-parsing to the DataSource Utility. |
|
|
1054 |
* @class DataSourceXMLSchema |
|
|
1055 |
* @extends Plugin.Base |
|
|
1056 |
*/ |
|
|
1057 |
var DataSourceXMLSchema = function() { |
|
|
1058 |
DataSourceXMLSchema.superclass.constructor.apply(this, arguments); |
|
|
1059 |
}; |
|
|
1060 |
|
|
|
1061 |
Y.mix(DataSourceXMLSchema, { |
|
|
1062 |
/** |
|
|
1063 |
* The namespace for the plugin. This will be the property on the host which |
|
|
1064 |
* references the plugin instance. |
|
|
1065 |
* |
|
|
1066 |
* @property NS |
|
|
1067 |
* @type String |
|
|
1068 |
* @static |
|
|
1069 |
* @final |
|
|
1070 |
* @value "schema" |
|
|
1071 |
*/ |
|
|
1072 |
NS: "schema", |
|
|
1073 |
|
|
|
1074 |
/** |
|
|
1075 |
* Class name. |
|
|
1076 |
* |
|
|
1077 |
* @property NAME |
|
|
1078 |
* @type String |
|
|
1079 |
* @static |
|
|
1080 |
* @final |
|
|
1081 |
* @value "dataSourceXMLSchema" |
|
|
1082 |
*/ |
|
|
1083 |
NAME: "dataSourceXMLSchema", |
|
|
1084 |
|
|
|
1085 |
///////////////////////////////////////////////////////////////////////////// |
|
|
1086 |
// |
|
|
1087 |
// DataSourceXMLSchema Attributes |
|
|
1088 |
// |
|
|
1089 |
///////////////////////////////////////////////////////////////////////////// |
|
|
1090 |
|
|
|
1091 |
ATTRS: { |
|
|
1092 |
schema: { |
|
|
1093 |
//value: {} |
|
|
1094 |
} |
|
|
1095 |
} |
|
|
1096 |
}); |
|
|
1097 |
|
|
|
1098 |
Y.extend(DataSourceXMLSchema, Y.Plugin.Base, { |
|
|
1099 |
/** |
|
|
1100 |
* Internal init() handler. |
|
|
1101 |
* |
|
|
1102 |
* @method initializer |
|
|
1103 |
* @param config {Object} Config object. |
|
|
1104 |
* @private |
|
|
1105 |
*/ |
|
|
1106 |
initializer: function(config) { |
|
|
1107 |
this.doBefore("_defDataFn", this._beforeDefDataFn); |
|
|
1108 |
}, |
|
|
1109 |
|
|
|
1110 |
/** |
|
|
1111 |
* Parses raw data into a normalized response. |
|
|
1112 |
* |
|
|
1113 |
* @method _beforeDefDataFn |
|
|
1114 |
* <dl> |
|
|
1115 |
* <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd> |
|
|
1116 |
* <dt>request (Object)</dt> <dd>The request.</dd> |
|
|
1117 |
* <dt>callback (Object)</dt> <dd>The callback object with the following properties: |
|
|
1118 |
* <dl> |
|
|
1119 |
* <dt>success (Function)</dt> <dd>Success handler.</dd> |
|
|
1120 |
* <dt>failure (Function)</dt> <dd>Failure handler.</dd> |
|
|
1121 |
* </dl> |
|
|
1122 |
* </dd> |
|
|
1123 |
* <dt>data (Object)</dt> <dd>Raw data.</dd> |
|
|
1124 |
* </dl> |
|
|
1125 |
* @protected |
|
|
1126 |
*/ |
|
|
1127 |
_beforeDefDataFn: function(e) { |
|
|
1128 |
var data = (Y.DataSource.IO && (this.get("host") instanceof Y.DataSource.IO) && e.data.responseXML && (e.data.responseXML.nodeType === 9)) ? e.data.responseXML : e.data, |
|
|
1129 |
response = Y.DataSchema.XML.apply(this.get("schema"), data); |
|
|
1130 |
|
|
|
1131 |
// Default |
|
|
1132 |
if(!response) { |
|
|
1133 |
response = { |
|
|
1134 |
meta: {}, |
|
|
1135 |
results: data |
|
|
1136 |
}; |
|
|
1137 |
} |
|
|
1138 |
|
|
|
1139 |
this.get("host").fire("response", Y.mix({response:response}, e)); |
|
|
1140 |
return new Y.Do.Halt("DataSourceXMLSchema plugin halted _defDataFn"); |
|
|
1141 |
} |
|
|
1142 |
}); |
|
|
1143 |
|
|
|
1144 |
Y.namespace('Plugin').DataSourceXMLSchema = DataSourceXMLSchema; |
|
|
1145 |
|
|
|
1146 |
|
|
|
1147 |
|
|
|
1148 |
}, '3.0.0b1' ,{requires:['plugin', 'datasource-local', 'dataschema-xml']}); |
|
|
1149 |
|
|
|
1150 |
YUI.add('datasource-arrayschema', function(Y) { |
|
|
1151 |
|
|
|
1152 |
/** |
|
|
1153 |
* Extends DataSource with schema-parsing on array data. |
|
|
1154 |
* |
|
|
1155 |
* @module datasource |
|
|
1156 |
* @submodule datasource-arrayschema |
|
|
1157 |
*/ |
|
|
1158 |
|
|
|
1159 |
/** |
|
|
1160 |
* Adds schema-parsing to the DataSource Utility. |
|
|
1161 |
* @class DataSourceArraySchema |
|
|
1162 |
* @extends Plugin.Base |
|
|
1163 |
*/ |
|
|
1164 |
var DataSourceArraySchema = function() { |
|
|
1165 |
DataSourceArraySchema.superclass.constructor.apply(this, arguments); |
|
|
1166 |
}; |
|
|
1167 |
|
|
|
1168 |
Y.mix(DataSourceArraySchema, { |
|
|
1169 |
/** |
|
|
1170 |
* The namespace for the plugin. This will be the property on the host which |
|
|
1171 |
* references the plugin instance. |
|
|
1172 |
* |
|
|
1173 |
* @property NS |
|
|
1174 |
* @type String |
|
|
1175 |
* @static |
|
|
1176 |
* @final |
|
|
1177 |
* @value "schema" |
|
|
1178 |
*/ |
|
|
1179 |
NS: "schema", |
|
|
1180 |
|
|
|
1181 |
/** |
|
|
1182 |
* Class name. |
|
|
1183 |
* |
|
|
1184 |
* @property NAME |
|
|
1185 |
* @type String |
|
|
1186 |
* @static |
|
|
1187 |
* @final |
|
|
1188 |
* @value "dataSourceArraySchema" |
|
|
1189 |
*/ |
|
|
1190 |
NAME: "dataSourceArraySchema", |
|
|
1191 |
|
|
|
1192 |
///////////////////////////////////////////////////////////////////////////// |
|
|
1193 |
// |
|
|
1194 |
// DataSourceArraySchema Attributes |
|
|
1195 |
// |
|
|
1196 |
///////////////////////////////////////////////////////////////////////////// |
|
|
1197 |
|
|
|
1198 |
ATTRS: { |
|
|
1199 |
schema: { |
|
|
1200 |
//value: {} |
|
|
1201 |
} |
|
|
1202 |
} |
|
|
1203 |
}); |
|
|
1204 |
|
|
|
1205 |
Y.extend(DataSourceArraySchema, Y.Plugin.Base, { |
|
|
1206 |
/** |
|
|
1207 |
* Internal init() handler. |
|
|
1208 |
* |
|
|
1209 |
* @method initializer |
|
|
1210 |
* @param config {Object} Config object. |
|
|
1211 |
* @private |
|
|
1212 |
*/ |
|
|
1213 |
initializer: function(config) { |
|
|
1214 |
this.doBefore("_defDataFn", this._beforeDefDataFn); |
|
|
1215 |
}, |
|
|
1216 |
|
|
|
1217 |
/** |
|
|
1218 |
* Parses raw data into a normalized response. |
|
|
1219 |
* |
|
|
1220 |
* @method _beforeDefDataFn |
|
|
1221 |
* <dl> |
|
|
1222 |
* <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd> |
|
|
1223 |
* <dt>request (Object)</dt> <dd>The request.</dd> |
|
|
1224 |
* <dt>callback (Object)</dt> <dd>The callback object with the following properties: |
|
|
1225 |
* <dl> |
|
|
1226 |
* <dt>success (Function)</dt> <dd>Success handler.</dd> |
|
|
1227 |
* <dt>failure (Function)</dt> <dd>Failure handler.</dd> |
|
|
1228 |
* </dl> |
|
|
1229 |
* </dd> |
|
|
1230 |
* <dt>data (Object)</dt> <dd>Raw data.</dd> |
|
|
1231 |
* </dl> |
|
|
1232 |
* @protected |
|
|
1233 |
*/ |
|
|
1234 |
_beforeDefDataFn: function(e) { |
|
|
1235 |
var data = (Y.DataSource.IO && (this.get("host") instanceof Y.DataSource.IO) && Y.Lang.isString(e.data.responseText)) ? e.data.responseText : e.data, |
|
|
1236 |
response = Y.DataSchema.Array.apply(this.get("schema"), data); |
|
|
1237 |
|
|
|
1238 |
// Default |
|
|
1239 |
if(!response) { |
|
|
1240 |
response = { |
|
|
1241 |
meta: {}, |
|
|
1242 |
results: data |
|
|
1243 |
}; |
|
|
1244 |
} |
|
|
1245 |
|
|
|
1246 |
this.get("host").fire("response", Y.mix({response:response}, e)); |
|
|
1247 |
return new Y.Do.Halt("DataSourceArraySchema plugin halted _defDataFn"); |
|
|
1248 |
} |
|
|
1249 |
}); |
|
|
1250 |
|
|
|
1251 |
Y.namespace('Plugin').DataSourceArraySchema = DataSourceArraySchema; |
|
|
1252 |
|
|
|
1253 |
|
|
|
1254 |
|
|
|
1255 |
}, '3.0.0b1' ,{requires:['plugin', 'datasource-local', 'dataschema-array']}); |
|
|
1256 |
|
|
|
1257 |
YUI.add('datasource-textschema', function(Y) { |
|
|
1258 |
|
|
|
1259 |
/** |
|
|
1260 |
* Extends DataSource with schema-parsing on text data. |
|
|
1261 |
* |
|
|
1262 |
* @module datasource |
|
|
1263 |
* @submodule datasource-textschema |
|
|
1264 |
*/ |
|
|
1265 |
|
|
|
1266 |
/** |
|
|
1267 |
* Adds schema-parsing to the DataSource Utility. |
|
|
1268 |
* @class DataSourceTextSchema |
|
|
1269 |
* @extends Plugin.Base |
|
|
1270 |
*/ |
|
|
1271 |
var DataSourceTextSchema = function() { |
|
|
1272 |
DataSourceTextSchema.superclass.constructor.apply(this, arguments); |
|
|
1273 |
}; |
|
|
1274 |
|
|
|
1275 |
Y.mix(DataSourceTextSchema, { |
|
|
1276 |
/** |
|
|
1277 |
* The namespace for the plugin. This will be the property on the host which |
|
|
1278 |
* references the plugin instance. |
|
|
1279 |
* |
|
|
1280 |
* @property NS |
|
|
1281 |
* @type String |
|
|
1282 |
* @static |
|
|
1283 |
* @final |
|
|
1284 |
* @value "schema" |
|
|
1285 |
*/ |
|
|
1286 |
NS: "schema", |
|
|
1287 |
|
|
|
1288 |
/** |
|
|
1289 |
* Class name. |
|
|
1290 |
* |
|
|
1291 |
* @property NAME |
|
|
1292 |
* @type String |
|
|
1293 |
* @static |
|
|
1294 |
* @final |
|
|
1295 |
* @value "dataSourceTextSchema" |
|
|
1296 |
*/ |
|
|
1297 |
NAME: "dataSourceTextSchema", |
|
|
1298 |
|
|
|
1299 |
///////////////////////////////////////////////////////////////////////////// |
|
|
1300 |
// |
|
|
1301 |
// DataSourceTextSchema Attributes |
|
|
1302 |
// |
|
|
1303 |
///////////////////////////////////////////////////////////////////////////// |
|
|
1304 |
|
|
|
1305 |
ATTRS: { |
|
|
1306 |
schema: { |
|
|
1307 |
//value: {} |
|
|
1308 |
} |
|
|
1309 |
} |
|
|
1310 |
}); |
|
|
1311 |
|
|
|
1312 |
Y.extend(DataSourceTextSchema, Y.Plugin.Base, { |
|
|
1313 |
/** |
|
|
1314 |
* Internal init() handler. |
|
|
1315 |
* |
|
|
1316 |
* @method initializer |
|
|
1317 |
* @param config {Object} Config object. |
|
|
1318 |
* @private |
|
|
1319 |
*/ |
|
|
1320 |
initializer: function(config) { |
|
|
1321 |
this.doBefore("_defDataFn", this._beforeDefDataFn); |
|
|
1322 |
}, |
|
|
1323 |
|
|
|
1324 |
/** |
|
|
1325 |
* Parses raw data into a normalized response. |
|
|
1326 |
* |
|
|
1327 |
* @method _beforeDefDataFn |
|
|
1328 |
* <dl> |
|
|
1329 |
* <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd> |
|
|
1330 |
* <dt>request (Object)</dt> <dd>The request.</dd> |
|
|
1331 |
* <dt>callback (Object)</dt> <dd>The callback object with the following properties: |
|
|
1332 |
* <dl> |
|
|
1333 |
* <dt>success (Function)</dt> <dd>Success handler.</dd> |
|
|
1334 |
* <dt>failure (Function)</dt> <dd>Failure handler.</dd> |
|
|
1335 |
* </dl> |
|
|
1336 |
* </dd> |
|
|
1337 |
* <dt>data (Object)</dt> <dd>Raw data.</dd> |
|
|
1338 |
* </dl> |
|
|
1339 |
* @protected |
|
|
1340 |
*/ |
|
|
1341 |
_beforeDefDataFn: function(e) { |
|
|
1342 |
var data = (Y.DataSource.IO && (this.get("host") instanceof Y.DataSource.IO) && Y.Lang.isString(e.data.responseText)) ? e.data.responseText : e.data, |
|
|
1343 |
response = Y.DataSchema.Text.apply(this.get("schema"), data); |
|
|
1344 |
|
|
|
1345 |
// Default |
|
|
1346 |
if(!response) { |
|
|
1347 |
response = { |
|
|
1348 |
meta: {}, |
|
|
1349 |
results: data |
|
|
1350 |
}; |
|
|
1351 |
} |
|
|
1352 |
|
|
|
1353 |
this.get("host").fire("response", Y.mix({response:response}, e)); |
|
|
1354 |
return new Y.Do.Halt("DataSourceTextSchema plugin halted _defDataFn"); |
|
|
1355 |
} |
|
|
1356 |
}); |
|
|
1357 |
|
|
|
1358 |
Y.namespace('Plugin').DataSourceTextSchema = DataSourceTextSchema; |
|
|
1359 |
|
|
|
1360 |
|
|
|
1361 |
|
|
|
1362 |
}, '3.0.0b1' ,{requires:['plugin', 'datasource-local', 'dataschema-text']}); |
|
|
1363 |
|
|
|
1364 |
YUI.add('datasource-polling', function(Y) { |
|
|
1365 |
|
|
|
1366 |
/** |
|
|
1367 |
* Extends DataSource with polling functionality. |
|
|
1368 |
* |
|
|
1369 |
* @module datasource |
|
|
1370 |
* @submodule datasource-polling |
|
|
1371 |
*/ |
|
|
1372 |
|
|
|
1373 |
/** |
|
|
1374 |
* Adds polling to the DataSource Utility. |
|
|
1375 |
* @class Pollable |
|
|
1376 |
* @extends DataSource.Local |
|
|
1377 |
*/ |
|
|
1378 |
var LANG = Y.Lang, |
|
|
1379 |
|
|
|
1380 |
Pollable = function() { |
|
|
1381 |
this._intervals = {}; |
|
|
1382 |
}; |
|
|
1383 |
|
|
|
1384 |
Pollable.prototype = { |
|
|
1385 |
|
|
|
1386 |
/** |
|
|
1387 |
* @property _intervals |
|
|
1388 |
* @description Hash of polling interval IDs that have been enabled, |
|
|
1389 |
* stored here to be able to clear all intervals. |
|
|
1390 |
* @private |
|
|
1391 |
*/ |
|
|
1392 |
_intervals: null, |
|
|
1393 |
|
|
|
1394 |
/** |
|
|
1395 |
* Sets up a polling mechanism to send requests at set intervals and forward |
|
|
1396 |
* responses to given callback. |
|
|
1397 |
* |
|
|
1398 |
* @method setInterval |
|
|
1399 |
* @param msec {Number} Length of interval in milliseconds. |
|
|
1400 |
* @param request {Object} Request object. |
|
|
1401 |
* @param callback {Object} An object literal with the following properties: |
|
|
1402 |
* <dl> |
|
|
1403 |
* <dt><code>success</code></dt> |
|
|
1404 |
* <dd>The function to call when the data is ready.</dd> |
|
|
1405 |
* <dt><code>failure</code></dt> |
|
|
1406 |
* <dd>The function to call upon a response failure condition.</dd> |
|
|
1407 |
* <dt><code>argument</code></dt> |
|
|
1408 |
* <dd>Arbitrary data that will be passed back to the success and failure handlers.</dd> |
|
|
1409 |
* </dl> |
|
|
1410 |
* @return {Number} Interval ID. |
|
|
1411 |
*/ |
|
|
1412 |
setInterval: function(msec, request, callback) { |
|
|
1413 |
var x = Y.later(msec, this, this.sendRequest, [request, callback], true); |
|
|
1414 |
this._intervals[x.id] = x; |
|
|
1415 |
return x.id; |
|
|
1416 |
}, |
|
|
1417 |
|
|
|
1418 |
/** |
|
|
1419 |
* Disables polling mechanism associated with the given interval ID. |
|
|
1420 |
* |
|
|
1421 |
* @method clearInterval |
|
|
1422 |
* @param id {Number} Interval ID. |
|
|
1423 |
*/ |
|
|
1424 |
clearInterval: function(id, key) { |
|
|
1425 |
// In case of being called by clearAllIntervals() |
|
|
1426 |
id = key || id; |
|
|
1427 |
if(this._intervals[id]) { |
|
|
1428 |
// Clear the interval |
|
|
1429 |
this._intervals[id].cancel(); |
|
|
1430 |
// Clear from tracker |
|
|
1431 |
delete this._intervals[id]; |
|
|
1432 |
} |
|
|
1433 |
}, |
|
|
1434 |
|
|
|
1435 |
/** |
|
|
1436 |
* Clears all intervals. |
|
|
1437 |
* |
|
|
1438 |
* @method clearAllIntervals |
|
|
1439 |
*/ |
|
|
1440 |
clearAllIntervals: function() { |
|
|
1441 |
Y.each(this._intervals, this.clearInterval, this); |
|
|
1442 |
} |
|
|
1443 |
}; |
|
|
1444 |
|
|
|
1445 |
Y.augment(Y.DataSource.Local, Pollable); |
|
|
1446 |
|
|
|
1447 |
|
|
|
1448 |
|
|
|
1449 |
}, '3.0.0b1' ,{requires:['datasource-local']}); |
|
|
1450 |
|
|
|
1451 |
|
|
|
1452 |
|
|
|
1453 |
YUI.add('datasource', function(Y){}, '3.0.0b1' ,{use:['datasource-local','datasource-io','datasource-get','datasource-function','datasource-cache','datasource-jsonschema','datasource-xmlschema','datasource-arrayschema','datasource-textschema','datasource-polling']}); |
|
|
1454 |
|