PDA

View Full Version : [SOLVED] Sorting associated store data



tdack
14 May 2012, 2:13 PM
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:


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:


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:


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:



listeners: [
{
fn: 'onJsonstoreRefresh',
event: 'refresh'
}]


// .....


onJsonstoreRefresh: function(store, data, eOpts) {
store.each(function (record) {
record.ProgramsStore.sort('StartTime', 'ASC');
});


}

mitchellsimoens
16 May 2012, 5:15 AM
Thank you for posting your solution! That helps others a lot