PDA

View Full Version : DwrArrayReader



otatop
23 Oct 2008, 12:23 PM
This is a simple mod on the ArrayReader so you can specify sub attributes of an array object. This proved to be really handy when working with the dwr proxy from http://extjs.com/forum/showthread.php?t=19529

Sample:

{name: 'name', mapping: 'object.name'},



Ext.namespace("Ext.ux.data");

Ext.ux.data.DwrArrayReader = Ext.extend(Ext.data.ArrayReader, {

readRecords : function(o){
var sid = this.meta ? this.meta.id : null;
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 || sid === 0) && n[sid] !== undefined && n[sid] !== "" ? n[sid] : null);
for(var j = 0, jlen = fields.length; j < jlen; j++){
var f = fields.items[j];
var k = f.mapping !== undefined && f.mapping !== null ? f.mapping : j;
var v;

var keys = f.mapping.split(".");
var currentValue = n;
for(var keyIndex = 0; (currentValue) && keyIndex < keys.length; keyIndex++){
currentValue = currentValue[keys[keyIndex]];
}
v = currentValue;

v = f.convert(v, n);
values[f.name] = v;
}
var record = new recordType(values, id);
record.json = n;
records[records.length] = record;
}
return {
records : records,
totalRecords : records.length
};
}
})