~ubuntu-branches/ubuntu/utopic/moodle/utopic

« back to all changes in this revision

Viewing changes to lib/yuilib/3.13.0/datasource-get/datasource-get.js

  • Committer: Package Import Robot
  • Author(s): Thijs Kinkhorst
  • Date: 2014-05-12 16:10:38 UTC
  • mfrom: (36.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20140512161038-puyqf65k4e0s8ytz
Tags: 2.6.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

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