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