PDA

View Full Version : Sorting a JsonStore



sissonb
1 Mar 2011, 3:12 PM
I have this Json,


{"schedules":{
"hashCode":-1827125752,
"scheduleList":[
{
"timeStamp":1299052800000,
"slots":[true,true,true,true,false,false,true,true]
},
{
"timeStamp":1299916800000,
"slots":[true,true,true,true,false,false,true,true]
},
{
"timeStamp":1299484800000,
"slots":[true,true,true,true,false,false,true,true]
},
{
"timeStamp":1299571200000,
"slots":[true,true,true,true,false,false,true,true]
},
{
"timeStamp":1299744000000,
"slots":[true,true,true,true,false,false,true,true]
},
{
"timeStamp":1300258800000,
"slots":[true,true,true,true,false,false,true,true]
},
{
"timeStamp":1300086000000,
"slots":[true,true,true,true,false,false,true,true]
},
{
"timeStamp":1300345200000,
"slots":[true,true,true,true,false,false,true,true]
},
{
"timeStamp":1299657600000,
"slots":[true,true,true,true,false,false,true,true]
},
{
"timeStamp":1299830400000,
"slots":[true,true,true,true,false,false,true,true]
},
{
"timeStamp":1299139200000,
"slots":[true,true,true,true,false,false,true,true]
},
{
"timeStamp":1300431600000,
"slots":[true,true,true,true,false,false,true,true]
},
{
"timeStamp":1299225600000,
"slots":[true,true,true,true,false,false,true,true]
},
{
"timeStamp":1300172400000,
"slots":[true,true,true,true,false,false,true,true]
},
{
"timeStamp":1299312000000,
"slots":[true,true,true,true,false,false,true,true]
}
]
}}

With this JsonStore,

CustomerCare.ScheduleData = new Ext.data.JsonStore({
storeId: 'ScheduleData',
url: 'http://localhost/testData/calendarUpdate.json',
idProperty: 'schedules',
method:'POST',
fields: [
{
name: 'hashCode',
mapping:'schedules.hashCode'
},
{
name: 'scheduleList',
mapping: 'schedules.scheduleList'
}
]
});


I am trying to sort the scheduleList array by it's timeStamp. I have tried,

CustomerCare.ScheduleData.sort('schedules.scheduleList.timeStamp');
CustomerCare.ScheduleData.sort('scheduleList.timeStamp');
CustomerCare.ScheduleData.sort('timeStamp');


But it never seems to have an effect on the data. Any ideas? I know with the normal JavaScript sort method you pass a function into the method that defines the sorting algorithm. I am trying to use Ext.js to do the sorting to keep my code clean, but might end up just passing the data through a normal sorting function.

The sorting function is going to be something like,



var sortFunction = function(tsObj1, tsObj1){
return tsObj1.timeStamp - tsObj2.timeStamp;
}

Pretty tricky...

sissonb
2 Mar 2011, 9:56 AM
So I've gone ahead and created my own sorting algorithm outside of extjs. I want to attach this sort to the loading of the JsonStore. Currently I am trying to use the afterLoad event in the jsonStore with no luck. The only way I can sort the data is to manually do the sort in the callback event everytime I load the store. Is there a way to attach this event to the JsonStore?

Here is a screencast of the issue. http://screencast.com/t/FYqcW08L

sissonb
2 Mar 2011, 3:51 PM
I have figured out how to add the sort to the load event. I needed to use the listeners object and put a load function in there.

This is how my code looks now,

CustomerCare.ScheduleData = new Ext.data.JsonStore({
storeId: 'ScheduleData',
url: 'http://localhost/testData/calendarUpdate.json',
idProperty: 'schedules',
listeners: {
'load': function() {

// Sort the data that we get from the service
CustomerCare.ScheduleData.getAt(0).data.scheduleList =
CustomerCare.ScheduleData.getAt(0).data.scheduleList.sort(scheduleSort);
},
'success': function(){

}
},
fields: [
{
name: 'hashCode',
mapping:'schedules.hashCode'
},
{
name: 'scheduleList',
mapping: 'schedules.scheduleList'
}
]
});

var scheduleSort = function(sch1, sch2) {
return sch1.timeStamp - sch2.timeStamp;
};


Is there a way to have ext.js do the sorting though?