当前位置: 首页 > 工具软件 > Locator-Yui > 使用案例 >

YUI3:DataSchema

邹星火
2023-12-01

DataSchema Utility 应用一个给定的模式 ,以任意格式的数据,标准化输入(例如JSON,XML,或者是分隔的文本)为一个JavaScript对象。DataSchema工具的作用是通过组建,在一种可预测的方式下,将各种各样的数据转换成一个固定的格式以便使用。

使用DataSchema工具

DataSchema basics

DataSchema.Array

DataSchema.JSON

DataSchema.XML

DataSchema.Text

DataSchema 作为一个DataSouce插件

DataSchema basics

 DataSchema classes 是一个独立的静态工具,它接受数据输入,再定义一个输出模式,就可以返回一个带有以下属性的JavaScript对象。(即:最后的返回结果一般为:{results=>[],meta=>{}})

PROPERTYTYPEDESCRIPTION
results数组数据数组
meta对象从输入数据中过滤出来的任意数据值。

你定义的模式依赖于你使用的DataSchema的子类(前面说的Array,JSON,XML,Text)

 

DataSchema.Array

当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"
];

 

而模式的定义用的是下面的属性。

PROPERTYTYPEDESCRIPTION
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)); 

DataSchema.JSON

当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"}
            ]
        }
    ]
};

 

PROPERTYTYPEDESCRIPTION
metaFieldsObjectKey/locator pairs that point to arbitrary data values.
resultListLocatorStringLocator to an internal array of tabular data.
resultFieldsArrayKeys 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));

 

DataSchema.XML

注意,这部分在android 浏览器上会出现问题。

当working with XML数据时使用DataSchema.JSON.

DataSchema.Text

DataSchema 作为一个DataSouce插件

 类似资料:

相关阅读

相关文章

相关问答