~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to gis/dhis-gis-geostat/mfbase/ext/source/data/ArrayReader.js

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

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