Upgrade to ExtJS 3.1.0 - Released 12/16/2009
[extjs.git] / src / data / ArrayReader.js
1 /*!
2  * Ext JS Library 3.1.0
3  * Copyright(c) 2006-2009 Ext JS, LLC
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**
8  * @class Ext.data.ArrayReader
9  * @extends Ext.data.JsonReader
10  * <p>Data reader class to create an Array of {@link Ext.data.Record} objects from an Array.
11  * Each element of that Array represents a row of data fields. The
12  * fields are pulled into a Record object using as a subscript, the <code>mapping</code> property
13  * of the field definition if it exists, or the field's ordinal position in the definition.</p>
14  * <p>Example code:</p>
15  * <pre><code>
16 var Employee = Ext.data.Record.create([
17     {name: 'name', mapping: 1},         // "mapping" only needed if an "id" field is present which
18     {name: 'occupation', mapping: 2}    // precludes using the ordinal position as the index.
19 ]);
20 var myReader = new Ext.data.ArrayReader({
21     {@link #idIndex}: 0
22 }, Employee);
23 </code></pre>
24  * <p>This would consume an Array like this:</p>
25  * <pre><code>
26 [ [1, 'Bill', 'Gardener'], [2, 'Ben', 'Horticulturalist'] ]
27  * </code></pre>
28  * @constructor
29  * Create a new ArrayReader
30  * @param {Object} meta Metadata configuration options.
31  * @param {Array/Object} recordType
32  * <p>Either an Array of {@link Ext.data.Field Field} definition objects (which
33  * will be passed to {@link Ext.data.Record#create}, or a {@link Ext.data.Record Record}
34  * constructor created from {@link Ext.data.Record#create}.</p>
35  */
36 Ext.data.ArrayReader = Ext.extend(Ext.data.JsonReader, {
37     /**
38      * @cfg {String} successProperty
39      * @hide
40      */
41     /**
42      * @cfg {Number} id (optional) The subscript within row Array that provides an ID for the Record.
43      * Deprecated. Use {@link #idIndex} instead.
44      */
45     /**
46      * @cfg {Number} idIndex (optional) The subscript within row Array that provides an ID for the Record.
47      */
48     /**
49      * Create a data block containing Ext.data.Records from an Array.
50      * @param {Object} o An Array of row objects which represents the dataset.
51      * @return {Object} data A data block which is used by an Ext.data.Store object as
52      * a cache of Ext.data.Records.
53      */
54     readRecords : function(o){
55         this.arrayData = o;
56         var s = this.meta,
57             sid = s ? Ext.num(s.idIndex, s.id) : null,
58             recordType = this.recordType,
59             fields = recordType.prototype.fields,
60             records = [],
61             v;
62
63         var root = this.getRoot(o);
64
65         for(var i = 0, len = root.length; i < len; i++) {
66             var n = root[i],
67                 values = {},
68                 id = ((sid || sid === 0) && n[sid] !== undefined && n[sid] !== "" ? n[sid] : null);
69             for(var j = 0, jlen = fields.length; j < jlen; j++) {
70                 var f = fields.items[j],
71                     k = f.mapping !== undefined && f.mapping !== null ? f.mapping : j;
72                 v = n[k] !== undefined ? n[k] : f.defaultValue;
73                 v = f.convert(v, n);
74                 values[f.name] = v;
75             }
76             var record = new recordType(values, id);
77             record.json = n;
78             records[records.length] = record;
79         }
80
81         var totalRecords = records.length;
82
83         if(s.totalProperty) {
84             v = parseInt(this.getTotal(o), 10);
85             if(!isNaN(v)) {
86                 totalRecords = v;
87             }
88         }
89
90         return {
91             records : records,
92             totalRecords : totalRecords
93         };
94     }
95 });