]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/yui3/build/datasource/datasource-get.js
Release 6.5.0
[Github/sugarcrm.git] / jssource / src_files / include / javascript / yui3 / build / datasource / datasource-get.js
1 /*
2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
5 version: 3.3.0
6 build: 3167
7 */
8 YUI.add('datasource-get', function(Y) {
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             o;
54
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;
64
65         // Dynamically add handler function with a closure to the callback stack
66         // for access to guid
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];
70
71             var process = this.get('asyncMode') !== "ignoreStaleResponses" ||
72                           this._last === guid;
73
74             if (process) {
75                 this.fire("data", Y.mix({ data: response }, e));
76             } else {
77             }
78
79         }, this);
80
81         // Add the callback param to the request url
82         uri += e.request + generateRequest.call( this, guid );
83
84
85         Y.DataSource.Local.transactions[e.tId] = get.script(uri, {
86             autopurge: true,
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];
91
92                 e.error = new Error(o.msg || "Script node data failure");
93                 this.fire("data", e);
94             }, this, e),
95             onTimeout: Y.bind(function(e, o) {
96                 delete YUI.Env.DataSource.callbacks[guid];
97                 delete Y.DataSource.Local.transactions[e.tId];
98
99                 e.error = new Error(o.msg || "Script node data timeout");
100                 this.fire("data", e);
101             }, this, e)
102         });
103
104         return e.tId;
105     },
106
107
108     /**
109      * Default method for adding callback param to url.  See
110      * generateRequestCallback attribute.
111      *
112      * @method _generateRequest
113      * @param guid {String} unique identifier for callback function wrapper
114      * @protected
115      */
116      _generateRequest: function (guid) {
117         return "&" + this.get("scriptCallbackParam") +
118                 "=YUI.Env.DataSource.callbacks." + guid;
119     }
120
121 }, {
122
123     /**
124      * Class name.
125      *
126      * @property NAME
127      * @type String
128      * @static     
129      * @final
130      * @value "dataSourceGet"
131      */
132     NAME: "dataSourceGet",
133
134
135     ////////////////////////////////////////////////////////////////////////////
136     //
137     // DataSource.Get Attributes
138     //
139     ////////////////////////////////////////////////////////////////////////////
140     ATTRS: {
141         /**
142          * Pointer to Get Utility.
143          *
144          * @attribute get
145          * @type Y.Get
146          * @default Y.Get
147          */
148         get: {
149             value: Y.Get,
150             cloneDefaultValue: false
151         },
152
153         /**
154          * Defines request/response management in the following manner:
155          * <dl>
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>
165          *     <dt>allowAll</dt>
166          *     <dd>Send all requests and handle all responses.</dd>
167          * </dl>
168          *
169          * @attribute asyncMode
170          * @type String
171          * @default "allowAll"
172          */
173         asyncMode: {
174             value: "allowAll"
175         },
176
177         /**
178          * Callback string parameter name sent to the remote script. By default,
179          * requests are sent to
180          * &#60;URI&#62;?&#60;scriptCallbackParam&#62;=callbackFunction
181          *
182          * @attribute scriptCallbackParam
183          * @type String
184          * @default "callback"
185          */
186         scriptCallbackParam : {
187             value: "callback"
188         },
189
190         /**
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.
194          *
195          * @attribute generateRequestCallback
196          * @type Function
197          */
198         generateRequestCallback : {
199             value: function () {
200                 return this._generateRequest.apply(this, arguments);
201             }
202         }
203     }
204 });
205   
206 YUI.namespace("Env.DataSource.callbacks");
207
208
209
210 }, '3.3.0' ,{requires:['datasource-local', 'get']});