1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<div class="intro component">
<p>
The DataSource Utility provides a consistent API for the retrieval of
data from arbitrary sources over a variety of supported protocols.
DataSource plugins and extensions enable additional functionality such
as schema normalization, caching, and polling of data.
</p>
</div>
{{>getting-started}}
<h2 id="using">Using DataSources</h2>
<h3 id="basics">DataSource basics</h3>
<p>
The DataSource Utility uses a callback mechanism to manage the data
retrieval process across a wide variety of potential sources. Define your
callback object with custom functions that will execute when the data
returns from your source. The <code>sendRequest()</code> method accepts an
object literal with properties for the request value, a callback object,
and/or any configuration values for the request.
</p>
```
request: myRequest,
callback: {
success: function(e){
alert(e.response);
},
failure: function(e){
alert(e.error.message);
}
}
});
```
<p>
You must instantiate the appropriate DataSource subclass for your source of
data.
</p>
<h4 id="local">Local sources</h4>
<p>
Use DataSource.Local when you are working with data that is held in local
memory, such as a JavaScript array or object.
</p>
```
var myDataSource = new Y.DataSource.Local({source:["a", "b", "c"]});
```
<h4 id="get">Remote sources with the Get Utility</h4>
<p>
Use DataSource.Get to access data coming from a server via the Get Utility.
The Get Utility supports data retrieval from cross-domain resources without
the need for a proxy, but the server must return JSON data and support a
script callback parameter in order for the response to return properly.
This parameter specifies the name of the internally defined function that
the return data will be wrapped in when it returns to the page.
</p>
```
var myDataSource = new Y.DataSource.Get({
});
```
<p>
You should not modify the internally assigned value of this script callback
parameter. However, you may need to set the parameter name to a different
value so that your server will accept it. By default, the script callback
parameter name is <code>"callback"</code>, but this value can be changed
via the Attribute <code>scriptCallbackParam</code>.
</p>
```
// By default the request is sent to
// "http://query.yahooapis.com/v1/public/yql?format=json&q=foo&callback=YUI.Env.DataSource.callbacks[0]"
request: "q=foo",
callback: myCallback
});
// But the parameter name can be customized to match the server requirement
myDataSource.set("scriptCallbackParam", "cbFunc");
// So now the request is sent to
// "http://query.yahooapis.com/v1/public/yql?format=json&q=foo&cbFunc=YUI.Env.DataSource.callbacks[0]"
request: "q=foo",
callback: myCallback
});
```
<p>
Use the DataSourceJSONSchema plugin to normalize the data that is sent to
your callack.
</p>
```
// Normalize the data sent to myCallback
myDataSource.plug({fn: Y.Plugin.DataSourceJSONSchema, cfg: {
schema: {
resultListLocator: "myResults",
resultFields: ["myField1", "myField2"]
}
}});
```
<h4 id="io">Remote sources with the IO Utility</h4>
<p>
DataSource.IO is used to access data coming from a server via the IO
Utility. Note that accessing a cross-domain server will require a
same-domain proxy or enabling IO's XDR feature, in order to bypass standard
browser security restrictions.
</p>
```
var myDataSource = new Y.DataSource.IO({source:"./myScript.php"});
```
<p>
The IO Utility supports retrieval of multiple data formats, including JSON,
XML, and plain text. Use the appropriate DataSchema plugin to normalize the
data that is sent to your callback.
</p>
```
myDataSource.plug({fn: Y.Plugin.DataSourceXMLSchema, cfg: {
schema: {
resultListLocator: "resultNodeName",
resultFields: [{key:"myField1", locator:"xpath/to/value"}]
}
}});
```
<h4 id="function">Sources using custom functions</h4>
<p>
Defining your own JavaScript function that returns data for a given request
allows full customization of the data retrieval mechanism.
</p>
```
var myDataSource = new Y.DataSource.Function({
source: function (request) {
return data;
}
});
```
<p>
Since your data can return data of any format, you may consider ways to
taking advantage of the built-in infrastructure, including using a
DataSchema plugin to normalize the data that is sent to your callback.
</p>
```
var myDataSource = new Y.DataSource.Function({
source: function (request) {
return [["ann", 123], ["bill", 456]];
}
});
myDataSource.plug({fn: Y.Plugin.DataSourceArraySchema, cfg: {
schema: {
resultFields: ["name","id"]
}
}});
```
<h3 id="caching">Caching</h3>
<p>
The DataSourceCache plugin provides integrated caching functionality to
your DataSource instance. Use the DataSource's <code>plug()</code> method
to instantiate a Cache instance. Set the <code>max</code> Attribute value
to the maximum number of entries the Cache should hold.
</p>
```
myDataSource.plug({fn:Y.Plugin.DataSourceCache, cfg:{max:3}});
```
<p>
Once the plugin is enabled, it will handle caching and retrieval of values
seamlessly for you without the need for extra code. However, all the
methods and properties of the Cache instance is available on the DataSource
instance's <code>cache</code> namepace.
</p>
```
// Flush myDataSource's cache.
// Disable myDataSource's cache
myDataSource.cache.set("max", 0);
```
<h3 id="polling">Polling</h3>
<p>
Pollable is a DataSource extension that enhances the class with polling
functionality. Once the extension is applied, all instances of DataSource
will have available on their prototype the methods that enable and disable
requests sent at regular intervals. To apply the extension, simply include
the <code>datasource-polling</code> sub-module in your
<code>YUI.use()</code> statement.
</p>
```
YUI().use('datasource-io', 'datasource-polling', 'json-parse', function(Y) {
var onlineFriends = Y.one('#friend-count'),
friendData,
intervalId;
friendData = new Y.DataSource.IO({
source: '/services/friends/'
});
// Start polling the server every 10 seconds
intervalId = friendData.setInterval(10000, {
request : Y.one('#user-id').get('value'),
callback: {
success: function (e) {
var friends = Y.JSON.parse(e.response.results[0]).friendCount;
if (!friends) {
friends = 'No friends. You should go outside more.';
}
onlineFriends.set('text', friends);
},
failure: function (e) {
onlineFriends.set('text',
'(Bang) Ouch! ' + e.error.message + ' happened!');
// Stop polling
friendData.clearInterval(intervalId);
}
}
});
});
```
<h3 id="events">Events</h3>
<table>
<thead>
<tr>
<th>Event</th>
<th>When</th>
<th>Event properties</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>request</code></td>
<td>Request is made.</td>
<td>
<dl>
<dt><code>tId</code></dt>
<dd>Unique transaction ID.</dd>
<dt><code>request</code></dt>
<dd>The request value.</dd>
<dt><code>callback</code></dt>
<dd>The callback object.</dd>
<dt><code>cfg</code></dt>
<dd>The configuration object.</dd>
</dl>
</td>
</tr>
<tr>
<td><code>data</code></td>
<td>Raw data is received from the source.</td>
<td>
All properties from `request` plus
<dl>
<dt><code>data</code></dt>
<dd>The raw data.</dd>
</dl>
</td>
</tr>
<tr>
<td><code>response</code></td>
<td>Response is returned to a callback function.</td>
<td>
All properties from `data` plus
<dl>
<dt><code>response</code></dt>
<dd>Data normalized into a response object.</dd>
</dl>
</td>
</tr>
<tr>
<td><code>error</code></td>
<td>After `response` event, before the configured failure callback is executed.</td>
<td>Same properties as the `response` event</td>
</tr>
</tbody>
</table>