|
525
|
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-io', function (Y, NAME) { |
|
|
9 |
|
|
|
10 |
/** |
|
|
11 |
* Provides a DataSource implementation which can be used to retrieve data via the IO Utility. |
|
|
12 |
* |
|
|
13 |
* @module datasource |
|
|
14 |
* @submodule datasource-io |
|
|
15 |
*/ |
|
|
16 |
|
|
|
17 |
/** |
|
|
18 |
* IO subclass for the DataSource Utility. |
|
|
19 |
* @class DataSource.IO |
|
|
20 |
* @extends DataSource.Local |
|
|
21 |
* @constructor |
|
|
22 |
*/ |
|
|
23 |
var DSIO = function() { |
|
|
24 |
DSIO.superclass.constructor.apply(this, arguments); |
|
|
25 |
}; |
|
|
26 |
|
|
|
27 |
|
|
|
28 |
///////////////////////////////////////////////////////////////////////////// |
|
|
29 |
// |
|
|
30 |
// DataSource.IO static properties |
|
|
31 |
// |
|
|
32 |
///////////////////////////////////////////////////////////////////////////// |
|
|
33 |
Y.mix(DSIO, { |
|
|
34 |
/** |
|
|
35 |
* Class name. |
|
|
36 |
* |
|
|
37 |
* @property NAME |
|
|
38 |
* @type String |
|
|
39 |
* @static |
|
|
40 |
* @final |
|
|
41 |
* @value "dataSourceIO" |
|
|
42 |
*/ |
|
|
43 |
NAME: "dataSourceIO", |
|
|
44 |
|
|
|
45 |
|
|
|
46 |
///////////////////////////////////////////////////////////////////////////// |
|
|
47 |
// |
|
|
48 |
// DataSource.IO Attributes |
|
|
49 |
// |
|
|
50 |
///////////////////////////////////////////////////////////////////////////// |
|
|
51 |
|
|
|
52 |
ATTRS: { |
|
|
53 |
/** |
|
|
54 |
* Pointer to IO Utility. |
|
|
55 |
* |
|
|
56 |
* @attribute io |
|
|
57 |
* @type Y.io |
|
|
58 |
* @default Y.io |
|
|
59 |
*/ |
|
|
60 |
io: { |
|
|
61 |
value: Y.io, |
|
|
62 |
cloneDefaultValue: false |
|
|
63 |
}, |
|
|
64 |
|
|
|
65 |
/** |
|
|
66 |
* Default IO Config. |
|
|
67 |
* |
|
|
68 |
* @attribute ioConfig |
|
|
69 |
* @type Object |
|
|
70 |
* @default null |
|
|
71 |
*/ |
|
|
72 |
ioConfig: { |
|
|
73 |
value: null |
|
|
74 |
} |
|
|
75 |
} |
|
|
76 |
}); |
|
|
77 |
|
|
|
78 |
Y.extend(DSIO, Y.DataSource.Local, { |
|
|
79 |
/** |
|
|
80 |
* Internal init() handler. |
|
|
81 |
* |
|
|
82 |
* @method initializer |
|
|
83 |
* @param config {Object} Config object. |
|
|
84 |
* @private |
|
|
85 |
*/ |
|
|
86 |
initializer: function(config) { |
|
|
87 |
this._queue = {interval:null, conn:null, requests:[]}; |
|
|
88 |
}, |
|
|
89 |
|
|
|
90 |
/** |
|
|
91 |
* IO success callback. |
|
|
92 |
* |
|
|
93 |
* @method successHandler |
|
|
94 |
* @param id {String} Transaction ID. |
|
|
95 |
* @param response {String} Response. |
|
|
96 |
* @param e {Event.Facade} Event facade. |
|
|
97 |
* @private |
|
|
98 |
*/ |
|
|
99 |
successHandler: function (id, response, e) { |
|
|
100 |
var defIOConfig = this.get("ioConfig"), |
|
|
101 |
payload = e.details[0]; |
|
|
102 |
|
|
|
103 |
delete Y.DataSource.Local.transactions[e.tId]; |
|
|
104 |
|
|
|
105 |
payload.data = response; |
|
|
106 |
this.fire("data", payload); |
|
|
107 |
|
|
|
108 |
|
|
|
109 |
if (defIOConfig && defIOConfig.on && defIOConfig.on.success) { |
|
|
110 |
defIOConfig.on.success.apply(defIOConfig.context || Y, arguments); |
|
|
111 |
} |
|
|
112 |
}, |
|
|
113 |
|
|
|
114 |
/** |
|
|
115 |
* IO failure callback. |
|
|
116 |
* |
|
|
117 |
* @method failureHandler |
|
|
118 |
* @param id {String} Transaction ID. |
|
|
119 |
* @param response {String} Response. |
|
|
120 |
* @param e {Event.Facade} Event facade. |
|
|
121 |
* @private |
|
|
122 |
*/ |
|
|
123 |
failureHandler: function (id, response, e) { |
|
|
124 |
var defIOConfig = this.get("ioConfig"), |
|
|
125 |
payload = e.details[0]; |
|
|
126 |
|
|
|
127 |
delete Y.DataSource.Local.transactions[e.tId]; |
|
|
128 |
|
|
|
129 |
payload.error = new Error("IO data failure"); |
|
|
130 |
|
|
|
131 |
payload.data = response; |
|
|
132 |
this.fire("data", payload); |
|
|
133 |
|
|
|
134 |
|
|
|
135 |
if (defIOConfig && defIOConfig.on && defIOConfig.on.failure) { |
|
|
136 |
defIOConfig.on.failure.apply(defIOConfig.context || Y, arguments); |
|
|
137 |
} |
|
|
138 |
}, |
|
|
139 |
|
|
|
140 |
/** |
|
|
141 |
* @property _queue |
|
|
142 |
* @description Object literal to manage asynchronous request/response |
|
|
143 |
* cycles enabled if queue needs to be managed (asyncMode/ioConnMode): |
|
|
144 |
* <dl> |
|
|
145 |
* <dt>interval {Number}</dt> |
|
|
146 |
* <dd>Interval ID of in-progress queue.</dd> |
|
|
147 |
* <dt>conn</dt> |
|
|
148 |
* <dd>In-progress connection identifier (if applicable).</dd> |
|
|
149 |
* <dt>requests {Object[]}</dt> |
|
|
150 |
* <dd>Array of queued request objects: {request:request, callback:callback}.</dd> |
|
|
151 |
* </dl> |
|
|
152 |
* @type Object |
|
|
153 |
* @default {interval:null, conn:null, requests:[]} |
|
|
154 |
* @private |
|
|
155 |
*/ |
|
|
156 |
_queue: null, |
|
|
157 |
|
|
|
158 |
/** |
|
|
159 |
* Passes query string to IO. Fires <code>response</code> event when |
|
|
160 |
* response is received asynchronously. |
|
|
161 |
* |
|
|
162 |
* @method _defRequestFn |
|
|
163 |
* @param e {Event.Facade} Event Facade with the following properties: |
|
|
164 |
* <dl> |
|
|
165 |
* <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd> |
|
|
166 |
* <dt>request (Object)</dt> <dd>The request.</dd> |
|
|
167 |
* <dt>callback (Object)</dt> <dd>The callback object with the following properties: |
|
|
168 |
* <dl> |
|
|
169 |
* <dt>success (Function)</dt> <dd>Success handler.</dd> |
|
|
170 |
* <dt>failure (Function)</dt> <dd>Failure handler.</dd> |
|
|
171 |
* </dl> |
|
|
172 |
* </dd> |
|
|
173 |
* <dt>cfg (Object)</dt> <dd>Configuration object.</dd> |
|
|
174 |
* </dl> |
|
|
175 |
* @protected |
|
|
176 |
*/ |
|
|
177 |
_defRequestFn: function(e) { |
|
|
178 |
var uri = this.get("source"), |
|
|
179 |
io = this.get("io"), |
|
|
180 |
defIOConfig = this.get("ioConfig"), |
|
|
181 |
request = e.request, |
|
|
182 |
cfg = Y.merge(defIOConfig, e.cfg, { |
|
|
183 |
on: Y.merge(defIOConfig, { |
|
|
184 |
success: this.successHandler, |
|
|
185 |
failure: this.failureHandler |
|
|
186 |
}), |
|
|
187 |
context: this, |
|
|
188 |
"arguments": e |
|
|
189 |
}); |
|
|
190 |
|
|
|
191 |
// Support for POST transactions |
|
|
192 |
if(Y.Lang.isString(request)) { |
|
|
193 |
if(cfg.method && (cfg.method.toUpperCase() === "POST")) { |
|
|
194 |
cfg.data = cfg.data ? cfg.data+request : request; |
|
|
195 |
} |
|
|
196 |
else { |
|
|
197 |
uri += request; |
|
|
198 |
} |
|
|
199 |
} |
|
|
200 |
Y.DataSource.Local.transactions[e.tId] = io(uri, cfg); |
|
|
201 |
return e.tId; |
|
|
202 |
} |
|
|
203 |
}); |
|
|
204 |
|
|
|
205 |
Y.DataSource.IO = DSIO; |
|
|
206 |
|
|
|
207 |
|
|
|
208 |
}, '3.10.3', {"requires": ["datasource-local", "io-base"]}); |