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