PDA

View Full Version : How to define writer config of a proxy of a Model?



senchatized
18 Feb 2014, 3:01 AM
In ExtJs4.2. How do I define the writer config of a proxy of a model in such a way that it will send the fields/params like /saveConfig?id=someId&field1=someVal&field2=someVal&field3=someVal



Ext.define('Form.model.Config', {
extend : 'Ext.data.Model',
fields : [{
name : 'field1',
type : 'string'
}, {
name : 'field2',
type : 'int'
}, {
name : 'field3',
type : 'int'
}],
idProperty : 'id',

proxy : {
type : 'ajax',
api : {
update : Global.context+'saveConfig'
},
writer : {

}
}
});

mitchellsimoens
3 Mar 2014, 7:49 AM
So you want it as a GET request?

senchatized
10 Mar 2014, 1:57 AM
Not actually.. but i have something like this in my server-side (groovy):


String field1 = params.get("field1"),
String field2 = params.get("field2"),
String field3 = params.get("field3"),

String value = <sometransformation on field1,2,3...>

anotherService.saveMe(params.long("id"), value)


I get null values from params.get("field1/2/3") and params.long("id").

I resolved it by doing something like:


writer : {
type: 'base',
writeAllFields: true,
writeRecords: function(request, data) {
if (data.length === 1) {
// convert to single object format
data = data[0];
}

for(var param in data) {
request.params[param] = data[param];
}

return request;
}
}

I copied this from some sencha framework code.

How should this be done correctly for me to get the values in server-side?

skirtle
10 Mar 2014, 2:20 PM
Writers don't support this by default. I suspect this is because it doesn't generalize well for saving multiple records at once.

Your workaround looks pretty good to me.

Personally I'd pull the logic out to a custom writer class to make it easier to re-use. I'd probably extend Json writer rather than the base Writer and then move the jsonData to the params in the writeRecords override. It's potentially a little tidier than your override but not by much.