Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

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