You found a bug! We've classified it as TOUCH-3811 . We encourage you to continue the discussion and to find an acceptable workaround while we work on a permanent fix.
  1. #1
    Sencha User
    Join Date
    Apr 2012
    Posts
    28
    Vote Rating
    2
    natb is on a distinguished road

      0  

    Default Model: the order of 'convert' fields can be broken

    Model: the order of 'convert' fields can be broken


    REQUIRED INFORMATION
    Ext version tested:
    • Sencha 2.1
    Browser versions tested against:
    • Chrome 23.0
    Description:
    • Sencha applies sorting to Model fields based on 'covert' function existence in order to ensure that fields with 'convert' are at the bottom.

      But it seems that in some cases sorting can reorder equal items thus breaking initially configured order and dependencies between 'convert' fields.

      It seems there is no need to apply sorting to fields array and enough to go through all fields once moving 'non-convert' fields to the top. This would keep the order of 'convert' fields.
    Steps to reproduce the problem:
    • Create model as in the test case with 'convert' fields depending each other -> view fields array, the order of 'convert' fields is different to the configured order.
    • Create a record of that model -> error appears because some of expected fields are not set yet.
    Test Case:
    Code:
     Ext.define('User', {
        extend: 'Ext.data.Model',
    
    
        config: {
            fields: [
                { name: 'first_name', type: 'string' },
                { name: 'first_name2', type: 'string' },
                { name: 'first_name3', type: 'string' },
                { name: 'first_name4', type: 'string' },
                { name: 'first_name5', type: 'string' },
                { name: 'first_name6', type: 'string' },
                
                { name: 'last_name', type: 'string' },
                
                { name: 'letters1',
                    convert: function (v, record) {
                        return record.get('last_name').substr(0, 2);
                    }
                },
                { name: 'letters2',
                    convert: function (v, record) {
                        return record.get('letters1').substr(0, 1);
                    }
                },
                { name: 'letters4',
                    convert: function (v, record) {
                        return record.get('letters1').substr(0, 1);
                    }
                },
                { name: 'letters5',
                    convert: function (v, record) {
                        return record.get('letters1').substr(0, 1);
                    }
                },
                { name: 'letters6',
                    convert: function (v, record) {
                        return record.get('letters1').substr(0, 1);
                    }
                },
                { name: 'letters7',
                    convert: function (v, record) {
                        return record.get('letters1').substr(0, 1);
                    }
                }
            ]
        }
    });
    
    User.getFields().keys // fields are reordered: id is added at position 1, first_name2 is moved to position 7, letters1 is moved to bottom position 13 so that letters2, letters3, etc can't be properly set now
    
    
    var user = Ext.create('User', {
        first_name: 'Ed',
        last_name: 'Spencer'
    });  // -> Error; Cannot call method 'substr' of undefined

  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


    Thanks for the report! I have opened a bug in our bug tracker.

  3. #3
    Sencha Premium Member
    Join Date
    Aug 2007
    Location
    The Netherlands
    Posts
    51
    Vote Rating
    3
    extjs@kingsquare.nl is on a distinguished road

      0  

    Default


    The same issue (still?) applies to ExtJs 4.2.2

  4. #4
    Sencha Premium Member
    Join Date
    Aug 2007
    Location
    The Netherlands
    Posts
    51
    Vote Rating
    3
    extjs@kingsquare.nl is on a distinguished road

      0  

    Default


    I worked around the issue by resorting the fields _after_ constructing the Model:

    Code:
     Ext.define('App.model.MyModel', myModelConfig, function (function (myModel) {
        var correctOrder = ['field', 'names', 'in', 'correct', 'order'];
        myModel.prototype.fields.sortBy(function (a, b) {
            var indexOfA = correctOrder.indexOf(a.name);
    	var indexOfB = correctOrder.indexOf(b.name);
    	return indexOfA-indexOfB;
        });
    });
    But that's just a hacky work-a-round