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/
8 YUI.add('datasource-get', function (Y, NAME) {
11 * Provides a DataSource implementation which can be used to retrieve data via the Get Utility.
14 * @submodule datasource-get
18 * Get Utility subclass for the DataSource Utility.
19 * @class DataSource.Get
20 * @extends DataSource.Local
23 var DSGet = function() {
24 DSGet.superclass.constructor.apply(this, arguments);
28 Y.DataSource.Get = Y.extend(DSGet, Y.DataSource.Local, {
30 * Passes query string to Get Utility. Fires <code>response</code> event when
31 * response is received asynchronously.
33 * @method _defRequestFn
34 * @param e {Event.Facade} Event Facade with the following properties:
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:
40 * <dt>success (Function)</dt> <dd>Success handler.</dd>
41 * <dt>failure (Function)</dt> <dd>Failure handler.</dd>
44 * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
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],
57 * Stores the most recent request id for validation against stale
66 // Dynamically add handler function with a closure to the callback stack
68 YUI.Env.DataSource.callbacks[guid] = function(response) {
69 delete YUI.Env.DataSource.callbacks[guid];
70 delete Y.DataSource.Local.transactions[e.tId];
72 var process = self.get('asyncMode') !== "ignoreStaleResponses" ||
76 payload.data = response;
78 self.fire("data", payload);
84 // Add the callback param to the request url
85 uri += e.request + generateRequest.call( this, guid );
88 Y.DataSource.Local.transactions[e.tId] = get.script(uri, {
90 // Works in Firefox only....
91 onFailure: function (o) {
92 delete YUI.Env.DataSource.callbacks[guid];
93 delete Y.DataSource.Local.transactions[e.tId];
95 payload.error = new Error(o.msg || "Script node data failure");
98 self.fire("data", payload);
100 onTimeout: function(o) {
101 delete YUI.Env.DataSource.callbacks[guid];
102 delete Y.DataSource.Local.transactions[e.tId];
104 payload.error = new Error(o.msg || "Script node data timeout");
107 self.fire("data", payload);
116 * Default method for adding callback param to url. See
117 * generateRequestCallback attribute.
119 * @method _generateRequest
120 * @param guid {String} unique identifier for callback function wrapper
123 _generateRequest: function (guid) {
124 return "&" + this.get("scriptCallbackParam") +
125 "=YUI.Env.DataSource.callbacks." + guid;
137 * @value "dataSourceGet"
139 NAME: "dataSourceGet",
142 ////////////////////////////////////////////////////////////////////////////
144 // DataSource.Get Attributes
146 ////////////////////////////////////////////////////////////////////////////
149 * Pointer to Get Utility.
157 cloneDefaultValue: false
161 * Defines request/response management in the following manner:
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>
173 * <dd>Send all requests and handle all responses.</dd>
176 * @attribute asyncMode
178 * @default "allowAll"
185 * Callback string parameter name sent to the remote script. By default,
186 * requests are sent to
187 * <URI>?<scriptCallbackParam>=callbackFunction
189 * @attribute scriptCallbackParam
191 * @default "callback"
193 scriptCallbackParam : {
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.
202 * @attribute generateRequestCallback
205 generateRequestCallback : {
207 return this._generateRequest.apply(this, arguments);
213 YUI.namespace("Env.DataSource.callbacks");
216 }, '3.13.0', {"requires": ["datasource-local", "get"]});