source: trunk/web/addons/job_monarch/lib/extjs-30/src/data/ArrayReader.js @ 625

Last change on this file since 625 was 625, checked in by ramonb, 15 years ago

lib/extjs-30:

  • new ExtJS 3.0
File size: 3.6 KB
Line 
1/*!
2 * Ext JS Library 3.0.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>
16var 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]);
20var 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 */
36Ext.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        if(!this.getRoot) {
64            this.getRoot = s.root ? this.getJsonAccessor(s.root) : function(p) {return p;};
65            if(s.totalProperty) {
66                this.getTotal = this.getJsonAccessor(s.totalProperty);
67            }
68        }
69
70        var root = this.getRoot(o);
71
72        for(var i = 0; i < root.length; i++) {
73            var n = root[i];
74            var values = {};
75            var id = ((sid || sid === 0) && n[sid] !== undefined && n[sid] !== "" ? n[sid] : null);
76            for(var j = 0, jlen = fields.length; j < jlen; j++) {
77                var f = fields.items[j];
78                var k = f.mapping !== undefined && f.mapping !== null ? f.mapping : j;
79                v = n[k] !== undefined ? n[k] : f.defaultValue;
80                v = f.convert(v, n);
81                values[f.name] = v;
82            }
83            var record = new recordType(values, id);
84            record.json = n;
85            records[records.length] = record;
86        }
87
88        var totalRecords = records.length;
89
90        if(s.totalProperty) {
91            v = parseInt(this.getTotal(o), 10);
92            if(!isNaN(v)) {
93                totalRecords = v;
94            }
95        }
96
97        return {
98            records : records,
99            totalRecords : totalRecords
100        };
101    }
102});
Note: See TracBrowser for help on using the repository browser.