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