/*
* Ext JS Library 2.0 Copyright(c) 2006-2007, Ext JS, LLC. licensing@extjs.com
*
* http://extjs.com/license
*/
/**
* @class Ext.data.ArrayReader
* @extends Ext.data.DataReader Data reader class to create an Array of
* Ext.data.Record objects from an Array. Each element of that Array
* represents a row of data fields. The fields are pulled into a Record
* object using as a subscript, the mapping property of the
* field definition if it exists, or the field's ordinal position in
* the definition.
*
* Example code:. * *
* var Employee = Ext.data.Record.create([{
* name : 'name',
* mapping : 1
* }, // "mapping" only needed if an "id" field is present which
* {
* name : 'occupation',
* mapping : 2
* } // precludes using the ordinal position as the index.
* ]);
* var myReader = new Ext.data.ArrayReader({
* id : 0
* // The subscript within row Array that provides an ID for the Record (optional)
* }, Employee);
*
*
* * This would consume an Array like this: * *
* [[1, 'Bill', 'Gardener'], [2, 'Ben', 'Horticulturalist']]
*
*
* @cfg {String} id (optional) The subscript within row Array that provides an
* ID for the Record
* @constructor Create a new ArrayReader
* @param {Object}
* meta Metadata configuration options.
* @param {Object}
* recordType Either an Array of field definition objects as
* specified to {@link Ext.data.Record#create}, or a
* {@link Ext.data.Record Record} constructor created using
* {@link Ext.data.Record#create}.
*/
Ext.data.ArrayReader = Ext.extend(Ext.data.JsonReader, {
/**
* Create a data block containing Ext.data.Records from an XML
* document.
*
* @param {Object}
* o An Array of row objects which represents the
* dataset.
* @return {Object} data A data block which is used by an
* Ext.data.Store object as a cache of Ext.data.Records.
*/
readRecords : function(o) {
var sid = this.meta ? this.meta.id : null;
var recordType = this.recordType, fields = recordType.prototype.fields;
var records = [];
var root = o;
for (var i = 0; i < root.length; i++) {
var n = root[i];
var values = {};
var id = ((sid || sid === 0) && n[sid] !== undefined
&& n[sid] !== "" ? n[sid] : null);
for (var j = 0, jlen = fields.length; j < jlen; j++) {
var f = fields.items[j];
var k = f.mapping !== undefined && f.mapping !== null
? f.mapping
: j;
var v = n[k] !== undefined ? n[k] : f.defaultValue;
v = f.convert(v);
values[f.name] = v;
}
var record = new recordType(values, id);
record.json = n;
records[records.length] = record;
}
return {
records : records,
totalRecords : records.length
};
}
});