PDA

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



lagnat
26 Jan 2012, 5:35 PM
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'));




SyntaxError: Expected an identifier but found '=' instead

evant
26 Jan 2012, 5:51 PM
See: http://www.sencha.com/forum/showthread.php?176762-4.1Beta1-SyntaxError-Unexpected-token-delete

lagnat
27 Jan 2012, 12:30 AM
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:

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?

evant
27 Jan 2012, 10:11 AM
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:



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.

lagnat
27 Jan 2012, 10:27 AM
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.

evant
27 Jan 2012, 4:07 PM
Agreed, please see: http://www.sencha.com/forum/showthread.php?176466-4.1-20120123-Unable-to-use-class-model-fields&p=722179&viewfull=1#post722179

lagnat
2 Feb 2012, 8:37 AM
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.