Success! Looks like we've fixed this one. According to our records the fix was applied for TOUCH-2122 in a recent build.
  1. #1
    Sencha User
    Join Date
    Sep 2011
    Posts
    46
    Vote Rating
    0
    Zyphrax is on a distinguished road

      0  

    Default Can't define a custom association (ST2-B3)

    Can't define a custom association (ST2-B3)


    In our project we would like to create a custom association class that works a little bit different than the belongsTo association. Unfortunately this seemingly old piece of code in Ext.data.association is giving me a headache:

    Code:
    statics: {
            create: function(association) {
                if (!association.isAssociation) {
                    if (Ext.isString(association)) {
                        association = {
                            type: association
                        };
                    }
    
                    switch (association.type) {
                        case 'belongsTo':
                            return Ext.create('Ext.data.association.BelongsTo', association);
                        case 'hasMany':
                            return Ext.create('Ext.data.association.HasMany', association);
                        case 'hasOne':
                            return Ext.create('Ext.data.association.HasOne', association);
                        default:
                            //<debug>
                            Ext.Logger.error('Unknown Association type: "' + association.type + '"');
                            //</debug>
                    }
                }
    
                return association;
            }
        }
    Could you change default: to Ext.create(association.type)?
    I think that the switch is somewhat nasty

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    36,812
    Vote Rating
    834
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    I think instead of the switch it could try to resolve the association class name based ont he type being passed in and the alias of the associations themselves.
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  3. #3
    Sencha User
    Join Date
    Mar 2007
    Location
    Haarlem, Netherlands
    Posts
    1,243
    Vote Rating
    10
    TommyMaintz will become famous soon enough TommyMaintz will become famous soon enough

      0  

    Default


    Alright, this has been fixed for the next release.

    The only thing that I think you might run into is the fact that reading in associated data in Model.js is checking for the type on the association. If you make a custom one, this code would not be triggered.

  4. #4
    Sencha User
    Join Date
    Sep 2011
    Posts
    46
    Vote Rating
    0
    Zyphrax is on a distinguished road

      0  

    Default


    Thanks for the fix!

  5. #5
    Sencha User
    Join Date
    Oct 2011
    Posts
    1
    Vote Rating
    0
    bparker is on a distinguished road

      0  

    Default


    Any chance this could get applied to the main ExtJS 4.1 code as well?

  6. #6
    Sencha User
    Join Date
    Aug 2011
    Posts
    6
    Vote Rating
    0
    mrroohian is on a distinguished road

      0  

    Default


    I fixed the problem with this patch. Waiting for the new release though.

    Code:
        statics: {
            
            create: function(association){
                if (Ext.isString(association)) {
                    association = {
                        type: association
                    };
                }
    
    
                switch (association.type) {
                    case 'belongsTo':
                        return new Ext.data.association.BelongsTo(association);
                    case 'hasMany':
                        return new Ext.data.association.HasMany(association);
                    case 'hasOne':
                        return new Ext.data.association.HasOne(association);
                    
    
    
    
    
                    default:
                        // I hope this works with the fix in the new version
                        return Ext.createByAlias('association.' + association.type.toLowerCase(), association);
                }
                return association;
            }
        },