Results 1 to 8 of 8

Thread: belongsTo association and XTemplate

  1. #1
    Ext JS Premium Member
    Join Date
    Jul 2008
    Location
    Miami, FL
    Posts
    72

    Default belongsTo association and XTemplate

    Hey all,

    I wanted to know how I could take advantage of the nested data loading stuff in an XTemplate.

    If I have for example the following models:

    Code:
    Ext.regModel ('Employee', {
        fields: [
            { name: 'salary', type: 'float' }
        ],
        belongsTo: 'Person'
    });
    
    Ext.regModel ('Person', {
        fields: [
            { name: 'firstName', type: 'string' },
            { name: 'lastName', type: 'string' }
        ]
    });
    I think the itemTpl would be something like "{person.firstName}" but that does not display any data. I have found a couple of examples on how to do this in the forum for hasMany associations but not for belongsTo.

    How would I use these in a List that displays the model Employee?

    Thanks for your time,

    Omar

  2. #2
    Ext JS Premium Member
    Join Date
    Jul 2008
    Location
    Miami, FL
    Posts
    72

    Default

    I've kept looking at this and I think I found where the issue might be located.

    Function prepareAssociatedData in DataView.js does not take into consideration "belongsTo" associations. It only handles hasMany, this is why using employee.data returns the person association as an array.

    What would be the correct way to fix this bug?

    Omar

  3. #3
    Sencha User
    Join Date
    Feb 2011
    Posts
    1

    Default

    Hello!

    I've run into the same issue. Does anybody know the solution for?

    Regards,
    Igor

  4. #4
    Sencha User
    Join Date
    Dec 2009
    Posts
    6

    Default

    Any luck finding a good approach to this?

  5. #5
    Ext JS Premium Member
    Join Date
    Jul 2008
    Location
    Miami, FL
    Posts
    72

    Default

    I ended up overriding the function prepareAssociatedData which just goes through the associations and creates a new Object containing all the data that the DataView requires.

    I am sure it is not the cleanest way to do it, but I found no other way.

    Hope that helps,

    Omar

  6. #6
    Ext JS Premium Member
    Join Date
    Jan 2011
    Posts
    22

    Default

    omarc, I've run into the same issue. Mind sharing your override of prepareAssociatedData?

  7. #7
    Ext JS Premium Member
    Join Date
    Jul 2008
    Location
    Miami, FL
    Posts
    72

    Default

    Sure, here is the override I am using:

    Code:
    Ext.DataView.override({
    	getRecordData: function(record, ids) {
    		var data = record.data;
    		Ext.apply(data, this.prepareAssociatedData(record, ids));
    
    		return data;
    	},
    
    	prepareAssociatedData: function(record, ids) {
    		ids = ids || [];
    
    		var data = {};
    		var associations = record.associations;
    
    		associations.each(function(association) {
    			if (association.type == "belongsTo") {
    				var belongsToRecord = record[association.instanceName];
    
    				if (belongsToRecord != undefined && ids.indexOf(belongsToRecord.internalId) == -1) {
    					ids.push(belongsToRecord.internalId);
    					data[association.name] = this.getRecordData(belongsToRecord, ids);
    				}
    			} else {
    				var toManyStore = record[association.storeName];
    
    				if (toManyStore != undefined) {
    					var records = [];
    					toManyStore.each(function(toManyRecord) {
    						if (ids.indexOf(toManyRecord.internalId) == -1) {
    							ids.push(toManyRecord.internalId);
    							records.push(this.getRecordData(toManyRecord, ids));
    						}
    					}, this);
    					data[association.name] = records;
    				}
    			}
    		}, this);
    
    		return data;
    	}
    });
    Works well for me, you may have to tweak it for your needs.

    Omar

  8. #8
    Sencha User
    Join Date
    Sep 2011
    Posts
    7

    Default Not fixed in Sencha Touch 1.1.0

    Hello,

    I am currently using Sencha Touch v 1.1.0 and I had this problem with XTemplate and models associations. I used the fix provided by omarc, works great.
    Any news about a fix included in the next version ?

    Thanks.

Similar Threads

  1. [FIXED] Can't have more than 1 hasMany association
    By stormin_walker in forum Sencha Touch 1.x: Bugs
    Replies: 20
    Last Post: 18 Dec 2013, 7:27 AM
  2. Replies: 12
    Last Post: 19 Jan 2010, 5:36 AM
  3. regarding the EXTJS trees - association/link
    By sravanth in forum Ext 3.x: Help & Discussion
    Replies: 3
    Last Post: 5 Jun 2009, 2:30 AM
  4. new XTemplate vs XTemplate.from
    By kesav in forum Ext 2.x: Help & Discussion
    Replies: 0
    Last Post: 5 Sep 2008, 8:38 AM
  5. 1:m association tables
    By johnstontrav in forum Ext 2.x: Help & Discussion
    Replies: 13
    Last Post: 14 May 2008, 3:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •