diff -r 000000000000 -r 40c8f766c9b8 src/cm/media/js/lib/yui/yui3.0.0/examples/dataschema/dataschema_parsing.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/cm/media/js/lib/yui/yui3.0.0/examples/dataschema/dataschema_parsing.html Mon Nov 23 15:14:29 2009 +0100 @@ -0,0 +1,248 @@ + + + + + YUI Library Examples: DataSchema Utility: Enforcing DataTypes + + + + + + + + + + + + +
+
+
+

+ + YUI 3.x Home - + +

+
+ + +
+ + + +
+
+
+
+

YUI Library Examples: DataSchema Utility: Enforcing DataTypes

+
+
+ + +
+
+
+
+ +

DataSchema Utility: Enforcing DataTypes

+ +
+
+ DataSchema supports a parser property that enforces type conversion on data as the schema is being applied. The parser property can point to one of the following types of parsing functions: + +
    +
  • A DataType subclass parse function, like Y.DataType.Number.parse
  • +
  • A registered shortcut to a DataType subclass parse function, like "number"
  • +
  • A custom function
  • +
+
+ +
+
+ + + + +
+

Basic example

+
Data
+
+{
+    "results":[
+        {"string":"aardvark", "number":"1", "date":"Jan 1, 2001"},
+        {"string":"bat", "number":"2", "date":"Feb 2, 2002"},
+        {"string":"camel", "number":"3", "date":"March 3, 2003"}
+    ]
+}
+    
+ +
Schema
+
+{
+    resultListLocator: "results",
+    resultFields: [
+        "string", // needs no parsing
+        {key:"number", parser: "number"}, // point to built-in shortcut
+        {key:"date", parser: Y.DataType.Date.parse}] // point to function
+}
+    
+ +
Normalized data
+ +
+ +
+ + + + + + +
+
+
+ +

Use the parser property in your schema's resultFields 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.

+ +
  1. YUI().use("datatype", "dataschema", function(Y) {
  2. var data_in = {
  3. "results":[
  4. {"string":"aardvark", "number":"1", "date":"Jan 1, 2001"},
  5. {"string":"bat", "number":"2", "date":"Feb 2, 2002"},
  6. {"string":"camel", "number":"3", "date":"March 3, 2003"}
  7. ]
  8. },
  9. schema = {
  10. resultListLocator: "results",
  11. resultFields: [
  12. // needs no parsing
  13. "string",
  14. // point parser to built-in function shortcut
  15. {key:"number", parser: "number"},
  16. // point parser to built-in function
  17. {key:"date", parser: Y.DataType.Date.parse}]
  18. },
  19. data_out = Y.DataSchema.JSON.apply(schema, data_in).results;
  20. });
YUI().use("datatype", "dataschema", function(Y) {
+    var data_in = {
+            "results":[
+                {"string":"aardvark", "number":"1", "date":"Jan 1, 2001"},
+                {"string":"bat", "number":"2", "date":"Feb 2, 2002"},
+                {"string":"camel", "number":"3", "date":"March 3, 2003"}
+            ]
+        },
+        schema = {
+            resultListLocator: "results",
+            resultFields: [
+                // needs no parsing
+                "string",
+                // point parser to built-in function shortcut
+                {key:"number", parser: "number"},
+                // point parser to built-in function
+                {key:"date", parser: Y.DataType.Date.parse}]
+        },
+        data_out = Y.DataSchema.JSON.apply(schema, data_in).results;
+});
+ +
+ +
+
+ + + +
+ +
+

Copyright © 2009 Yahoo! Inc. All rights reserved.

+

Privacy Policy - + Terms of Service - + Copyright Policy - + Job Openings

+
+
+ + + + + +