|
0
|
1 |
/* |
|
|
2 |
Copyright (c) 2009, Yahoo! Inc. All rights reserved. |
|
|
3 |
Code licensed under the BSD License: |
|
|
4 |
http://developer.yahoo.net/yui/license.txt |
|
|
5 |
version: 3.0.0b1 |
|
|
6 |
build: 1163 |
|
|
7 |
*/ |
|
|
8 |
YUI.add('dataschema-array', function(Y) { |
|
|
9 |
|
|
|
10 |
/** |
|
|
11 |
* Provides a DataSchema implementation which can be used to work with data stored in arrays. |
|
|
12 |
* |
|
|
13 |
* @module dataschema |
|
|
14 |
* @submodule dataschema-array |
|
|
15 |
*/ |
|
|
16 |
|
|
|
17 |
/** |
|
|
18 |
* Array subclass for the DataSchema Utility. |
|
|
19 |
* @class DataSchema.Array |
|
|
20 |
* @extends DataSchema.Base |
|
|
21 |
* @static |
|
|
22 |
*/ |
|
|
23 |
var LANG = Y.Lang, |
|
|
24 |
|
|
|
25 |
SchemaArray = { |
|
|
26 |
|
|
|
27 |
///////////////////////////////////////////////////////////////////////////// |
|
|
28 |
// |
|
|
29 |
// DataSchema.Array static methods |
|
|
30 |
// |
|
|
31 |
///////////////////////////////////////////////////////////////////////////// |
|
|
32 |
/** |
|
|
33 |
* Applies a given schema to given Array data. |
|
|
34 |
* |
|
|
35 |
* @method apply |
|
|
36 |
* @param schema {Object} Schema to apply. |
|
|
37 |
* @param data {Object} Array data. |
|
|
38 |
* @return {Object} Schema-parsed data. |
|
|
39 |
* @static |
|
|
40 |
*/ |
|
|
41 |
apply: function(schema, data) { |
|
|
42 |
var data_in = data, |
|
|
43 |
data_out = {results:[],meta:{}}; |
|
|
44 |
|
|
|
45 |
if(LANG.isArray(data_in)) { |
|
|
46 |
if(LANG.isArray(schema.resultFields)) { |
|
|
47 |
// Parse results data |
|
|
48 |
data_out = SchemaArray._parseResults(schema.resultFields, data_in, data_out); |
|
|
49 |
} |
|
|
50 |
else { |
|
|
51 |
data_out.results = data_in; |
|
|
52 |
} |
|
|
53 |
} |
|
|
54 |
else { |
|
|
55 |
data_out.error = new Error("Array schema parse failure"); |
|
|
56 |
} |
|
|
57 |
|
|
|
58 |
return data_out; |
|
|
59 |
}, |
|
|
60 |
|
|
|
61 |
/** |
|
|
62 |
* Schema-parsed list of results from full data |
|
|
63 |
* |
|
|
64 |
* @method _parseResults |
|
|
65 |
* @param fields {Array} Schema to parse against. |
|
|
66 |
* @param array_in {Array} Array to parse. |
|
|
67 |
* @param data_out {Object} In-progress parsed data to update. |
|
|
68 |
* @return {Object} Parsed data object. |
|
|
69 |
* @static |
|
|
70 |
* @protected |
|
|
71 |
*/ |
|
|
72 |
_parseResults: function(fields, array_in, data_out) { |
|
|
73 |
var results = [], |
|
|
74 |
result, item, type, field, key, value, i, j; |
|
|
75 |
|
|
|
76 |
for(i=array_in.length-1; i>-1; i--) { |
|
|
77 |
result = {}; |
|
|
78 |
item = array_in[i]; |
|
|
79 |
type = (LANG.isObject(item) && !LANG.isFunction(item)) ? 2 : (LANG.isArray(item)) ? 1 : (LANG.isString(item)) ? 0 : -1; |
|
|
80 |
if(type > 0) { |
|
|
81 |
for(j=fields.length-1; j>-1; j--) { |
|
|
82 |
field = fields[j]; |
|
|
83 |
key = (!LANG.isUndefined(field.key)) ? field.key : field; |
|
|
84 |
value = (!LANG.isUndefined(item[key])) ? item[key] : item[j]; |
|
|
85 |
result[key] = Y.DataSchema.Base.parse(value, field); |
|
|
86 |
} |
|
|
87 |
} |
|
|
88 |
else if(type === 0) { |
|
|
89 |
result = item; |
|
|
90 |
} |
|
|
91 |
else { |
|
|
92 |
//TODO: null or {}? |
|
|
93 |
result = null; |
|
|
94 |
} |
|
|
95 |
results[i] = result; |
|
|
96 |
} |
|
|
97 |
data_out.results = results; |
|
|
98 |
|
|
|
99 |
return data_out; |
|
|
100 |
} |
|
|
101 |
}; |
|
|
102 |
|
|
|
103 |
Y.DataSchema.Array = Y.mix(SchemaArray, Y.DataSchema.Base); |
|
|
104 |
|
|
|
105 |
|
|
|
106 |
|
|
|
107 |
}, '3.0.0b1' ,{requires:['dataschema-base']}); |