PDA

View Full Version : [4.0.2] Grid'store - extraParams always sent via GET with CRUD actions



eleftrik
6 Aug 2011, 1:08 AM
When a grid makes an Ajax request about CRUD, for example updating a record, 'extraParams' (defined in the store's grid) is always sent via GET.

When reading the store the are no problem, extraParams are sent via POST (correct).

In Ext 3.x it was possibile to sent params via POST.

Nickname
7 Aug 2011, 6:05 AM
Can you provide some code?

I tried to reproduce this, but my RowEditor keep using POST only with the DC param in the URL.

Here is the store definition I use

Ext.define('Webdesktop.store.administration.Groups', {
extend : 'Ext.data.Store',
model : 'Webdesktop.model.administration.Group',
autoLoad : true,
proxy : {
type : 'ajax',
extraParams: {
demo: 123
},
api : {
read : '_module/administration/_action/loadGroupGroups',
create : '_module/administration/_action/saveNewGroup',
update : '_module/administration/_action/saveEditGroup'
},
reader : {
type : 'json',
root : 'groups'
},
writer : {
type : 'json',
root : 'groups',
encode : true
}
}
});

From FireBug:
POST /noc/webdesktop/api/request/_module/administration/_action/saveEditGroup?_dc=1312725371172


POST Parameter (application/x-www-form-urlencoded)
demo 123
groups {"id":3,"name":"Team b","description":"The Team","memberscount":2}

eleftrik
7 Aug 2011, 7:18 AM
Dear Nickname, thank you very much!

I didn't know that the encode property existed, it was missing in my writer sections, and this caused 2 problems:
- extraParams always sent via GET
- update and insert, for a example in an edit grid, always sent data via PUT, so I had to access it via php://input (according to some official example)

In my defence I can say that official examples never used the encode property, so everything was more complicated to handle (especially with php://input).

I apologize for opening a bug, but looking at the examples it was hard to understand that encode (which is set to FALSE by default!) is so important.

Thanks, you solved 2 problems of mine :)

eleftrik
8 Aug 2011, 12:31 AM
POST Parameter (application/x-www-form-urlencoded)
demo 123
groups {"id":3,"name":"Team b","description":"The Team","memberscount":2}

I have noticed that 'group' is sent via POST as a string (so I have to json_decode it server side).
Is there to send every single property as separate POST variable? If I remember well, ExtJs 3.x sent variables this way.

Thanks again.

Nickname
8 Aug 2011, 12:46 AM
Read the docs from Ext.data.Writer.Json


nameProperty (http://docs.sencha.com/ext-js/4-0/#/api/-cfg-nameProperty) : StringThis property is used to read the key for each value that will be sent to the server.



this could do the trick

Nickname
8 Aug 2011, 12:55 AM
Pardon me, this won't do it. Wrong interpretation of the docs.

Hmm, no idea at the moment. Perhaps you write your own writer or play with the other config options from the JsonWriter.

svb
8 Aug 2011, 3:13 AM
Hi,

with the encode set to true the whole body is apparently encoded with URLEncode. The thing is I am using a Json writer, so I'd like everything to be Json-encoded and not additionally URL-encoded. Is it possible to get a result like

{demo: 123, groups: {"id":3,"name":"Team b","description":"The Team","memberscount":2} }

?

Thanks

eleftrik
8 Aug 2011, 7:23 AM
Pardon me, this won't do it. Wrong interpretation of the docs.

Hmm, no idea at the moment. Perhaps you write your own writer or play with the other config options from the JsonWriter.

Thanks for your reply. I don't understand why Ext 4 made it so "complicated"...

Nickname
8 Aug 2011, 8:02 AM
I know I shouldn't do this, because for me it looks like really bad practice, but here you go. A first try. No support or any kind of "why do several other things not work anymore"!

Version as an UX:



Ext.define('Ext.ux.data.writer.JsonSingleParams', {
extend: 'Ext.data.writer.Json',
alias: 'writer.jsonSingleParams',

/**
* @cfg {Boolean} useSingleParams new param to really enable single params
*/
useSingleParams: false,

writeRecords: function(request, data) {
var root = this.root;

if (this.allowSingle && data.length == 1) {
// convert to single object format
data = data[0];
}

if (this.encode) {
if (root) {
// sending as a param, need to encode
request.params[root] = Ext.encode(data);
} else if(this.useSingleParams && this.allowSingle) {
// single params makes only sens, if we have 1 records (check for allowSingle),
// otherwise we would overwrite with each record the last one
Ext.iterate(data, function(k, v) {
request.params[k] = Ext.encode(v);
}, this);
} else {
//<debug>
Ext.Error.raise('Must specify a root when using encode');
//</debug>
}
} else {
// send as jsonData
request.jsonData = request.jsonData || {};
if (root) {
request.jsonData[root] = data;
} else {
request.jsonData = data;
}
}
return request;
}
});

to use this load your file properly and config your writer as


writer: {
type: 'jsonSingleParams',
encode: true,
allowSingle: true,
useSingleParams: true
}


I didn't test it