2
YUI 3.10.3 (build 2fb5187)
3
Copyright 2013 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
8
YUI.add('datasource-local', function (Y, NAME) {
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.
19
* Provides the base DataSource implementation, which can be extended to
20
* create DataSources for specific data protocols, such as the IO Utility, the
21
* Get Utility, or custom functions.
24
* @submodule datasource-local
28
* Base class for the DataSource Utility.
29
* @class DataSource.Local
35
DSLocal = function() {
36
DSLocal.superclass.constructor.apply(this, arguments);
39
/////////////////////////////////////////////////////////////////////////////
41
// DataSource static properties
43
/////////////////////////////////////////////////////////////////////////////
52
* @value "dataSourceLocal"
54
NAME: "dataSourceLocal",
56
/////////////////////////////////////////////////////////////////////////////
58
// DataSource Attributes
60
/////////////////////////////////////////////////////////////////////////////
65
* @description Pointer to live data.
75
* Global transaction counter.
86
* Global in-progress transaction objects.
88
* @property transactions
95
* Returns data to callback.
97
* @method issueCallback
98
* @param e {EventFacade} Event Facade.
99
* @param caller {DataSource} Calling DataSource instance.
102
issueCallback: function (e, caller) {
103
var callbacks = e.on || e.callback,
104
callback = callbacks && callbacks.success,
105
payload = e.details[0];
107
payload.error = (e.error || e.response.error);
110
caller.fire("error", payload);
111
callback = callbacks && callbacks.failure;
115
//TODO: this should be executed from a specific context
121
Y.extend(DSLocal, Y.Base, {
123
* Internal init() handler.
125
* @method initializer
126
* @param config {Object} Config object.
129
initializer: function(config) {
134
* This method creates all the events for this module.
135
* @method _initEvents
138
_initEvents: function() {
140
* Fired when a data request is received.
143
* @param e {Event.Facade} Event Facade with the following properties:
145
* <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
146
* <dt>request (Object)</dt> <dd>The request.</dd>
147
* <dt>callback (Object)</dt> <dd>The callback object
148
* (deprecated, refer to <strong>on</strong></dd>
149
* <dt>on (Object)</dt> <dd>The map of configured callback
151
* <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
153
* @preventable _defRequestFn
155
this.publish("request", {defaultFn: Y.bind("_defRequestFn", this), queuable:true});
158
* Fired when raw data is received.
161
* @param e {Event.Facade} Event Facade with the following properties:
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>Deprecated alias for the
166
* <strong>on</strong> property</dd>
167
* <dt>on (Object)</dt> <dd>The map of configured transaction
168
* callbacks. An object with the following properties:
170
* <dt>success (Function)</dt> <dd>Success handler.</dd>
171
* <dt>failure (Function)</dt> <dd>Failure handler.</dd>
174
* <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
175
* <dt>data (Object)</dt> <dd>Raw data.</dd>
177
* @preventable _defDataFn
179
this.publish("data", {defaultFn: Y.bind("_defDataFn", this), queuable:true});
182
* Fired when response is returned.
185
* @param e {Event.Facade} Event Facade with the following properties:
187
* <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
188
* <dt>request (Object)</dt> <dd>The request.</dd>
189
* <dt>callback (Object)</dt> <dd>Deprecated alias for the
190
* <strong>on</strong> property</dd>
191
* <dt>on (Object)</dt> <dd>The map of configured transaction
192
* callbacks. An object with the following properties:
194
* <dt>success (Function)</dt> <dd>Success handler.</dd>
195
* <dt>failure (Function)</dt> <dd>Failure handler.</dd>
198
* <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
199
* <dt>data (Object)</dt> <dd>Raw data.</dd>
200
* <dt>response (Object)</dt>
201
* <dd>Normalized response object with the following properties:
203
* <dt>results (Object)</dt> <dd>Parsed results.</dd>
204
* <dt>meta (Object)</dt> <dd>Parsed meta data.</dd>
205
* <dt>error (Boolean)</dt> <dd>Error flag.</dd>
209
* <dd>Any error that occurred along the transaction lifecycle.</dd>
211
* @preventable _defResponseFn
213
this.publish("response", {defaultFn: Y.bind("_defResponseFn", this), queuable:true});
216
* Fired when an error is encountered.
219
* @param e {Event.Facade} Event Facade with the following properties:
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>Deprecated alias for the
224
* <strong>on</strong> property</dd>
225
* <dt>on (Object)</dt> <dd>The map of configured transaction
226
* callbacks. An object with the following properties:
228
* <dt>success (Function)</dt> <dd>Success handler.</dd>
229
* <dt>failure (Function)</dt> <dd>Failure handler.</dd>
232
* <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
233
* <dt>data (Object)</dt> <dd>Raw data.</dd>
234
* <dt>response (Object)</dt>
235
* <dd>Normalized response object with the following properties:
237
* <dt>results (Object)</dt> <dd>Parsed results.</dd>
238
* <dt>meta (Object)</dt> <dd>Parsed meta data.</dd>
239
* <dt>error (Object)</dt> <dd>Error object.</dd>
243
* <dd>Any error that occurred along the transaction lifecycle.</dd>
250
* Manages request/response transaction. Must fire <code>response</code>
251
* event when response is received. This method should be implemented by
252
* subclasses to achieve more complex behavior such as accessing remote data.
254
* @method _defRequestFn
255
* @param e {Event.Facade} Event Facadewith the following properties:
257
* <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
258
* <dt>request (Object)</dt> <dd>The request.</dd>
259
* <dt>callback (Object)</dt> <dd>Deprecated alias for the
260
* <strong>on</strong> property</dd>
261
* <dt>on (Object)</dt> <dd>The map of configured transaction
262
* callbacks. An object with the following properties:
264
* <dt>success (Function)</dt> <dd>Success handler.</dd>
265
* <dt>failure (Function)</dt> <dd>Failure handler.</dd>
268
* <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
272
_defRequestFn: function(e) {
273
var data = this.get("source"),
274
payload = e.details[0];
277
if(LANG.isUndefined(data)) {
278
payload.error = new Error("Local source undefined");
282
this.fire("data", payload);
286
* Normalizes raw data into a response that includes results and meta properties.
289
* @param e {Event.Facade} Event Facade with the following properties:
291
* <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
292
* <dt>request (Object)</dt> <dd>The request.</dd>
293
* <dt>callback (Object)</dt> <dd>Deprecated alias for the
294
* <strong>on</strong> property</dd>
295
* <dt>on (Object)</dt> <dd>The map of configured transaction
296
* callbacks. An object with the following properties:
298
* <dt>success (Function)</dt> <dd>Success handler.</dd>
299
* <dt>failure (Function)</dt> <dd>Failure handler.</dd>
302
* <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
303
* <dt>data (Object)</dt> <dd>Raw data.</dd>
307
_defDataFn: function(e) {
311
results: (LANG.isArray(data)) ? data : [data],
312
meta: (meta) ? meta : {}
314
payload = e.details[0];
316
payload.response = response;
317
this.fire("response", payload);
321
* Sends data as a normalized response to callback.
323
* @method _defResponseFn
324
* @param e {Event.Facade} Event Facade with the following properties:
326
* <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
327
* <dt>request (Object)</dt> <dd>The request.</dd>
328
* <dt>callback (Object)</dt> <dd>Deprecated alias for the
329
* <strong>on</strong> property</dd>
330
* <dt>on (Object)</dt> <dd>The map of configured transaction
331
* callbacks. An object with the following properties:
333
* <dt>success (Function)</dt> <dd>Success handler.</dd>
334
* <dt>failure (Function)</dt> <dd>Failure handler.</dd>
337
* <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
338
* <dt>data (Object)</dt> <dd>Raw data.</dd>
339
* <dt>response (Object)</dt> <dd>Normalized response object with the following properties:
341
* <dt>results (Object)</dt> <dd>Parsed results.</dd>
342
* <dt>meta (Object)</dt> <dd>Parsed meta data.</dd>
343
* <dt>error (Boolean)</dt> <dd>Error flag.</dd>
349
_defResponseFn: function(e) {
350
// Send the response back to the callback
351
DSLocal.issueCallback(e, this);
355
* Generates a unique transaction ID and fires <code>request</code> event.
356
* <strong>Note</strong>: the property <code>callback</code> is a
357
* deprecated alias for the <code>on</code> transaction configuration
358
* property described below.
360
* @method sendRequest
361
* @param [request] {Object} An object literal with the following properties:
363
* <dt><code>request</code></dt>
364
* <dd>The request to send to the live data source, if any.</dd>
365
* <dt><code>on</code></dt>
366
* <dd>An object literal with the following properties:
368
* <dt><code>success</code></dt>
369
* <dd>The function to call when the data is ready.</dd>
370
* <dt><code>failure</code></dt>
371
* <dd>The function to call upon a response failure condition.</dd>
372
* <dt><code>argument</code></dt>
373
* <dd>Arbitrary data payload that will be passed back to the success and failure handlers.</dd>
376
* <dt><code>cfg</code></dt>
377
* <dd>Configuration object, if any.</dd>
379
* @return {Number} Transaction ID.
381
sendRequest: function(request) {
382
var tId = DSLocal._tId++,
385
request = request || {};
387
callbacks = request.on || request.callback;
389
this.fire("request", {
391
request: request.request,
394
cfg: request.cfg || {}
402
Y.namespace("DataSource").Local = DSLocal;
405
}, '3.10.3', {"requires": ["base"]});