DataSchema Utility 应用一个给定的模式 ,以任意格式的数据,标准化输入(例如JSON,XML,或者是分隔的文本)为一个JavaScript对象。DataSchema工具的作用是通过组建,在一种可预测的方式下,将各种各样的数据转换成一个固定的格式以便使用。
DataSchema classes 是一个独立的静态工具,它接受数据输入,再定义一个输出模式,就可以返回一个带有以下属性的JavaScript对象。(即:最后的返回结果一般为:{results=>[],meta=>{}})
PROPERTY | TYPE | DESCRIPTION |
---|---|---|
results | 数组 | 数据数组 |
meta | 对象 | 从输入数据中过滤出来的任意数据值。 |
你定义的模式依赖于你使用的DataSchema的子类(前面说的Array,JSON,XML,Text)
当working with JavaScript数组时,使用DataSchema
三种情况:JavaScript对象,数组,primitive values(基本值?)
// A sample array of objects [ {make:"Chevrolet",model:"Bel Air",year:1957}, {make:"Dodge",model:"Dart",year:1964}, {make:"Ford",model:"Mustang",year:1968} ]; // A sample array of arrays [ ["Chevrolet", "Bel Air", 1957], ["Dodge", "Dart", 1964], ["Ford", "Mustang", 1968] ]; // A sample array of primitives [ "1957 Chevrolet Bel Air", "1964 Dodge Dart", "1968 Ford Mustang" ];
而模式的定义用的是下面的属性。
PROPERTY | TYPE | DESCRIPTION |
---|---|---|
resultsField | 数组 | 包含在数组中,制定到特定值的keys |
var mySchema = { resultFields: [{key:"make"}, {key:"model"}, {key:"year"}] }; // Returns an object with the properties "results" and "meta" var myOutput = Y.DataSchema.Array.apply(mySchema, myData));
当working with JavaScript object 或者JSON data时使用DataSchema.JSON.Typically, your data will hold meta values as well as an internal array of tabular data.(这句话如何理解??)
// Sample JSON data { "profile":{ "current":160, "target":150 }, "program": [ { "category":"exercise", "weekly schedule":[ {"day":"sunday", "activity":"swimming"}, {"day":"monday", "activity":"running"}, {"day":"tuesday", "activity":"biking"}, {"day":"wednesday", "activity":"running"}, {"day":"thursday", "activity":"swimming"}, {"day":"friday", "activity":"running"}, {"day":"saturday", "activity":"golf"} ] } ] };
PROPERTY | TYPE | DESCRIPTION |
---|---|---|
metaFields | Object | Key/locator pairs that point to arbitrary data values. |
resultListLocator | String | Locator to an internal array of tabular data. |
resultFields | Array | Keys to assign to the values contained in the array. |
var mySchema = { metaFields: {current:"profile.current", target:"profile.target"}, resultListLocator: "program[0]['weekly schedule']", resultFields: [{key:"day"}, {key:"activity"}] }; // Returns an object with the properties "results" and "meta" var myOutput = Y.DataSchema.JSON.apply(mySchema, myData));
注意,这部分在android 浏览器上会出现问题。
当working with XML数据时使用DataSchema.JSON.