/** * @author Ed Spencer * @class Ext.data.ArrayReader * @extends Ext.data.JsonReader * *Data reader class to create an Array of {@link 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({ {@link #idIndex}: 0 }, Employee);
This would consume an Array like this:
** * @constructor * Create a new ArrayReader * @param {Object} meta Metadata configuration options. * @param {Array/Object} recordType *[ [1, 'Bill', 'Gardener'], [2, 'Ben', 'Horticulturalist'] ]
Either an Array of {@link Ext.data.Field Field} definition objects (which * will be passed to {@link Ext.data.Record#create}, or a {@link Ext.data.Record Record} * constructor created from {@link Ext.data.Record#create}.
*/ Ext.data.ArrayReader = Ext.extend(Ext.data.JsonReader, { /** * @private * Most of the work is done for us by JsonReader, but we need to overwrite the field accessors to just * reference the correct position in the array. */ buildExtractors: function() { Ext.data.ArrayReader.superclass.buildExtractors.apply(this, arguments); var fields = this.model.prototype.fields.items, length = fields.length, extractorFunctions = [], i; for (i = 0; i < length; i++) { extractorFunctions.push(function(index) { return function(data) { return data[index]; }; }(fields[i].mapping || i)); } this.extractorFunctions = extractorFunctions; } }); Ext.data.ReaderMgr.registerType('array', Ext.data.ArrayReader);