1. #1
    Ext JS Premium Member
    Join Date
    Oct 2007
    Posts
    85
    Vote Rating
    4
    lagnat is on a distinguished road

      0  

    Default [4.1b2] Ext.data.reader.Reader doesn't work with Models that have numeric field names

    [4.1b2] Ext.data.reader.Reader doesn't work with Models that have numeric field names


    Code:
            Ext.define('MyModel', {
                    extend: 'Ext.data.Model',
                    fields: [
                            {name: '12345',  type: 'string'}
                    ],
                    proxy: {
                            type: 'memory'
                    }
            });
    
            var reader = Ext.create('Ext.data.JsonReader', {
                    model: 'MyModel',
                    root: 'root'
            });
    
            var recs = reader.readRecords({
                    root: [{
                            '12345': 'field 1 value'
                    }]
            });
    
            alert(recs.records[0].get('12345'));
    Code:
    SyntaxError: Expected an identifier but found '=' instead
    -->

  2. #2
    Sencha - Ext JS Dev Team evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    17,018
    Vote Rating
    650
    evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute

      0  
    Evan Trimboli
    Sencha Developer
    Twitter - @evantrimboli
    Don't be afraid of the source code!
    -->

  3. #3
    Ext JS Premium Member
    Join Date
    Oct 2007
    Posts
    85
    Vote Rating
    4
    lagnat is on a distinguished road

      0  

    Default


    Evan,

    Prepending underscore doesn't solve the problem. Consider a Model with 2 fields called "12345" and "67890". The generated code will result in both variables being called "_".

    How about this:
    Code:
    fieldVarName[i] = '_' + i;
    On a related note, I checked the latest nightly build to see what the final fix would be and didn't see any change. Can we expect to see this fix in the next nightly?
    -->

  4. #4
    Sencha - Ext JS Dev Team evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    17,018
    Vote Rating
    650
    evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute

      0  

    Default


    I don't see why it would end up with just an underscore as the name, you can see here I've got the fix applied and it reads the data:

    Code:
    Ext.define('Test', {
        extend : 'Ext.data.Model',
        fields : ['12345', '67890']
    });
    
    Ext.require('Ext.data.*');
    
    Ext.onReady(function() {
        
        Ext.define('ReaderOverride', {
            override: 'Ext.data.reader.Reader',
            
            buildRecordDataExtractor: function() {
                var me = this,
                    modelProto = me.model.prototype,
                    clientIdProp = modelProto.clientIdProperty,
                    fields = modelProto.fields.items,
                    numFields = fields.length,
                    fieldVarName = [],
                    varName,
                    i = 0,
                    field,
                    code = [
                        'var me = this,\n',
                        '    fields = me.model.prototype.fields,\n',
                        '    value,\n',
                        '    internalId'
                    ];
    
                for (; i < numFields; i++) {
                    field = fields[i];
                    fieldVarName[i] = '_' + Ext.String.createVarName(field.name);
                    code.push(',\n    ', fieldVarName[i], ' = fields.get("', field.name, '")');
                }
                code.push(';\n\n    return function(dest, source, record) {\n');
    
                for (i = 0; i < numFields; i++) {
                    field = fields[i];
                    varName = fieldVarName[i];
                    // createFieldAccessExpression must be implemented in subclasses to extract data from the source object in the correct way.
                    code.push('        dest', ((field.name === varName) ? ('.' + varName) : ('["' + field.name + '"]')), ' = ', me.createFieldAccessExpression(field, varName, 'source'), ';\n');
                }
    
                // set the client id as the internalId of the record.
                // clientId handles the case where a client side record did not previously exist on the server,
                // so the server is passing back a client id that can be used to pair the server side record up with the client record
                if (clientIdProp) {
                    code.push('        if (internalId = ' + me.createFieldAccessExpression({mapping: clientIdProp}, null, 'source') + ') {\n');
                    code.push('            record.internalId = internalId;\n        }\n');
                }
    
                code.push('    };');
    
                return Ext.functionFactory(code.join('')).call(me);
            }    
        });
        
        var store = Ext.create('Ext.data.Store', {
            model : 'Test',
            data : {
                records : [{
                    '12345' : 1,
                    '67890' : 'foo'
                }, {
                    '12345' : 2,
                    '67890' : 'bar'
                }]
            },
            proxy : {
                type : 'memory',
                reader : {
                    type : 'json',
                    root : 'records'
                }
            }
        });
        
        store.each(function(rec){
            console.log(rec.get('12345'), rec.get('67890'));
        });
    });
    It should be merged into a nightly build over the next few days.
    Evan Trimboli
    Sencha Developer
    Twitter - @evantrimboli
    Don't be afraid of the source code!
    -->

  5. #5
    Ext JS Premium Member
    Join Date
    Oct 2007
    Posts
    85
    Vote Rating
    4
    lagnat is on a distinguished road

      0  

    Default


    Yes.. it does read the data but then defaultValue doesn't work.

    My $.02: As far as I can tell, there's no need to generate variables names based on the field names anyway. It also seems like it's extra work to even fetch the fields on the (maybe) chance that default value is needed. In my testing, which I admit was minimal, using '_' + i as the field name worked well.
    Attached Images
    -->

  6. #6
    Sencha - Ext JS Dev Team evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    17,018
    Vote Rating
    650
    evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute

      0  
    Evan Trimboli
    Sencha Developer
    Twitter - @evantrimboli
    Don't be afraid of the source code!
    -->

  7. #7
    Ext JS Premium Member
    Join Date
    Oct 2007
    Posts
    85
    Vote Rating
    4
    lagnat is on a distinguished road

      0  

    Default


    Evan.. this fix is now in the nightly builds. Thanks. I just wanted to point out that the local var "prefix" is not being used.
    -->

Thread Participants: 1