|
1 /* |
|
2 YUI 3.10.3 (build 2fb5187) |
|
3 Copyright 2013 Yahoo! Inc. All rights reserved. |
|
4 Licensed under the BSD License. |
|
5 http://yuilibrary.com/license/ |
|
6 */ |
|
7 |
|
8 YUI.add('datasource-get', function (Y, NAME) { |
|
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 payload = e.details[0], |
|
54 self = this; |
|
55 |
|
56 /** |
|
57 * Stores the most recent request id for validation against stale |
|
58 * response handling. |
|
59 * |
|
60 * @property _last |
|
61 * @type {String} |
|
62 * @protected |
|
63 */ |
|
64 this._last = guid; |
|
65 |
|
66 // Dynamically add handler function with a closure to the callback stack |
|
67 // for access to guid |
|
68 YUI.Env.DataSource.callbacks[guid] = function(response) { |
|
69 delete YUI.Env.DataSource.callbacks[guid]; |
|
70 delete Y.DataSource.Local.transactions[e.tId]; |
|
71 |
|
72 var process = self.get('asyncMode') !== "ignoreStaleResponses" || |
|
73 self._last === guid; |
|
74 |
|
75 if (process) { |
|
76 payload.data = response; |
|
77 |
|
78 self.fire("data", payload); |
|
79 } else { |
|
80 Y.log("DataSource ignored stale response for id " + e.tId + "(" + e.request + ")", "info", "datasource-get"); |
|
81 } |
|
82 |
|
83 }; |
|
84 |
|
85 // Add the callback param to the request url |
|
86 uri += e.request + generateRequest.call( this, guid ); |
|
87 |
|
88 Y.log("DataSource is querying URL " + uri, "info", "datasource-get"); |
|
89 |
|
90 Y.DataSource.Local.transactions[e.tId] = get.script(uri, { |
|
91 autopurge: true, |
|
92 // Works in Firefox only.... |
|
93 onFailure: function (o) { |
|
94 delete YUI.Env.DataSource.callbacks[guid]; |
|
95 delete Y.DataSource.Local.transactions[e.tId]; |
|
96 |
|
97 payload.error = new Error(o.msg || "Script node data failure"); |
|
98 |
|
99 Y.log("Script node data failure", "error", "datasource-get"); |
|
100 |
|
101 self.fire("data", payload); |
|
102 }, |
|
103 onTimeout: function(o) { |
|
104 delete YUI.Env.DataSource.callbacks[guid]; |
|
105 delete Y.DataSource.Local.transactions[e.tId]; |
|
106 |
|
107 payload.error = new Error(o.msg || "Script node data timeout"); |
|
108 |
|
109 Y.log("Script node data timeout", "error", "datasource-get"); |
|
110 |
|
111 self.fire("data", payload); |
|
112 } |
|
113 }); |
|
114 |
|
115 return e.tId; |
|
116 }, |
|
117 |
|
118 |
|
119 /** |
|
120 * Default method for adding callback param to url. See |
|
121 * generateRequestCallback attribute. |
|
122 * |
|
123 * @method _generateRequest |
|
124 * @param guid {String} unique identifier for callback function wrapper |
|
125 * @protected |
|
126 */ |
|
127 _generateRequest: function (guid) { |
|
128 return "&" + this.get("scriptCallbackParam") + |
|
129 "=YUI.Env.DataSource.callbacks." + guid; |
|
130 } |
|
131 |
|
132 }, { |
|
133 |
|
134 /** |
|
135 * Class name. |
|
136 * |
|
137 * @property NAME |
|
138 * @type String |
|
139 * @static |
|
140 * @final |
|
141 * @value "dataSourceGet" |
|
142 */ |
|
143 NAME: "dataSourceGet", |
|
144 |
|
145 |
|
146 //////////////////////////////////////////////////////////////////////////// |
|
147 // |
|
148 // DataSource.Get Attributes |
|
149 // |
|
150 //////////////////////////////////////////////////////////////////////////// |
|
151 ATTRS: { |
|
152 /** |
|
153 * Pointer to Get Utility. |
|
154 * |
|
155 * @attribute get |
|
156 * @type Y.Get |
|
157 * @default Y.Get |
|
158 */ |
|
159 get: { |
|
160 value: Y.Get, |
|
161 cloneDefaultValue: false |
|
162 }, |
|
163 |
|
164 /** |
|
165 * Defines request/response management in the following manner: |
|
166 * <dl> |
|
167 * <!--<dt>queueRequests</dt> |
|
168 * <dd>If a request is already in progress, wait until response is |
|
169 * returned before sending the next request.</dd> |
|
170 * <dt>cancelStaleRequests</dt> |
|
171 * <dd>If a request is already in progress, cancel it before |
|
172 * sending the next request.</dd>--> |
|
173 * <dt>ignoreStaleResponses</dt> |
|
174 * <dd>Send all requests, but handle only the response for the most |
|
175 * recently sent request.</dd> |
|
176 * <dt>allowAll</dt> |
|
177 * <dd>Send all requests and handle all responses.</dd> |
|
178 * </dl> |
|
179 * |
|
180 * @attribute asyncMode |
|
181 * @type String |
|
182 * @default "allowAll" |
|
183 */ |
|
184 asyncMode: { |
|
185 value: "allowAll" |
|
186 }, |
|
187 |
|
188 /** |
|
189 * Callback string parameter name sent to the remote script. By default, |
|
190 * requests are sent to |
|
191 * <URI>?<scriptCallbackParam>=callbackFunction |
|
192 * |
|
193 * @attribute scriptCallbackParam |
|
194 * @type String |
|
195 * @default "callback" |
|
196 */ |
|
197 scriptCallbackParam : { |
|
198 value: "callback" |
|
199 }, |
|
200 |
|
201 /** |
|
202 * Accepts the DataSource instance and a callback ID, and returns a callback |
|
203 * param/value string that gets appended to the script URI. Implementers |
|
204 * can customize this string to match their server's query syntax. |
|
205 * |
|
206 * @attribute generateRequestCallback |
|
207 * @type Function |
|
208 */ |
|
209 generateRequestCallback : { |
|
210 value: function () { |
|
211 return this._generateRequest.apply(this, arguments); |
|
212 } |
|
213 } |
|
214 } |
|
215 }); |
|
216 |
|
217 YUI.namespace("Env.DataSource.callbacks"); |
|
218 |
|
219 |
|
220 }, '3.10.3', {"requires": ["datasource-local", "get"]}); |