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