Upgrade to ExtJS 3.2.0 - Released 03/30/2010
[extjs.git] / src / data / ArrayReader.js
1 /*!
2  * Ext JS Library 3.2.0
3  * Copyright(c) 2006-2010 Ext JS, Inc.
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             success = true,
62             v;
63
64         var root = this.getRoot(o);
65
66         for(var i = 0, len = root.length; i < len; i++) {
67             var n = root[i],
68                 values = {},
69                 id = ((sid || sid === 0) && n[sid] !== undefined && n[sid] !== "" ? n[sid] : null);
70             for(var j = 0, jlen = fields.length; j < jlen; j++) {
71                 var f = fields.items[j],
72                     k = f.mapping !== undefined && f.mapping !== null ? f.mapping : j;
73                 v = n[k] !== undefined ? n[k] : f.defaultValue;
74                 v = f.convert(v, n);
75                 values[f.name] = v;
76             }
77             var record = new recordType(values, id);
78             record.json = n;
79             records[records.length] = record;
80         }
81
82         var totalRecords = records.length;
83
84         if(s.totalProperty) {
85             v = parseInt(this.getTotal(o), 10);
86             if(!isNaN(v)) {
87                 totalRecords = v;
88             }
89         }
90         if(s.successProperty){
91             v = this.getSuccess(o);
92             if(v === false || v === 'false'){
93                 success = false;
94             }
95         }
96
97         return {
98             success : success,
99             records : records,
100             totalRecords : totalRecords
101         };
102     }
103 });