Hy there,
I'm displaying some nested data with a dataview. I've managed to do this by using the hasMany-relationship in my models and overriding some functions of the dataview. This is working just fine except that the dataview does not update properly after manipulating the store (deleting or updating records).
The store's model looks like this:
PHP Code:
// Define the pet-model
Ext.define('Pet', {
extend: 'Ext.data.Model',
fields: [
{ name: 'name', type: 'string' },
{ name: 'type', type: 'string' }
]
});
// Define the person-model (each person has many pets...)
Ext.define('Person', {
extend: 'Ext.data.Model',
fields: [{ name: 'name', type: 'string' }],
hasMany: { model: 'Pet', name: 'pets' }
});
I've overriden 5 methods of the dataview to make it show the xtemplate with nested data correctly. I took the overrides from this thread and adjusted them a little to fit my needs:
PHP Code:
updateIndexes: function(startIndex, endIndex) {
var ns = this.all.elements,
records = this.store.getRange(),
recordsCnt = records.length || 0,
allRecords = [],
i;
Ext.iterate(records, function (record, idx) {
var pets = record.pets();
pets.each(function(recipe) {
allRecords.push(recipe);
});
});
startIndex = startIndex || 0;
endIndex = endIndex || ((endIndex === 0) ? 0 : (ns.length - 1));
for(i = startIndex; i <= endIndex; i++) {
ns[i].viewIndex = i;
ns[i].viewRecordId = ((allRecords.length > i) && allRecords[i]) ? allRecords[i].internalId : undefined;
if (!ns[i].boundView) {
ns[i].boundView = this.id;
}
}
},
prepareData: function(data, index, record) {
var associatedData, attr;
if (record) {
associatedData = record.getAssociatedData();
for (attr in associatedData) {
if (associatedData.hasOwnProperty(attr)) {
data[attr] = associatedData[attr];
}
}
}
return data;
},
collectData: function(records, startIndex){
var data = [],
i,
len = records.length,
record;
for (i = 0; i < len; i++) {
record = records[i];
data[i] = this.prepareData(record.data, startIndex + i, record);
}
return data;
},
bufferRender: function(records, index){
var me = this,
div = me.renderBuffer || (me.renderBuffer = document.createElement('div'));
me.tpl.overwrite(div, me.collectData(records, index));
return Ext.query(me.getItemSelector(), div);
},
getRecord: function(node) {
var recordId = Ext.getDom(node).viewRecordId,
record = (recordId ? this.store.data.getByKey(recordId) : undefined);
if (!record && (undefined !== recordId)) {
this.store.each(function(rec) {
record = rec.pets().data.getByKey(recordId);
return (record === undefined);
});
}
return record;
}
I've put together a little live example: http://jsfiddle.net/6GFc2/
As you can see the nested data is displayed just fine but after the deletion of a "sub-record" (by clicking the x) the dataview is not updated correctly even though the store is (as you can see in the debug output panel).
I'd really appreciate any help with that. I guess I'm going to have to override some more methods of the dataview to make this work but actually I don't have no clue where to start looking...
Thanks & best regards,
mike