PDA

View Full Version : JsonReader: What if server does not provide 'totalProperty'?



MatteoSp
18 Sep 2009, 4:32 PM
I'm using a JsonStore and, therefore, a JsonReader.

My server is not able to provide the total record count with the response that contains data. It can only provide such info with a second (specific) call, but: how can I manually set this information to JsonReader?

thanks
m.

httpdotcom
19 Sep 2009, 4:45 AM
I assume you are needing totalProperty to do pagination, and without it, pagination will not work. You may need to write a custom script/controller to make the two calls (one for totalproperty and one for the data) and put the information together and return it to your script.

MatteoSp
19 Sep 2009, 5:28 AM
thanks, can you give me just an idea of how can I made the store/reader catch data from my new script instead of directly call my server?

thanks again
m.

hendricd
19 Sep 2009, 6:03 AM
thanks, can you give me just an idea of how can I made the store/reader catch data from my new script instead of directly call my server?

thanks again
m.

@MatteoSp -- You could try overriding the read method of your JSON Reader Instance to handle it. Try something like:

Abbreviated:


myStore = new Ext.data.Store({
reader : new Ext.data.JsonReader (
{
totalProperty : 'recordCount', //or other
root : 'rows',
....
}, recordType),
.....
});

myStore.reader.read = function(response){
var json = response.responseText;
var o = Ext.decode(json);
if(!o) {
throw {message: 'JsonReader.read: Json object not found'};
}

//Optional -- Might want to handle metaData changes (if any) first, then evaluate lengths
if(o.metaData){
this.onMetaChange(o.metaData);
delete o.metaData;
}

var m = this.meta;
if(m.totalProperty){
o[m.totalProperty] || (o[m.totalProperty] = this.getRoot(o).length);
}
return this.readRecords(o);
};
Tweak it. ;)

MatteoSp
19 Sep 2009, 6:50 AM
wonderful, thank you very much. I really appreciate your help

m.