4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
13 * @class Roo.data.ArrayReader
14 * @extends Roo.data.DataReader
15 * Data reader class to create an Array of Roo.data.Record objects from an Array.
16 * Each element of that Array represents a row of data fields. The
17 * fields are pulled into a Record object using as a subscript, the <em>mapping</em> property
18 * of the field definition if it exists, or the field's ordinal position in the definition.<br>
22 var RecordDef = Roo.data.Record.create([
23 {name: 'name', mapping: 1}, // "mapping" only needed if an "id" field is present which
24 {name: 'occupation', mapping: 2} // precludes using the ordinal position as the index.
26 var myReader = new Roo.data.ArrayReader({
27 id: 0 // The subscript within row Array that provides an ID for the Record (optional)
31 * This would consume an Array like this:
33 [ [1, 'Bill', 'Gardener'], [2, 'Ben', 'Horticulturalist'] ]
37 * Create a new JsonReader
38 * @param {Object} meta Metadata configuration options.
39 * @param {Object|Array} recordType Either an Array of field definition objects
41 * @cfg {Array} fields Array of field definition objects
42 * @cfg {String} id Name of the property within a row object that contains a record identifier value.
43 * as specified to {@link Roo.data.Record#create},
44 * or an {@link Roo.data.Record} object
47 * created using {@link Roo.data.Record#create}.
49 Roo.data.ArrayReader = function(meta, recordType)
51 Roo.data.ArrayReader.superclass.constructor.call(this, meta, recordType||meta.fields);
54 Roo.extend(Roo.data.ArrayReader, Roo.data.JsonReader, {
57 * Create a data block containing Roo.data.Records from an XML document.
58 * @param {Object} o An Array of row objects which represents the dataset.
59 * @return {Object} A data block which is used by an {@link Roo.data.Store} object as
60 * a cache of Roo.data.Records.
62 readRecords : function(o)
64 var sid = this.meta ? this.meta.id : null;
65 var recordType = this.recordType, fields = recordType.prototype.fields;
68 for(var i = 0; i < root.length; i++){
71 var id = ((sid || sid === 0) && n[sid] !== undefined && n[sid] !== "" ? n[sid] : null);
72 for(var j = 0, jlen = fields.length; j < jlen; j++){
73 var f = fields.items[j];
74 var k = f.mapping !== undefined && f.mapping !== null ? f.mapping : j;
75 var v = n[k] !== undefined ? n[k] : f.defaultValue;
79 var record = new recordType(values, id);
81 records[records.length] = record;
85 totalRecords : records.length
88 // used when loading children.. @see loadDataFromChildren
89 toLoadData: function(rec)
91 // expect rec just to be an array.. eg [a,b,c, [...] << cn ]
92 return typeof(rec.data.cn) == 'undefined' ? [] : rec.data.cn;