1. #1
    Sencha User
    Join Date
    Jul 2011
    Location
    Melbourne Australia
    Posts
    8
    Vote Rating
    0
    tdack is on a distinguished road

      0  

    Default [SOLVED] Sorting associated store data

    [SOLVED] Sorting associated store data


    I have a store that has an associated store loaded automatically through the model association.

    How can I have the associated child store of each parent record automatically sorted on a field in the child store?

    Parent Store:
    Code:
    Ext.define('MythTouch.store.ProgramGuide', {
        extend: 'Ext.data.Store',
        requires: [
            'MythTouch.model.ChannelModel'
        ],
    
    
        config: {
            autoLoad: true,
            groupDir: 'ASC',
            groupField: 'ChanId',
            model: 'MythTouch.model.ChannelModel',
            storeId: 'ProgramGuide',
            proxy: {
                type: 'ajax',
                enablePagingParams: false,
                url: 'xml-proxy.php?Guide/GetProgramGuide',
                reader: {
                    type: 'json',
                    idProperty: 'ChanId',
                    rootProperty: 'ProgramGuide.Channels',
                    totalProperty: 'Count'
                }
            }
        }
    });
    Parent Model:
    Code:
    Ext.define('MythTouch.model.ChannelModel', {
        extend: 'Ext.data.Model',
        requires: [
            'MythTouch.model.ProgramModel'
        ],
        config: {
            fields: [
                { name: 'ChanId',     sortType: 'asInt',   type: 'int'  },
                { name: 'ChanNum', sortType: 'asText', type: 'int' },
                { name: 'CallSign' },
                { convert: function(v, rec) {
                        v = "img-proxy.php?" + v.replace("?", "&");
                        return v;  },
                    name: 'IconURL', type: 'string' },
                { name: 'ChannelName' }
            ],
            hasMany: {
                associationKey: 'Programs',
                model: 'MythTouch.model.ProgramModel',
                autoLoad: true,
                name: 'Programs',
                associatedName: 'Programs'
            }
        }
    });
    Associated Child Model:
    Code:
    Ext.define('MythTouch.model.ProgramModel', {
        extend: 'Ext.data.Model',
        requires: [
            'MythTouch.model.RecordingModel'
        ],
        config: {
            fields: [
                { name: 'StartTime' },
                { name: 'EndTime' },
                { name: 'Title' },
                { name: 'Repeat' }
            ]
        }
    });
    Ideally I'd like the store for the associated child model to be sorted on StartTime automatically.

    Creating this in Sencha Architect I can't work out how to make this happen.

    Any advice appreciated.


    Solved!

    Well that turned our easier than expected.

    I simply defined a listener and handler for the refresh event like so:

    Code:
            listeners: [
                {
                    fn: 'onJsonstoreRefresh',
                    event: 'refresh'
                }]
    
    
    // .....
    
    
        onJsonstoreRefresh: function(store, data, eOpts) {
                          store.each(function (record) {
                         record.ProgramsStore.sort('StartTime', 'ASC');
            });
    
    
        }
    Last edited by tdack; 14 May 2012 at 9:29 PM. Reason: [SOLVED]

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    St. Louis, MO
    Posts
    33,684
    Vote Rating
    435
    mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of

      0  

    Default


    Thank you for posting your solution! That helps others a lot
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    http://www.JSONPLint.com - Source to lint your JSONP!

    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 almost in print!

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

Tags for this Thread