2 Copyright (c) 2009, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.net/yui/license.txt
8 YUI.add('datasource-get', function(Y) {
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 /////////////////////////////////////////////////////////////////////////////
30 // DataSource.Get static properties
32 /////////////////////////////////////////////////////////////////////////////
41 * @value "dataSourceGet"
43 NAME: "dataSourceGet",
46 /////////////////////////////////////////////////////////////////////////////
48 // DataSource.Get Attributes
50 /////////////////////////////////////////////////////////////////////////////
54 * Pointer to Get Utility.
62 cloneDefaultValue: false
66 * Defines request/response management in the following manner:
68 * <!--<dt>queueRequests</dt>
69 * <dd>If a request is already in progress, wait until response is returned before sending the next request.</dd>
70 * <dt>cancelStaleRequests</dt>
71 * <dd>If a request is already in progress, cancel it before sending the next request.</dd>-->
72 * <dt>ignoreStaleResponses</dt>
73 * <dd>Send all requests, but handle only the response for the most recently sent request.</dd>
75 * <dd>Send all requests and handle all responses.</dd>
78 * @attribute asyncMode
87 * Callback string parameter name sent to the remote script. By default,
88 * requests are sent to
89 * <URI>?<scriptCallbackParam>=callbackFunction
91 * @attribute scriptCallbackParam
95 scriptCallbackParam : {
100 * Accepts the DataSource instance and a callback ID, and returns a callback
101 * param/value string that gets appended to the script URI. Implementers
102 * can customize this string to match their server's query syntax.
104 * @attribute generateRequestCallback
107 generateRequestCallback : {
108 value: function(self, id) {
109 return "&" + self.get("scriptCallbackParam") + "=YUI.Env.DataSource.callbacks["+id+"]" ;
120 * Global array of callback functions, one for each request sent.
122 * @property callbacks
129 * Unique ID to track requests.
139 Y.extend(DSGet, Y.DataSource.Local, {
141 * Passes query string to Get Utility. Fires <code>response</code> event when
142 * response is received asynchronously.
144 * @method _defRequestFn
145 * @param e {Event.Facade} Event Facade with the following properties:
147 * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
148 * <dt>request (Object)</dt> <dd>The request.</dd>
149 * <dt>callback (Object)</dt> <dd>The callback object with the following properties:
151 * <dt>success (Function)</dt> <dd>Success handler.</dd>
152 * <dt>failure (Function)</dt> <dd>Failure handler.</dd>
155 * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
159 _defRequestFn: function(e) {
160 var uri = this.get("source"),
161 get = this.get("get"),
177 // Dynamically add handler function with a closure to the callback stack
178 YUI.Env.DataSource.callbacks[id] = Y.rbind(function(response) {
179 if((self.get("asyncMode") !== "ignoreStaleResponses")||
180 (id === DSGet.callbacks.length-1)) { // Must ignore stale responses
182 self.fire("data", Y.mix({data:response}, e));
187 delete DSGet.callbacks[id];
190 // We are now creating a request
191 uri += e.request + this.get("generateRequestCallback")(this, id);
192 //uri = this.doBefore(sUri);
195 // Works in Firefox only....
196 onFailure: Y.bind(function(e) {
197 e.error = new Error("Script node data failure");
198 this.fire("error", e);
220 Y.DataSource.Get = DSGet;
221 YUI.namespace("Env.DataSource.callbacks");
226 }, '3.0.0' ,{requires:['datasource-local', 'get']});