initial import
[roojs1] / Roo / data / ArrayReader.js
1 /*
2  * Based on:
3  * Ext JS Library 1.1.1
4  * Copyright(c) 2006-2007, Ext JS, LLC.
5  *
6  * Originally Released Under LGPL - original licence link has changed is not relivant.
7  *
8  * Fork - LGPL
9  * <script type="text/javascript">
10  */
11
12 /**
13  * @class Roo.data.ArrayReader
14  * @extends Roo.data.DataReader
15  * Data reader class to create an Array of Roo.data.Record objects from an Array.
16  * Each element of that Array represents a row of data fields. The
17  * fields are pulled into a Record object using as a subscript, the <em>mapping</em> property
18  * of the field definition if it exists, or the field's ordinal position in the definition.<br>
19  * <p>
20  * Example code:.
21  * <pre><code>
22 var RecordDef = Roo.data.Record.create([
23     {name: 'name', mapping: 1},         // "mapping" only needed if an "id" field is present which
24     {name: 'occupation', mapping: 2}    // precludes using the ordinal position as the index.
25 ]);
26 var myReader = new Roo.data.ArrayReader({
27     id: 0                     // The subscript within row Array that provides an ID for the Record (optional)
28 }, RecordDef);
29 </code></pre>
30  * <p>
31  * This would consume an Array like this:
32  * <pre><code>
33 [ [1, 'Bill', 'Gardener'], [2, 'Ben', 'Horticulturalist'] ]
34   </code></pre>
35  * @cfg {String} id (optional) The subscript within row Array that provides an ID for the Record
36  * @constructor
37  * Create a new JsonReader
38  * @param {Object} meta Metadata configuration options.
39  * @param {Object} recordType Either an Array of field definition objects
40  * as specified to {@link Roo.data.Record#create},
41  * or an {@link Roo.data.Record} object
42  * created using {@link Roo.data.Record#create}.
43  */
44 Roo.data.ArrayReader = function(meta, recordType){
45     Roo.data.ArrayReader.superclass.constructor.call(this, meta, recordType);
46 };
47
48 Roo.extend(Roo.data.ArrayReader, Roo.data.JsonReader, {
49     /**
50      * Create a data block containing Roo.data.Records from an XML document.
51      * @param {Object} o An Array of row objects which represents the dataset.
52      * @return {Object} data A data block which is used by an Roo.data.Store object as
53      * a cache of Roo.data.Records.
54      */
55     readRecords : function(o){
56         var sid = this.meta ? this.meta.id : null;
57         var recordType = this.recordType, fields = recordType.prototype.fields;
58         var records = [];
59         var root = o;
60             for(var i = 0; i < root.length; i++){
61                     var n = root[i];
62                 var values = {};
63                 var id = ((sid || sid === 0) && n[sid] !== undefined && n[sid] !== "" ? n[sid] : null);
64                 for(var j = 0, jlen = fields.length; j < jlen; j++){
65                 var f = fields.items[j];
66                 var k = f.mapping !== undefined && f.mapping !== null ? f.mapping : j;
67                 var v = n[k] !== undefined ? n[k] : f.defaultValue;
68                 v = f.convert(v);
69                 values[f.name] = v;
70             }
71                 var record = new recordType(values, id);
72                 record.json = n;
73                 records[records.length] = record;
74             }
75             return {
76                 records : records,
77                 totalRecords : records.length
78             };
79     }
80 });