1 |
efrain |
1 |
YUI.add('datasource-get', function (Y, NAME) {
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* Provides a DataSource implementation which can be used to retrieve data via the Get Utility.
|
|
|
5 |
*
|
|
|
6 |
* @module datasource
|
|
|
7 |
* @submodule datasource-get
|
|
|
8 |
*/
|
|
|
9 |
|
|
|
10 |
/**
|
|
|
11 |
* Get Utility subclass for the DataSource Utility.
|
|
|
12 |
* @class DataSource.Get
|
|
|
13 |
* @extends DataSource.Local
|
|
|
14 |
* @constructor
|
|
|
15 |
*/
|
|
|
16 |
var DSGet = function() {
|
|
|
17 |
DSGet.superclass.constructor.apply(this, arguments);
|
|
|
18 |
};
|
|
|
19 |
|
|
|
20 |
|
|
|
21 |
Y.DataSource.Get = Y.extend(DSGet, Y.DataSource.Local, {
|
|
|
22 |
/**
|
|
|
23 |
* Passes query string to Get Utility. Fires <code>response</code> event when
|
|
|
24 |
* response is received asynchronously.
|
|
|
25 |
*
|
|
|
26 |
* @method _defRequestFn
|
|
|
27 |
* @param e {EventFacade} Event Facade with the following properties:
|
|
|
28 |
* <dl>
|
|
|
29 |
* <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
|
|
|
30 |
* <dt>request (Object)</dt> <dd>The request.</dd>
|
|
|
31 |
* <dt>callback (Object)</dt> <dd>The callback object with the following properties:
|
|
|
32 |
* <dl>
|
|
|
33 |
* <dt>success (Function)</dt> <dd>Success handler.</dd>
|
|
|
34 |
* <dt>failure (Function)</dt> <dd>Failure handler.</dd>
|
|
|
35 |
* </dl>
|
|
|
36 |
* </dd>
|
|
|
37 |
* <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
|
|
|
38 |
* </dl>
|
|
|
39 |
* @protected
|
|
|
40 |
*/
|
|
|
41 |
_defRequestFn: function(e) {
|
|
|
42 |
var uri = this.get("source"),
|
|
|
43 |
get = this.get("get"),
|
|
|
44 |
guid = Y.guid().replace(/\-/g, '_'),
|
|
|
45 |
generateRequest = this.get( "generateRequestCallback" ),
|
|
|
46 |
payload = e.details[0],
|
|
|
47 |
self = this;
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* Stores the most recent request id for validation against stale
|
|
|
51 |
* response handling.
|
|
|
52 |
*
|
|
|
53 |
* @property _last
|
|
|
54 |
* @type {String}
|
|
|
55 |
* @protected
|
|
|
56 |
*/
|
|
|
57 |
this._last = guid;
|
|
|
58 |
|
|
|
59 |
// Dynamically add handler function with a closure to the callback stack
|
|
|
60 |
// for access to guid
|
|
|
61 |
YUI.Env.DataSource.callbacks[guid] = function(response) {
|
|
|
62 |
delete YUI.Env.DataSource.callbacks[guid];
|
|
|
63 |
delete Y.DataSource.Local.transactions[e.tId];
|
|
|
64 |
|
|
|
65 |
var process = self.get('asyncMode') !== "ignoreStaleResponses" ||
|
|
|
66 |
self._last === guid;
|
|
|
67 |
|
|
|
68 |
if (process) {
|
|
|
69 |
payload.data = response;
|
|
|
70 |
|
|
|
71 |
self.fire("data", payload);
|
|
|
72 |
} else {
|
|
|
73 |
Y.log("DataSource ignored stale response for id " + e.tId + "(" + e.request + ")", "info", "datasource-get");
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
};
|
|
|
77 |
|
|
|
78 |
// Add the callback param to the request url
|
|
|
79 |
uri += e.request + generateRequest.call( this, guid );
|
|
|
80 |
|
|
|
81 |
Y.log("DataSource is querying URL " + uri, "info", "datasource-get");
|
|
|
82 |
|
|
|
83 |
Y.DataSource.Local.transactions[e.tId] = get.script(uri, {
|
|
|
84 |
autopurge: true,
|
|
|
85 |
// Works in Firefox only....
|
|
|
86 |
onFailure: function (o) {
|
|
|
87 |
delete YUI.Env.DataSource.callbacks[guid];
|
|
|
88 |
delete Y.DataSource.Local.transactions[e.tId];
|
|
|
89 |
|
|
|
90 |
payload.error = new Error(o.msg || "Script node data failure");
|
|
|
91 |
|
|
|
92 |
Y.log("Script node data failure", "error", "datasource-get");
|
|
|
93 |
|
|
|
94 |
self.fire("data", payload);
|
|
|
95 |
},
|
|
|
96 |
onTimeout: function(o) {
|
|
|
97 |
delete YUI.Env.DataSource.callbacks[guid];
|
|
|
98 |
delete Y.DataSource.Local.transactions[e.tId];
|
|
|
99 |
|
|
|
100 |
payload.error = new Error(o.msg || "Script node data timeout");
|
|
|
101 |
|
|
|
102 |
Y.log("Script node data timeout", "error", "datasource-get");
|
|
|
103 |
|
|
|
104 |
self.fire("data", payload);
|
|
|
105 |
}
|
|
|
106 |
});
|
|
|
107 |
|
|
|
108 |
return e.tId;
|
|
|
109 |
},
|
|
|
110 |
|
|
|
111 |
|
|
|
112 |
/**
|
|
|
113 |
* Default method for adding callback param to url. See
|
|
|
114 |
* generateRequestCallback attribute.
|
|
|
115 |
*
|
|
|
116 |
* @method _generateRequest
|
|
|
117 |
* @param guid {String} unique identifier for callback function wrapper
|
|
|
118 |
* @protected
|
|
|
119 |
*/
|
|
|
120 |
_generateRequest: function (guid) {
|
|
|
121 |
return "&" + this.get("scriptCallbackParam") +
|
|
|
122 |
"=YUI.Env.DataSource.callbacks." + guid;
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
}, {
|
|
|
126 |
|
|
|
127 |
/**
|
|
|
128 |
* Class name.
|
|
|
129 |
*
|
|
|
130 |
* @property NAME
|
|
|
131 |
* @type String
|
|
|
132 |
* @static
|
|
|
133 |
* @final
|
|
|
134 |
* @value "dataSourceGet"
|
|
|
135 |
*/
|
|
|
136 |
NAME: "dataSourceGet",
|
|
|
137 |
|
|
|
138 |
|
|
|
139 |
////////////////////////////////////////////////////////////////////////////
|
|
|
140 |
//
|
|
|
141 |
// DataSource.Get Attributes
|
|
|
142 |
//
|
|
|
143 |
////////////////////////////////////////////////////////////////////////////
|
|
|
144 |
ATTRS: {
|
|
|
145 |
/**
|
|
|
146 |
* Pointer to Get Utility.
|
|
|
147 |
*
|
|
|
148 |
* @attribute get
|
|
|
149 |
* @type Y.Get
|
|
|
150 |
* @default Y.Get
|
|
|
151 |
*/
|
|
|
152 |
get: {
|
|
|
153 |
value: Y.Get,
|
|
|
154 |
cloneDefaultValue: false
|
|
|
155 |
},
|
|
|
156 |
|
|
|
157 |
/**
|
|
|
158 |
* Defines request/response management in the following manner:
|
|
|
159 |
* <dl>
|
|
|
160 |
* <!--<dt>queueRequests</dt>
|
|
|
161 |
* <dd>If a request is already in progress, wait until response is
|
|
|
162 |
* returned before sending the next request.</dd>
|
|
|
163 |
* <dt>cancelStaleRequests</dt>
|
|
|
164 |
* <dd>If a request is already in progress, cancel it before
|
|
|
165 |
* sending the next request.</dd>-->
|
|
|
166 |
* <dt>ignoreStaleResponses</dt>
|
|
|
167 |
* <dd>Send all requests, but handle only the response for the most
|
|
|
168 |
* recently sent request.</dd>
|
|
|
169 |
* <dt>allowAll</dt>
|
|
|
170 |
* <dd>Send all requests and handle all responses.</dd>
|
|
|
171 |
* </dl>
|
|
|
172 |
*
|
|
|
173 |
* @attribute asyncMode
|
|
|
174 |
* @type String
|
|
|
175 |
* @default "allowAll"
|
|
|
176 |
*/
|
|
|
177 |
asyncMode: {
|
|
|
178 |
value: "allowAll"
|
|
|
179 |
},
|
|
|
180 |
|
|
|
181 |
/**
|
|
|
182 |
* Callback string parameter name sent to the remote script. By default,
|
|
|
183 |
* requests are sent to
|
|
|
184 |
* <URI>?<scriptCallbackParam>=callbackFunction
|
|
|
185 |
*
|
|
|
186 |
* @attribute scriptCallbackParam
|
|
|
187 |
* @type String
|
|
|
188 |
* @default "callback"
|
|
|
189 |
*/
|
|
|
190 |
scriptCallbackParam : {
|
|
|
191 |
value: "callback"
|
|
|
192 |
},
|
|
|
193 |
|
|
|
194 |
/**
|
|
|
195 |
* Accepts the DataSource instance and a callback ID, and returns a callback
|
|
|
196 |
* param/value string that gets appended to the script URI. Implementers
|
|
|
197 |
* can customize this string to match their server's query syntax.
|
|
|
198 |
*
|
|
|
199 |
* @attribute generateRequestCallback
|
|
|
200 |
* @type Function
|
|
|
201 |
*/
|
|
|
202 |
generateRequestCallback : {
|
|
|
203 |
value: function () {
|
|
|
204 |
return this._generateRequest.apply(this, arguments);
|
|
|
205 |
}
|
|
|
206 |
}
|
|
|
207 |
}
|
|
|
208 |
});
|
|
|
209 |
|
|
|
210 |
YUI.namespace("Env.DataSource.callbacks");
|
|
|
211 |
|
|
|
212 |
|
|
|
213 |
}, '3.18.1', {"requires": ["datasource-local", "get"]});
|