PDA

View Full Version : [FIXED] DataView with hasOne association



omarc
18 Oct 2011, 10:48 AM
I am trying to use a DataView with a template that makes reference to a hasOne association and I get the following error in Safari:

TypeError: '[object Object]' is not a function (evaluating 'Ext.log('Error: ' + e.message)')

After tracing the problem, I found that the prepareAssociatedData function in DataView.js only supports hasMany associations, I modified it so that it also works for hasOne associations:



prepareAssociatedData: function(record, ids) {
//we keep track of all of the internalIds of the models that we have loaded so far in here
ids = ids || [];


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


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


if (association.type == "hasMany") {
//this is the hasMany store filled with the associated data
associatedInstance = record[association.storeName];


//we will use this to contain each associated record's data
associationData[association.name] = [];


//if it's loaded, put it into the association data
if (associatedInstance && associatedInstance.data.length > 0) {
associatedRecords = associatedInstance.data.items;
associatedRecordCount = associatedRecords.length;


//now we're finally iterating over the records in the association. We do this recursively
for (; j < associatedRecordCount; j++) {
associatedRecord = associatedRecords[j];
internalId = associatedRecord.internalId;


//when we load the associations for a specific model instance we add it to the set of loaded ids so that
//we don't load it twice. If we don't do this, we can fall into endless recursive loading failures.
if (ids.indexOf(internalId) == -1) {
ids.push(internalId);


associationData[association.name][j] = associatedRecord.data;
Ext.apply(associationData[association.name][j], this.prepareAssociatedData(associatedRecord, ids));
}
}
}
} else if (association.type == "hasOne") {
//this is the to-one record instance
associatedInstance = record[association.instanceName];


//if it's loaded, put it into the association data
if (associatedInstance) {
associationData[association.name] = associatedInstance.data;
Ext.apply(associationData[association.name], this.prepareAssociatedData(associatedInstance, ids));
}
}
}


return associationData;
}


Hope this or some variation makes it into the final release.

Thanks and great work on Sencha Touch!

Omar

Jamie Avins
18 Oct 2011, 12:55 PM
Thank you, we'll be sure to resolve that.

oddz
18 Oct 2011, 8:17 PM
Doesn't pass belongsTo either. Though that feature did not exist in 1.0 either… had to add support myself.

TommyMaintz
19 Jan 2012, 2:09 PM
This has been fixed and will be part of the next Touch 2.0 release. Thanks for the port and debugging done to solve this!