PDA

View Full Version : Small problem in ArrayReader.js



Animal
23 Feb 2007, 5:56 AM
There's a problem when the id field is a numeric index into the data array, and the id is in index zero.

this statement:



var id = (sid && n[sid] !== undefined && n[sid] !== "" ? n[sid] : null);


yields null when (sid == 0). The first truth test on it fails.

The following code works:



Ext.extend(Ext.data.ArrayReader, Ext.data.JsonReader, {
readRecords : function(o){
var sid = this.meta ? this.meta.id : undefined; // <-- changed
var recordType = this.recordType, fields = recordType.prototype.fields;
var records = [];
var root = o;
for(var i = 0; i < root.length; i++){
var n = root[i];
var values = {};
var id = ((sid != undefined) && (n[sid] !== "") ? n[sid] : null); // <-- changed
for(var j = 0, jlen = fields.length; j < jlen; j++){
var f = fields.items[j];
var k = f.mapping || j;
var v = n[k] !== undefined ? n[k] : f.defaultValue;
v = f.convert(v);
values[f.name] = v;
}
var record = new recordType(values, id);
record.json = n;
records[records.length] = record;
}
return {
records : records,
totalRecords : records.length
};
}
});

jack.slocum
23 Feb 2007, 7:48 AM
I am worried about it = undefined (if the id is not present). What about this paranoid check?

var id = ((sid || sid === 0) && n[sid] !== undefined && n[sid] !== "" ? n[sid] : null);

Animal
23 Feb 2007, 8:55 AM
Whatever works! :D