PDA

View Full Version : How to disable JSON-encoding proxy query params?



webleaf
28 Sep 2011, 7:14 AM
Proxy send data as JSON-encoded string param.
For example:

[{"property":"order_id","value":5}] (for filter)

or

[{"property":"date","direction":"DESC"}] (for sort)

How to disable it for sending params as raw arrays? Because, i don't want use json_decode() on server-side.

stevil
28 Sep 2011, 9:46 AM
Proxy send data as JSON-encoded string param.
For example:

[{"property":"order_id","value":5}] (for filter)

or

[{"property":"date","direction":"DESC"}] (for sort)

How to disable it for sending params as raw arrays? Because, i don't want use json_decode() on server-side.

Because I wanted sorting and filtering to work the way they did before 4.0, I did this:




Ext.override(Ext.data.proxy.Server, {
getParams: function (operation) {
var me = this,
params = {},
isDef = Ext.isDefined,
groupers = operation.groupers,
sorters = operation.sorters,
filters = operation.filters,
page = operation.page,
start = operation.start,
limit = operation.limit,


simpleSortMode = me.simpleSortMode,


pageParam = me.pageParam,
startParam = me.startParam,
limitParam = me.limitParam,
groupParam = me.groupParam,
sortParam = me.sortParam,
filterParam = me.filterParam,
directionParam = me.directionParam;


if (pageParam && isDef(page)) {
params[pageParam] = page;
}


if (startParam && isDef(start)) {
params[startParam] = start;
}


if (limitParam && isDef(limit)) {
params[limitParam] = limit;
}


if (groupParam && groupers && groupers.length > 0) {


params[groupParam] = me.encodeSorters(groupers);
}


if (sortParam && sorters && sorters.length > 0) {
if (simpleSortMode) {
params[sortParam] = sorters[0].property;
params[directionParam] = sorters[0].direction;
} else {
me.encodeSorters(params, sorters);
}


}


if (filterParam && filters && filters.length > 0) {
// override to support encoding onto the params object itself
me.encodeFilters(params, filters, 0);
}


// override to allow us to put "static" (non-user-changeable filters)
// on the request
if (filterParam && me.staticFilters && me.staticFilters.length > 0) {
// override to support encoding onto the params object itself
me.encodeFilters(params, me.staticFilters, filters ? filters.length : 0);
}


return params;
},




encodeFilters: function (params, filters, startAt) {
var length = filters.length,
i = startAt;


for (; i < length; i++) {
params['filter[' + i + '][field]'] = filters[i].property;
params['filter[' + i + '][data][type]'] = 'string';
params['filter[' + i + '][data][comparison]'] = 'contains';
params['filter[' + i + '][data][value]'] = Ext.htmlEncode(filters[i].value);
}
},


encodeSorters: function (params, sorters) {
var min = '',
length = sorters.length,
i = 0;


for (; i < length; i++) {
min += (i > 0 ? ',' : '') + sorters[i].property + '#' + sorters[i].direction;
}


params["sort"] = min;
}
});





Forgive the formatting of the code - pasted from VS to Notepad and then here, and it won't leave the line breaks alone!

preaction
28 Sep 2011, 2:44 PM
There's an example of this in the docs at http://docs.sencha.com/ext-js/4-0/#!/api/Ext.data.proxy.Ajax (http://docs.sencha.com/ext-js/4-0/#%21/api/Ext.data.proxy.Ajax)

Basically, you provide your own encodeFilters and encodeSorters functions to do what you want.

alecm
13 Aug 2014, 9:50 PM
This thread is pretty old. But I want to give answer on it anyway.
There is config option in Ext.data.proxy.Ajax class: sortParam. Set it to undefined or false and it's done.
Here is example from my code:

proxy: {
pageParam: false,
startParam: false,
limitParam: false,
filterParam: false,
sortParam: false,
noCache: false,
type: 'ajax',
.....
}