PDA

View Full Version : custom JSON format at POST



bryanevil
16 Jun 2010, 8:01 PM
Hi all

I am new to extjs.

In a grid, I am looking for a way to customize the JSON format at the POST action, e.g. aways return a object format, and aways return in an array. It is because some times it pass an array of int (or array of object):

{"d":[49,30]}

some times it pass only single value (or object):

{"d":31}

It make me difficult to configure my .net web service because look like web service could not convert int to int[] array.

This is my extjs code:



var CompanyColomn = [
{ header: 'ID', width: 200, sortable: true, dataIndex: 'ID' },
{ id: 'CompanyName', header: 'Company Name', width: 400, sortable: true, dataIndex: 'CompanyName', editor: new Ext.form.TextField() }

];
var companyinfoProxy = new Ext.data.HttpProxy({
api:{
// Call web service method using GET syntax
read: '../WebServices/Account/CompanyService.asmx/GetAll',
create: '../WebServices/Account/CompanyService.asmx/Add',
update: '../WebServices/Account/CompanyService.asmx/Update',
destroy: '../WebServices/Account/CompanyService.asmx/Delete'},
// Ask for Json response
headers: { 'Content-type': 'application/json' }
});
var companyinfoReader = new Ext.data.JsonReader({
totalProperty: 'total',
successProperty: 'success',
idProperty: 'ID',
root: 'd',
messageProperty: 'message',
// map to MonthlyReportDTO
fields: ['CompanyName', 'ID']

});
var companyinfoWriter = new Ext.data.JsonWriter({
encode: false,
writeAllFields: true


});
var CompanyInfoJsonStore = new Ext.data.Store({
// Load data at once
autoLoad: true,
// Override default http proxy settings
proxy: companyinfoProxy,
reader: companyinfoReader,
writer: companyinfoWriter,
autoSave:false
});


Bryan

Animal
16 Jun 2010, 8:44 PM
Yes, that's inconsistent.

Use this override:



Ext.override(Ext.data.DataWriter, {
apply : function(params, baseParams, action, rs) {
var data = [],
renderer = action + 'Record';
if (Ext.isArray(rs) || this.listful) {
Ext.each(rs, function(rec){
data.push(this[renderer](rec));
}, this);
}
else if (rs instanceof Ext.data.Record) {
data = this[renderer](rs);
}
this.render(params, baseParams, data);
}
});


And configure your writer with listful: true

@Ext devs: Is that the intention of the listful config?