Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
YUI.add('datasource-local', function (Y, NAME) {
2
 
3
/**
4
 * The DataSource utility provides a common configurable interface for widgets to
5
 * access a variety of data, from JavaScript arrays to online database servers.
6
 *
7
 * @module datasource
8
 * @main datasource
9
 */
10
 
11
/**
12
 * Provides the base DataSource implementation, which can be extended to
13
 * create DataSources for specific data protocols, such as the IO Utility, the
14
 * Get Utility, or custom functions.
15
 *
16
 * @module datasource
17
 * @submodule datasource-local
18
 */
19
 
20
/**
21
 * Base class for the DataSource Utility.
22
 * @class DataSource.Local
23
 * @extends Base
24
 * @constructor
25
 */
26
var LANG = Y.Lang,
27
 
28
DSLocal = function() {
29
    DSLocal.superclass.constructor.apply(this, arguments);
30
};
31
 
32
    /////////////////////////////////////////////////////////////////////////////
33
    //
34
    // DataSource static properties
35
    //
36
    /////////////////////////////////////////////////////////////////////////////
37
Y.mix(DSLocal, {
38
    /**
39
     * Class name.
40
     *
41
     * @property NAME
42
     * @type String
43
     * @static
44
     * @final
45
     * @value "dataSourceLocal"
46
     */
47
    NAME: "dataSourceLocal",
48
 
49
    /////////////////////////////////////////////////////////////////////////////
50
    //
51
    // DataSource Attributes
52
    //
53
    /////////////////////////////////////////////////////////////////////////////
54
 
55
    ATTRS: {
56
        /**
57
        * @attribute source
58
        * @description Pointer to live data.
59
        * @type MIXED
60
        * @default null
61
        */
62
        source: {
63
            value: null
64
        }
65
    },
66
 
67
    /**
68
     * Global transaction counter.
69
     *
70
     * @property _tId
71
     * @type Number
72
     * @static
73
     * @private
74
     * @default 0
75
     */
76
    _tId: 0,
77
 
78
    /**
79
     * Global in-progress transaction objects.
80
     *
81
     * @property transactions
82
     * @type Object
83
     * @static
84
     */
85
    transactions: {},
86
 
87
    /**
88
     * Returns data to callback.
89
     *
90
     * @method issueCallback
91
     * @param e {EventFacade} Event Facade.
92
     * @param caller {DataSource} Calling DataSource instance.
93
     * @static
94
     */
95
    issueCallback: function (e, caller) {
96
        var callbacks = e.on || e.callback,
97
            callback = callbacks && callbacks.success,
98
            payload = e.details[0];
99
 
100
        payload.error = (e.error || e.response.error);
101
 
102
        if (payload.error) {
103
            caller.fire("error", payload);
104
            callback = callbacks && callbacks.failure;
105
        }
106
 
107
        if (callback) {
108
            //TODO: this should be executed from a specific context
109
            callback(payload);
110
        }
111
    }
112
});
113
 
114
Y.extend(DSLocal, Y.Base, {
115
    /**
116
    * Internal init() handler.
117
    *
118
    * @method initializer
119
    * @param config {Object} Config object.
120
    * @private
121
    */
122
    initializer: function(config) {
123
        this._initEvents();
124
    },
125
 
126
    /**
127
    * This method creates all the events for this module.
128
    * @method _initEvents
129
    * @private
130
    */
131
    _initEvents: function() {
132
        /**
133
         * Fired when a data request is received.
134
         *
135
         * @event request
136
         * @param e {EventFacade} Event Facade with the following properties:
137
         * <dl>
138
         * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
139
         * <dt>request (Object)</dt> <dd>The request.</dd>
140
         * <dt>callback (Object)</dt> <dd>The callback object
141
         *   (deprecated, refer to <strong>on</strong></dd>
142
         * <dt>on (Object)</dt> <dd>The map of configured callback
143
         *   functions.</dd>
144
         * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
145
         * </dl>
146
         * @preventable _defRequestFn
147
         */
148
        this.publish("request", {defaultFn: Y.bind("_defRequestFn", this), queuable:true});
149
 
150
        /**
151
         * Fired when raw data is received.
152
         *
153
         * @event data
154
         * @param e {EventFacade} Event Facade with the following properties:
155
         * <dl>
156
         * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
157
         * <dt>request (Object)</dt> <dd>The request.</dd>
158
         * <dt>callback (Object)</dt> <dd>Deprecated alias for the
159
         *   <strong>on</strong> property</dd>
160
         * <dt>on (Object)</dt> <dd>The map of configured transaction
161
         *   callbacks.  An object with the following properties:
162
         *     <dl>
163
         *         <dt>success (Function)</dt> <dd>Success handler.</dd>
164
         *         <dt>failure (Function)</dt> <dd>Failure handler.</dd>
165
         *     </dl>
166
         * </dd>
167
         * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
168
         * <dt>data (Object)</dt> <dd>Raw data.</dd>
169
         * </dl>
170
         * @preventable _defDataFn
171
         */
172
        this.publish("data", {defaultFn: Y.bind("_defDataFn", this), queuable:true});
173
 
174
        /**
175
         * Fired when response is returned.
176
         *
177
         * @event response
178
         * @param e {EventFacade} Event Facade with the following properties:
179
         * <dl>
180
         * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
181
         * <dt>request (Object)</dt> <dd>The request.</dd>
182
         * <dt>callback (Object)</dt> <dd>Deprecated alias for the
183
         *   <strong>on</strong> property</dd>
184
         * <dt>on (Object)</dt> <dd>The map of configured transaction
185
         *   callbacks.  An object with the following properties:
186
         *     <dl>
187
         *         <dt>success (Function)</dt> <dd>Success handler.</dd>
188
         *         <dt>failure (Function)</dt> <dd>Failure handler.</dd>
189
         *     </dl>
190
         * </dd>
191
         * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
192
         * <dt>data (Object)</dt> <dd>Raw data.</dd>
193
         * <dt>response (Object)</dt>
194
         *     <dd>Normalized response object with the following properties:
195
         *         <dl>
196
         *             <dt>results (Object)</dt> <dd>Parsed results.</dd>
197
         *             <dt>meta (Object)</dt> <dd>Parsed meta data.</dd>
198
         *             <dt>error (Boolean)</dt> <dd>Error flag.</dd>
199
         *         </dl>
200
         *     </dd>
201
         * <dt>error</dt>
202
         *     <dd>Any error that occurred along the transaction lifecycle.</dd>
203
         * </dl>
204
         * @preventable _defResponseFn
205
         */
206
         this.publish("response", {defaultFn: Y.bind("_defResponseFn", this), queuable:true});
207
 
208
        /**
209
         * Fired when an error is encountered.
210
         *
211
         * @event error
212
         * @param e {EventFacade} Event Facade with the following properties:
213
         * <dl>
214
         * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
215
         * <dt>request (Object)</dt> <dd>The request.</dd>
216
         * <dt>callback (Object)</dt> <dd>Deprecated alias for the
217
         *   <strong>on</strong> property</dd>
218
         * <dt>on (Object)</dt> <dd>The map of configured transaction
219
         *   callbacks.  An object with the following properties:
220
         *     <dl>
221
         *         <dt>success (Function)</dt> <dd>Success handler.</dd>
222
         *         <dt>failure (Function)</dt> <dd>Failure handler.</dd>
223
         *     </dl>
224
         * </dd>
225
         * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
226
         * <dt>data (Object)</dt> <dd>Raw data.</dd>
227
         * <dt>response (Object)</dt>
228
         *     <dd>Normalized response object with the following properties:
229
         *         <dl>
230
         *             <dt>results (Object)</dt> <dd>Parsed results.</dd>
231
         *             <dt>meta (Object)</dt> <dd>Parsed meta data.</dd>
232
         *             <dt>error (Object)</dt> <dd>Error object.</dd>
233
         *         </dl>
234
         *     </dd>
235
         * <dt>error</dt>
236
         *     <dd>Any error that occurred along the transaction lifecycle.</dd>
237
         * </dl>
238
         */
239
 
240
    },
241
 
242
    /**
243
     * Manages request/response transaction. Must fire <code>response</code>
244
     * event when response is received. This method should be implemented by
245
     * subclasses to achieve more complex behavior such as accessing remote data.
246
     *
247
     * @method _defRequestFn
248
     * @param e {EventFacade} Event Facadewith the following properties:
249
     * <dl>
250
     * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
251
     * <dt>request (Object)</dt> <dd>The request.</dd>
252
     * <dt>callback (Object)</dt> <dd>Deprecated alias for the
253
     *   <strong>on</strong> property</dd>
254
     * <dt>on (Object)</dt> <dd>The map of configured transaction
255
     *   callbacks.  An object with the following properties:
256
     *     <dl>
257
     *         <dt>success (Function)</dt> <dd>Success handler.</dd>
258
     *         <dt>failure (Function)</dt> <dd>Failure handler.</dd>
259
     *     </dl>
260
     * </dd>
261
     * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
262
     * </dl>
263
     * @protected
264
     */
265
    _defRequestFn: function(e) {
266
        var data = this.get("source"),
267
            payload = e.details[0];
268
 
269
        // Problematic data
270
        if(LANG.isUndefined(data)) {
271
            payload.error = new Error("Local source undefined");
272
        }
273
 
274
        payload.data = data;
275
        this.fire("data", payload);
276
    },
277
 
278
    /**
279
     * Normalizes raw data into a response that includes results and meta properties.
280
     *
281
     * @method _defDataFn
282
     * @param e {EventFacade} Event Facade with the following properties:
283
     * <dl>
284
     * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
285
     * <dt>request (Object)</dt> <dd>The request.</dd>
286
     * <dt>callback (Object)</dt> <dd>Deprecated alias for the
287
     *   <strong>on</strong> property</dd>
288
     * <dt>on (Object)</dt> <dd>The map of configured transaction
289
     *   callbacks.  An object with the following properties:
290
     *     <dl>
291
     *         <dt>success (Function)</dt> <dd>Success handler.</dd>
292
     *         <dt>failure (Function)</dt> <dd>Failure handler.</dd>
293
     *     </dl>
294
     * </dd>
295
     * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
296
     * <dt>data (Object)</dt> <dd>Raw data.</dd>
297
     * </dl>
298
     * @protected
299
     */
300
    _defDataFn: function(e) {
301
        var data = e.data,
302
            meta = e.meta,
303
            response = {
304
                results: (LANG.isArray(data)) ? data : [data],
305
                meta: (meta) ? meta : {}
306
            },
307
            payload = e.details[0];
308
 
309
        payload.response = response;
310
        this.fire("response", payload);
311
    },
312
 
313
    /**
314
     * Sends data as a normalized response to callback.
315
     *
316
     * @method _defResponseFn
317
     * @param e {EventFacade} Event Facade with the following properties:
318
     * <dl>
319
     * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
320
     * <dt>request (Object)</dt> <dd>The request.</dd>
321
     * <dt>callback (Object)</dt> <dd>Deprecated alias for the
322
     *   <strong>on</strong> property</dd>
323
     * <dt>on (Object)</dt> <dd>The map of configured transaction
324
     *   callbacks.  An object with the following properties:
325
     *     <dl>
326
     *         <dt>success (Function)</dt> <dd>Success handler.</dd>
327
     *         <dt>failure (Function)</dt> <dd>Failure handler.</dd>
328
     *     </dl>
329
     * </dd>
330
     * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
331
     * <dt>data (Object)</dt> <dd>Raw data.</dd>
332
     * <dt>response (Object)</dt> <dd>Normalized response object with the following properties:
333
     *     <dl>
334
     *         <dt>results (Object)</dt> <dd>Parsed results.</dd>
335
     *         <dt>meta (Object)</dt> <dd>Parsed meta data.</dd>
336
     *         <dt>error (Boolean)</dt> <dd>Error flag.</dd>
337
     *     </dl>
338
     * </dd>
339
     * </dl>
340
     * @protected
341
     */
342
    _defResponseFn: function(e) {
343
        // Send the response back to the callback
344
        DSLocal.issueCallback(e, this);
345
    },
346
 
347
    /**
348
     * Generates a unique transaction ID and fires <code>request</code> event.
349
     * <strong>Note</strong>: the property <code>callback</code> is a
350
     * deprecated alias for the <code>on</code> transaction configuration
351
     * property described below.
352
     *
353
     * @method sendRequest
354
     * @param [request] {Object} An object literal with the following properties:
355
     *     <dl>
356
     *     <dt><code>request</code></dt>
357
     *     <dd>The request to send to the live data source, if any.</dd>
358
     *     <dt><code>on</code></dt>
359
     *     <dd>An object literal with the following properties:
360
     *         <dl>
361
     *         <dt><code>success</code></dt>
362
     *         <dd>The function to call when the data is ready.</dd>
363
     *         <dt><code>failure</code></dt>
364
     *         <dd>The function to call upon a response failure condition.</dd>
365
     *         <dt><code>argument</code></dt>
366
     *         <dd>Arbitrary data payload that will be passed back to the success and failure handlers.</dd>
367
     *         </dl>
368
     *     </dd>
369
     *     <dt><code>cfg</code></dt>
370
     *     <dd>Configuration object, if any.</dd>
371
     *     </dl>
372
     * @return {Number} Transaction ID.
373
     */
374
    sendRequest: function(request) {
375
        var tId = DSLocal._tId++,
376
            callbacks;
377
 
378
        request = request || {};
379
 
380
        callbacks = request.on || request.callback;
381
 
382
        this.fire("request", {
383
            tId: tId,
384
            request: request.request,
385
            on: callbacks,
386
            callback: callbacks,
387
            cfg: request.cfg || {}
388
        });
389
 
390
 
391
        return tId;
392
    }
393
});
394
 
395
Y.namespace("DataSource").Local = DSLocal;
396
 
397
 
398
}, '3.18.1', {"requires": ["base"]});