View Full Version : [FIXED] DataView.prepareAssociatedData does not handle belongsTo associations

29 Nov 2011, 10:48 AM

Sencha Touch 2 PR2
Browser versions tested against:

Chrome 11
DOCTYPE tested against:


The DataView.prepareAssociatedData does not handle loading of belongsTo associations.
Steps to reproduce the problem:

Please view http://www.sencha.com/forum/showthread.php?156713-Many-to-one-associations-in-tpl for a full example and description of issue. There is also a semi working method extension listed. That example has Messages with Users. Each message has one user. I load the messages nested with their user in json and want to display the messages as a list with the user data.
The result that was expected:

In my tpl I expected to be able to write {user.name} and have their name show.
The result that occurs instead:

blank name.
Test Case:
http://www.sencha.com/forum/showthread.php?156713-Many-to-one-associations-in-tpl has all the code. If the prepareAssociatedData I have in my list is not there it will not work.

Possible fix:

I modified the prepareAssociatedData method based on another posters fix for ST1. From the comments in the method I assume there are other issues which I have yet to encounter. I am not sure if those issues still hold true for ST2 or not as I was not the one to add the comment.

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,
associatedStore, associatedRecords, associatedRecord,
associatedRecordCount, association, internalId;

for (; i < associationCount; i++) {
association = associations[i];
if(association.type === 'belongsTo') {
associatedRecord = record[association.associatedName + 'BelongsToInstance'];
if(associatedRecord) {
associationData[association.name] = associatedRecord.data;
* This does not function as intended. When uncommented items which the record
* has many of will only have one item in the result set. However, for now I am
* not going to worry about it. Though, I am pretty sure it is important that
* this functions properly considering if the record here had yet another
* association it would not be loaded into the template. Essentially, at this time
* all belongTo associations can be considered leafs, sadly.
//Ext.apply(associationData[association.name], this.prepareAssociatedData(associatedRecord, ids));
} else {
//this is the hasMany store filled with the associated data
associatedStore = 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 (associatedStore && associatedStore.data.length > 0) {
associatedRecords = associatedStore.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) {

associationData[association.name][j] = associatedRecord.data;
Ext.apply(associationData[association.name][j], this.prepareAssociatedData(associatedRecord, ids));

return associationData;

Jamie Avins
29 Nov 2011, 2:53 PM
Thank you for the report.

19 Jan 2012, 2:10 PM
This has been fixed and will be part of the next Touch 2.0 release. Thanks for the report.