Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
YUI.add('datasource-io', function (Y, NAME) {
2
 
3
/**
4
 * Provides a DataSource implementation which can be used to retrieve data via the IO Utility.
5
 *
6
 * @module datasource
7
 * @submodule datasource-io
8
 */
9
 
10
/**
11
 * IO subclass for the DataSource Utility.
12
 * @class DataSource.IO
13
 * @extends DataSource.Local
14
 * @constructor
15
 */
16
var DSIO = function() {
17
    DSIO.superclass.constructor.apply(this, arguments);
18
};
19
 
20
 
21
    /////////////////////////////////////////////////////////////////////////////
22
    //
23
    // DataSource.IO static properties
24
    //
25
    /////////////////////////////////////////////////////////////////////////////
26
Y.mix(DSIO, {
27
    /**
28
     * Class name.
29
     *
30
     * @property NAME
31
     * @type String
32
     * @static
33
     * @final
34
     * @value "dataSourceIO"
35
     */
36
    NAME: "dataSourceIO",
37
 
38
 
39
    /////////////////////////////////////////////////////////////////////////////
40
    //
41
    // DataSource.IO Attributes
42
    //
43
    /////////////////////////////////////////////////////////////////////////////
44
 
45
    ATTRS: {
46
        /**
47
         * Pointer to IO Utility.
48
         *
49
         * @attribute io
50
         * @type Y.io
51
         * @default Y.io
52
         */
53
        io: {
54
            value: Y.io,
55
            cloneDefaultValue: false
56
        },
57
 
58
        /**
59
         * Default IO Config.
60
         *
61
         * @attribute ioConfig
62
         * @type Object
63
         * @default null
64
         */
65
         ioConfig: {
66
            value: null
67
         }
68
    }
69
});
70
 
71
Y.extend(DSIO, Y.DataSource.Local, {
72
    /**
73
    * Internal init() handler.
74
    *
75
    * @method initializer
76
    * @param config {Object} Config object.
77
    * @private
78
    */
79
    initializer: function(config) {
80
        this._queue = {interval:null, conn:null, requests:[]};
81
    },
82
 
83
    /**
84
    * IO success callback.
85
    *
86
    * @method successHandler
87
    * @param id {String} Transaction ID.
88
    * @param response {String} Response.
89
    * @param e {EventFacade} Event facade.
90
    * @private
91
    */
92
    successHandler: function (id, response, e) {
93
        var defIOConfig = this.get("ioConfig"),
94
            payload = e.details[0];
95
 
96
        delete Y.DataSource.Local.transactions[e.tId];
97
 
98
        payload.data = response;
99
        this.fire("data", payload);
100
 
101
        Y.log("Received IO data response for \"" + e.request + "\"", "info", "datasource-io");
102
 
103
        if (defIOConfig && defIOConfig.on && defIOConfig.on.success) {
104
            defIOConfig.on.success.apply(defIOConfig.context || Y, arguments);
105
        }
106
    },
107
 
108
    /**
109
    * IO failure callback.
110
    *
111
    * @method failureHandler
112
    * @param id {String} Transaction ID.
113
    * @param response {String} Response.
114
    * @param e {EventFacade} Event facade.
115
    * @private
116
    */
117
    failureHandler: function (id, response, e) {
118
        var defIOConfig = this.get("ioConfig"),
119
            payload = e.details[0];
120
 
121
        delete Y.DataSource.Local.transactions[e.tId];
122
 
123
        payload.error = new Error("IO data failure");
124
        Y.log("IO data failure", "error", "datasource-io");
125
 
126
        payload.data = response;
127
        this.fire("data", payload);
128
 
129
        Y.log("Received IO data failure for \"" + e.request + "\"", "info", "datasource-io");
130
 
131
        if (defIOConfig && defIOConfig.on && defIOConfig.on.failure) {
132
            defIOConfig.on.failure.apply(defIOConfig.context || Y, arguments);
133
        }
134
    },
135
 
136
    /**
137
    * @property _queue
138
    * @description Object literal to manage asynchronous request/response
139
    * cycles enabled if queue needs to be managed (asyncMode/ioConnMode):
140
    * <dl>
141
    *     <dt>interval {Number}</dt>
142
    *         <dd>Interval ID of in-progress queue.</dd>
143
    *     <dt>conn</dt>
144
    *         <dd>In-progress connection identifier (if applicable).</dd>
145
    *     <dt>requests {Object[]}</dt>
146
    *         <dd>Array of queued request objects: {request:request, callback:callback}.</dd>
147
    * </dl>
148
    * @type Object
149
    * @default {interval:null, conn:null, requests:[]}
150
    * @private
151
    */
152
    _queue: null,
153
 
154
    /**
155
     * Passes query string to IO. Fires <code>response</code> event when
156
     * response is received asynchronously.
157
     *
158
     * @method _defRequestFn
159
     * @param e {EventFacade} Event Facade with the following properties:
160
     * <dl>
161
     * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
162
     * <dt>request (Object)</dt> <dd>The request.</dd>
163
     * <dt>callback (Object)</dt> <dd>The callback object with the following properties:
164
     *     <dl>
165
     *         <dt>success (Function)</dt> <dd>Success handler.</dd>
166
     *         <dt>failure (Function)</dt> <dd>Failure handler.</dd>
167
     *     </dl>
168
     * </dd>
169
     * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
170
     * </dl>
171
     * @protected
172
     */
173
    _defRequestFn: function(e) {
174
        var uri = this.get("source"),
175
            io = this.get("io"),
176
            defIOConfig = this.get("ioConfig"),
177
            request = e.request,
178
            cfg = Y.merge(defIOConfig, e.cfg, {
179
                on: Y.merge(defIOConfig, {
180
                    success: this.successHandler,
181
                    failure: this.failureHandler
182
                }),
183
                context: this,
184
                "arguments": e
185
            });
186
 
187
        // Support for POST transactions
188
        if(Y.Lang.isString(request)) {
189
            if(cfg.method && (cfg.method.toUpperCase() === "POST")) {
190
                cfg.data = cfg.data ? cfg.data+request : request;
191
            }
192
            else {
193
                uri += request;
194
            }
195
        }
196
        Y.DataSource.Local.transactions[e.tId] = io(uri, cfg);
197
        return e.tId;
198
    }
199
});
200
 
201
Y.DataSource.IO = DSIO;
202
 
203
 
204
}, '3.18.1', {"requires": ["datasource-local", "io-base"]});