|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Example: Enforcing DataTypes</title> |
|
|
6 |
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic"> |
|
|
7 |
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css"> |
|
|
8 |
<link rel="stylesheet" href="../assets/css/main.css"> |
|
|
9 |
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css"> |
|
|
10 |
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png"> |
|
|
11 |
<script src="../../build/yui/yui-min.js"></script> |
|
|
12 |
|
|
|
13 |
</head> |
|
|
14 |
<body> |
|
|
15 |
<!-- |
|
|
16 |
<a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a> |
|
|
17 |
--> |
|
|
18 |
<div id="doc"> |
|
|
19 |
<div id="hd"> |
|
|
20 |
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1> |
|
|
21 |
</div> |
|
|
22 |
|
|
|
23 |
|
|
|
24 |
<h1>Example: Enforcing DataTypes</h1> |
|
|
25 |
<div class="yui3-g"> |
|
|
26 |
<div class="yui3-u-3-4"> |
|
|
27 |
<div id="main"> |
|
|
28 |
<div class="content"><div class="intro"> |
|
|
29 |
<p>DataSchema supports a <code>parser</code> property that enforces type conversion on data as the schema is being applied. The <code>parser</code> property can point to one of the following types of parsing functions:</p> |
|
|
30 |
|
|
|
31 |
<ul> |
|
|
32 |
<li>A DataType subclass parse function, like <code>Y.DataType.Number.parse</code></li> |
|
|
33 |
<li>A registered shortcut to a DataType subclass parse function, like <code>"number"</code></li> |
|
|
34 |
<li>A custom function</li> |
|
|
35 |
</ul> |
|
|
36 |
</div> |
|
|
37 |
|
|
|
38 |
<div class="example yui3-skin-sam"> |
|
|
39 |
<style scoped> |
|
|
40 |
/* custom styles for this example */ |
|
|
41 |
#demo .output {margin-bottom:1em; padding:10px; border:1px solid #D9D9D9;} |
|
|
42 |
</style> |
|
|
43 |
|
|
|
44 |
<form id="demo"> |
|
|
45 |
<h4>Basic example</h4> |
|
|
46 |
<h6>Data</h6> |
|
|
47 |
<pre> |
|
|
48 |
{ |
|
|
49 |
"results":[ |
|
|
50 |
{"string":"aardvark", "number":"1", "date":"Jan 1, 2001"}, |
|
|
51 |
{"string":"bat", "number":"2", "date":"Feb 2, 2002"}, |
|
|
52 |
{"string":"camel", "number":"3", "date":"March 3, 2003"} |
|
|
53 |
] |
|
|
54 |
} |
|
|
55 |
</pre> |
|
|
56 |
|
|
|
57 |
<h6>Schema</h6> |
|
|
58 |
<pre> |
|
|
59 |
{ |
|
|
60 |
resultListLocator: "results", |
|
|
61 |
resultFields: [ |
|
|
62 |
"string", // needs no parsing |
|
|
63 |
{key:"number", parser: "number"}, // point to built-in shortcut |
|
|
64 |
{key:"date", parser: Y.DataType.Date.parse}] // point to function |
|
|
65 |
} |
|
|
66 |
</pre> |
|
|
67 |
|
|
|
68 |
<h6>Normalized data</h6> |
|
|
69 |
<input type="button" id="demo_apply_parsing" value="Apply schema =>"> |
|
|
70 |
<div id="demo_output_parsing" class="output"></div> |
|
|
71 |
|
|
|
72 |
</form> |
|
|
73 |
|
|
|
74 |
<script> |
|
|
75 |
YUI().use("dump", "node", "datatype", "dataschema", function (Y) { |
|
|
76 |
Y.on("click", function(e){ |
|
|
77 |
var data_in = { |
|
|
78 |
"results":[ |
|
|
79 |
{"string":"aardvark", "number":"1", "date":"Jan 1, 2001"}, |
|
|
80 |
{"string":"bat", "number":"2", "date":"Feb 2, 2002"}, |
|
|
81 |
{"string":"camel", "number":"3", "date":"March 3, 2003"} |
|
|
82 |
] |
|
|
83 |
}, |
|
|
84 |
schema = { |
|
|
85 |
resultListLocator: "results", |
|
|
86 |
resultFields: [ |
|
|
87 |
"string", // needs no parsing |
|
|
88 |
{key:"number", parser: "number"}, // point parser to built-in function shortcut |
|
|
89 |
{key:"date", parser: Y.DataType.Date.parse}] // point parser to built-in function |
|
|
90 |
}, |
|
|
91 |
data_out = Y.DataSchema.JSON.apply(schema, data_in).results, |
|
|
92 |
output = [], |
|
|
93 |
result; |
|
|
94 |
|
|
|
95 |
for(var i=0; i<data_out.length; i++) { |
|
|
96 |
result = data_out[i]; |
|
|
97 |
output.push("<p>string: " + result.string + " [" + Y.Lang.type(result.string) + "]" + |
|
|
98 |
", number: " + result.number + " [" + Y.Lang.type(result.number) + "]" + |
|
|
99 |
", date: " + result.date + " [" + Y.Lang.type(result.date) + "]</p>"); |
|
|
100 |
} |
|
|
101 |
output = output.join(""); |
|
|
102 |
|
|
|
103 |
Y.one("#demo_output_parsing").setHTML(output); |
|
|
104 |
}, "#demo_apply_parsing"); |
|
|
105 |
}); |
|
|
106 |
</script> |
|
|
107 |
|
|
|
108 |
</div> |
|
|
109 |
|
|
|
110 |
<p>Use the <code>parser</code> property in your schema's <code>resultFields</code> definition to point to a parsing function. Parsing your data in this manner is essential if your numerical or date data comes over the wire as JSON, since all the values will be strings.</p> |
|
|
111 |
|
|
|
112 |
<pre class="code prettyprint">YUI().use("datatype", "dataschema", function(Y) { |
|
|
113 |
var data_in = { |
|
|
114 |
"results":[ |
|
|
115 |
{"string":"aardvark", "number":"1", "date":"Jan 1, 2001"}, |
|
|
116 |
{"string":"bat", "number":"2", "date":"Feb 2, 2002"}, |
|
|
117 |
{"string":"camel", "number":"3", "date":"March 3, 2003"} |
|
|
118 |
] |
|
|
119 |
}, |
|
|
120 |
schema = { |
|
|
121 |
resultListLocator: "results", |
|
|
122 |
resultFields: [ |
|
|
123 |
// needs no parsing |
|
|
124 |
"string", |
|
|
125 |
// point parser to built-in function shortcut |
|
|
126 |
{key:"number", parser: "number"}, |
|
|
127 |
// point parser to built-in function |
|
|
128 |
{key:"date", parser: Y.DataType.Date.parse}] |
|
|
129 |
}, |
|
|
130 |
data_out = Y.DataSchema.JSON.apply(schema, data_in).results; |
|
|
131 |
});</pre> |
|
|
132 |
|
|
|
133 |
</div> |
|
|
134 |
</div> |
|
|
135 |
</div> |
|
|
136 |
|
|
|
137 |
<div class="yui3-u-1-4"> |
|
|
138 |
<div class="sidebar"> |
|
|
139 |
|
|
|
140 |
|
|
|
141 |
|
|
|
142 |
<div class="sidebox"> |
|
|
143 |
<div class="hd"> |
|
|
144 |
<h2 class="no-toc">Examples</h2> |
|
|
145 |
</div> |
|
|
146 |
|
|
|
147 |
<div class="bd"> |
|
|
148 |
<ul class="examples"> |
|
|
149 |
|
|
|
150 |
|
|
|
151 |
<li data-description="Schema parsing a JavaScript array."> |
|
|
152 |
<a href="dataschema-array.html">DataSchema.Array</a> |
|
|
153 |
</li> |
|
|
154 |
|
|
|
155 |
|
|
|
156 |
|
|
|
157 |
<li data-description="Schema parsing JSON data."> |
|
|
158 |
<a href="dataschema-json.html">DataSchema.JSON</a> |
|
|
159 |
</li> |
|
|
160 |
|
|
|
161 |
|
|
|
162 |
|
|
|
163 |
<li data-description="Schema parsing XML data."> |
|
|
164 |
<a href="dataschema-xml.html">DataSchema.XML for XML Data</a> |
|
|
165 |
</li> |
|
|
166 |
|
|
|
167 |
|
|
|
168 |
|
|
|
169 |
<li data-description="Schema parsing data held in TABLE elements."> |
|
|
170 |
<a href="dataschema-table.html">DataSchema.XML for HTML Tables</a> |
|
|
171 |
</li> |
|
|
172 |
|
|
|
173 |
|
|
|
174 |
|
|
|
175 |
<li data-description="Schema parsing delimited plain-text data."> |
|
|
176 |
<a href="dataschema-text.html">DataSchema.Text</a> |
|
|
177 |
</li> |
|
|
178 |
|
|
|
179 |
|
|
|
180 |
|
|
|
181 |
<li data-description="Parsing data into specified types as the schema is being applied."> |
|
|
182 |
<a href="dataschema-parsing.html">Enforcing DataTypes</a> |
|
|
183 |
</li> |
|
|
184 |
|
|
|
185 |
|
|
|
186 |
|
|
|
187 |
|
|
|
188 |
|
|
|
189 |
|
|
|
190 |
|
|
|
191 |
|
|
|
192 |
|
|
|
193 |
|
|
|
194 |
|
|
|
195 |
|
|
|
196 |
|
|
|
197 |
|
|
|
198 |
</ul> |
|
|
199 |
</div> |
|
|
200 |
</div> |
|
|
201 |
|
|
|
202 |
|
|
|
203 |
|
|
|
204 |
<div class="sidebox"> |
|
|
205 |
<div class="hd"> |
|
|
206 |
<h2 class="no-toc">Examples That Use This Component</h2> |
|
|
207 |
</div> |
|
|
208 |
|
|
|
209 |
<div class="bd"> |
|
|
210 |
<ul class="examples"> |
|
|
211 |
|
|
|
212 |
|
|
|
213 |
|
|
|
214 |
|
|
|
215 |
|
|
|
216 |
|
|
|
217 |
|
|
|
218 |
|
|
|
219 |
|
|
|
220 |
|
|
|
221 |
|
|
|
222 |
|
|
|
223 |
|
|
|
224 |
|
|
|
225 |
<li data-description="The Local DataSource manages retrieval of in-page data, from JavaScript arrays and objects to DOM elements."> |
|
|
226 |
<a href="../datasource/datasource-local.html">DataSource.Local</a> |
|
|
227 |
</li> |
|
|
228 |
|
|
|
229 |
|
|
|
230 |
|
|
|
231 |
<li data-description="The Get DataSource, which manages retrieval of data from remote sources via the Get Utility, can be useful for accessing data from cross-domain servers without the need for a proxy."> |
|
|
232 |
<a href="../datasource/datasource-get.html">DataSource.Get</a> |
|
|
233 |
</li> |
|
|
234 |
|
|
|
235 |
|
|
|
236 |
|
|
|
237 |
<li data-description="The IO DataSource manages retrieval of data from remote sources, via the IO Utility."> |
|
|
238 |
<a href="../datasource/datasource-io.html">DataSource.IO</a> |
|
|
239 |
</li> |
|
|
240 |
|
|
|
241 |
|
|
|
242 |
|
|
|
243 |
<li data-description="The Function DataSource, which manages retrieval of data from a JavaScript function, provides a highly customizeable mechanism for implementer-defined data retrieval algorithms"> |
|
|
244 |
<a href="../datasource/datasource-function.html">DataSource.Function</a> |
|
|
245 |
</li> |
|
|
246 |
|
|
|
247 |
|
|
|
248 |
|
|
|
249 |
<li data-description="Use the DataSourceCache plugin to enable caching and reduce server calls to remote sources."> |
|
|
250 |
<a href="../datasource/datasource-caching.html">DataSource with Caching</a> |
|
|
251 |
</li> |
|
|
252 |
|
|
|
253 |
|
|
|
254 |
|
|
|
255 |
<li data-description="The DataSourceCache plugin supports offline caching so that cached data persists across browser sessions."> |
|
|
256 |
<a href="../datasource/datasource-offlinecache.html">DataSource with Offline Cache</a> |
|
|
257 |
</li> |
|
|
258 |
|
|
|
259 |
|
|
|
260 |
</ul> |
|
|
261 |
</div> |
|
|
262 |
</div> |
|
|
263 |
|
|
|
264 |
</div> |
|
|
265 |
</div> |
|
|
266 |
</div> |
|
|
267 |
</div> |
|
|
268 |
|
|
|
269 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
270 |
<script>prettyPrint();</script> |
|
|
271 |
|
|
|
272 |
<script> |
|
|
273 |
YUI.Env.Tests = { |
|
|
274 |
examples: [], |
|
|
275 |
project: '../assets', |
|
|
276 |
assets: '../assets/dataschema', |
|
|
277 |
name: 'dataschema-parsing', |
|
|
278 |
title: 'Enforcing DataTypes', |
|
|
279 |
newWindow: '', |
|
|
280 |
auto: false |
|
|
281 |
}; |
|
|
282 |
YUI.Env.Tests.examples.push('dataschema-array'); |
|
|
283 |
YUI.Env.Tests.examples.push('dataschema-json'); |
|
|
284 |
YUI.Env.Tests.examples.push('dataschema-xml'); |
|
|
285 |
YUI.Env.Tests.examples.push('dataschema-table'); |
|
|
286 |
YUI.Env.Tests.examples.push('dataschema-text'); |
|
|
287 |
YUI.Env.Tests.examples.push('dataschema-parsing'); |
|
|
288 |
YUI.Env.Tests.examples.push('datasource-local'); |
|
|
289 |
YUI.Env.Tests.examples.push('datasource-get'); |
|
|
290 |
YUI.Env.Tests.examples.push('datasource-io'); |
|
|
291 |
YUI.Env.Tests.examples.push('datasource-function'); |
|
|
292 |
YUI.Env.Tests.examples.push('datasource-caching'); |
|
|
293 |
YUI.Env.Tests.examples.push('datasource-offlinecache'); |
|
|
294 |
|
|
|
295 |
</script> |
|
|
296 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
297 |
|
|
|
298 |
|
|
|
299 |
|
|
|
300 |
</body> |
|
|
301 |
</html> |