PDA

View Full Version : [FIXED-EXTJSIV-321] AbstractDataView.prepareAssociatedData



c2c-guinness
24 Mar 2011, 1:02 PM
Doesn't it seem a little odd that AbstractDataView.prepareAssociatedData requires that each association have an existing store with existing data?

belongsTo associations wont have a store, even if they have data.

So records cannot reference their parent data in templates, etc.

See: src/AbstractDataView.js: Line 368

I've already corrected a similar problem to allow grid columns (headers) to reference parent table data. Here is my override for Ext.grid.HeaderContainer.prepareData:




var orig_hc = Ext.grid.HeaderContainer;
var orig_hc_pd = orig_hc.prototype.prepareData;

Ext.override(Ext.grid.HeaderContainer,{
prepareData: function(data, rowIdx, record){

var raw = record.raw;

for( var name in raw ) {
var vari = raw[name];
if( Ext.isObject( vari ) ) {
var subRecord = vari; //[0];
for (var prop in subRecord) {
var val = subRecord[prop];
data[ name + "." + prop] = val;
}
}
}

var ret = orig_hc_pd.apply( this, arguments );
return ret;

}
});



This override allows a header's dataIndex to be myParent.data. The override is not intended to suppliment hasMany relationships in the same way. This override, or something like it, should exist in ExtJS core.. I think.

Lastly, where is hasOne relationships? Seems like a lot of common sense stuff was completely skipped for model relationships. I mean, you have a Polymorphic relationship, which isnt even a part of standard relational db design (its a bad practice), but no hasOne, which is very definitely part of standard relational db design (happens often).

Thanks,
Luke

c2c-guinness
24 Mar 2011, 2:03 PM
A temporary fix.. allows templates to reference parent model objects via "{parentModel.property}"




Ext.override(Ext.AbstractDataView,{

prepareAssociatedData: function(record, ids) {
ids = ids || [];

var associations = record.associations.items,
associationCount = associations.length,
associationData = {},
associatedStore, associatedName, associatedRecords, associatedRecord,
associatedRecordCount, association, internalId, i, j;

for (i = 0; i < associationCount; i++) {
association = associations[i];

associatedStore = record[association.storeName];

associationData[association.name] = [];

if (associatedStore && associatedStore.data.length > 0) {
associatedRecords = associatedStore.data.items;
associatedRecordCount = associatedRecords.length;

for (j = 0; j < associatedRecordCount; j++) {
associatedRecord = associatedRecords[j];
internalId = associatedRecord.internalId;

if (Ext.Array.indexOf(ids, internalId) == -1) {
ids.push(internalId);

associationData[association.name][j] = associatedRecord.data;
Ext.apply(associationData[association.name][j], this.prepareAssociatedData(associatedRecord, ids));
}
}
} else if( record.raw[ association.name ] ) {
var aData = record.raw[ association.name ];
if (Ext.isObject(aData)) {
associationData[association.name] = aData;
for (var prop in aData) {
var val = aData[prop];
associationData[ association.name + "." + prop] = val;
}
}
}
}

return associationData;

}

});

evant
3 Apr 2011, 12:46 AM
In the next release it will copy parent associations as well.

c2c-guinness
3 Apr 2011, 7:43 AM
What do you mean? Can you tell me exactly what will happen in the next release so I can remove/edit my overrides?

Thanks,
Luke