Blame view

js/app/stores/AmdaTreeReader.js 2.46 KB
16035364   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/** 
 * Project  : AMDA-NG4
 * Name     : AmdaTreeReader.js
 * @class   amdaReader.AmdaTreeReader
 * @extends Ext.data.reader.Json 
 * @brief    
 * @author  Sencha Support + CDA
 * @version $Id: AmdaTreeReader.js 894 2012-05-02 09:18:29Z elena $
 * @todo
 *******************************************************************************
 *    FT Id     :   Date   : Name - Description
 *******************************************************************************
 *    :           :10/06/2011: CDA  - Init
 *    :           :22/06/2011: elena  - added new nodes
 *   
 *  
 */

Ext.define('amdaReader.AmdaTreeReader', {
    extend: 'Ext.data.reader.Json',
        
    alias: 'reader.tree',
    
    extractData : function(root) {
        var me = this,
            records = [],
            Model   = me.model,
            length  = root.length,
            convertedValues, node, record, i, Type;

        if (!root.length && Ext.isObject(root)) {
            root = [root];
            length = 1;
        }

        for (i = 0; i < length; i++) {
            node = root[i];
            Type = me.getType(node);
            // Create a record with an empty data object.
            // Populate that data object by extracting and converting field values from raw data
            record = new Type(undefined, me.getId(node), node, convertedValues = node);

            // If the server did not include an id in the response data, the Model constructor will mark the record as phantom.
            // We  need to set phantom to false here because records created from a server response using a reader by definition are not phantom records.
            record.phantom = false;

            // Use generated function to extract all fields at once
5d15649f   Benjamin Renard   Migration to ExtJ...
48
            me.convertRecordData(convertedValues, node, record, me.applyDefaults);
16035364   Benjamin Renard   First commit
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
            records.push(record);

            if (me.implicitIncludes) {
                me.readAssociated(record, node);
            }
        }

        return records;
    },
 
    extractValues: function(data, Type) {
  
        var fields = Type.prototype.fields.items,
            i      = 0,
            length = fields.length,
            output = {},
            field, value;
 
        for (; i < length; i++) {
            field = fields[i];
            value = data[field.name];
            output[field.name] = value;
        }
 
        return output;
    },
 
    /**
     * @abstract This method has to be overriden in subclasses
     */
    getType: function(node){}
});