2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
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 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" ),
56 * Stores the most recent request id for validation against stale
65 // Dynamically add handler function with a closure to the callback stack
67 YUI.Env.DataSource.callbacks[guid] = Y.bind(function(response) {
68 delete YUI.Env.DataSource.callbacks[guid];
69 delete Y.DataSource.Local.transactions[e.tId];
71 var process = this.get('asyncMode') !== "ignoreStaleResponses" ||
75 this.fire("data", Y.mix({ data: response }, e));
81 // Add the callback param to the request url
82 uri += e.request + generateRequest.call( this, guid );
85 Y.DataSource.Local.transactions[e.tId] = get.script(uri, {
87 // Works in Firefox only....
88 onFailure: Y.bind(function(e, o) {
89 delete YUI.Env.DataSource.callbacks[guid];
90 delete Y.DataSource.Local.transactions[e.tId];
92 e.error = new Error(o.msg || "Script node data failure");
95 onTimeout: Y.bind(function(e, o) {
96 delete YUI.Env.DataSource.callbacks[guid];
97 delete Y.DataSource.Local.transactions[e.tId];
99 e.error = new Error(o.msg || "Script node data timeout");
100 this.fire("data", e);
109 * Default method for adding callback param to url. See
110 * generateRequestCallback attribute.
112 * @method _generateRequest
113 * @param guid {String} unique identifier for callback function wrapper
116 _generateRequest: function (guid) {
117 return "&" + this.get("scriptCallbackParam") +
118 "=YUI.Env.DataSource.callbacks." + guid;
130 * @value "dataSourceGet"
132 NAME: "dataSourceGet",
135 ////////////////////////////////////////////////////////////////////////////
137 // DataSource.Get Attributes
139 ////////////////////////////////////////////////////////////////////////////
142 * Pointer to Get Utility.
150 cloneDefaultValue: false
154 * Defines request/response management in the following manner:
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>
166 * <dd>Send all requests and handle all responses.</dd>
169 * @attribute asyncMode
171 * @default "allowAll"
178 * Callback string parameter name sent to the remote script. By default,
179 * requests are sent to
180 * <URI>?<scriptCallbackParam>=callbackFunction
182 * @attribute scriptCallbackParam
184 * @default "callback"
186 scriptCallbackParam : {
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.
195 * @attribute generateRequestCallback
198 generateRequestCallback : {
200 return this._generateRequest.apply(this, arguments);
206 YUI.namespace("Env.DataSource.callbacks");
210 }, '3.3.0' ,{requires:['datasource-local', 'get']});