AmdaTreeReader.js 2.46 KB
/** 
 * 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
            me.convertRecordData(convertedValues, node, record, me.applyDefaults);
            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){}
});