Skip to content

Commit 4daace7

Browse files
committed
request params built like server.proxy
1 parent 9566037 commit 4daace7

1 file changed

Lines changed: 190 additions & 45 deletions

File tree

WebSocket.js

Lines changed: 190 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,85 @@ Ext.define('Ext.ux.data.proxy.WebSocket', {
8686
*/
8787
url: '',
8888

89+
/**
90+
* @cfg {String} [pageParam="page"]
91+
* The name of the 'page' parameter to send in a request. Defaults to 'page'. Set this to `''` if you don't
92+
* want to send a page parameter.
93+
*/
94+
pageParam: 'page',
95+
96+
/**
97+
* @cfg {String} [startParam="start"]
98+
* The name of the 'start' parameter to send in a request. Defaults to 'start'. Set this to `''` if you don't
99+
* want to send a start parameter.
100+
*/
101+
startParam: 'start',
102+
103+
/**
104+
* @cfg {String} [limitParam="limit"]
105+
* The name of the 'limit' parameter to send in a request. Defaults to 'limit'. Set this to `''` if you don't
106+
* want to send a limit parameter.
107+
*/
108+
limitParam: 'limit',
109+
110+
/**
111+
* @cfg {String} [groupParam="group"]
112+
* The name of the 'group' parameter to send in a request. Defaults to 'group'. Set this to `''` if you don't
113+
* want to send a group parameter.
114+
*/
115+
groupParam: 'group',
116+
117+
/**
118+
* @cfg {String} [groupDirectionParam="groupDir"]
119+
* The name of the direction parameter to send in a request. **This is only used when simpleGroupMode is set to
120+
* true.**
121+
*/
122+
groupDirectionParam: 'groupDir',
123+
124+
/**
125+
* @cfg {String} [sortParam="sort"]
126+
* The name of the 'sort' parameter to send in a request. Defaults to 'sort'. Set this to `''` if you don't
127+
* want to send a sort parameter.
128+
*/
129+
sortParam: 'sort',
130+
131+
/**
132+
* @cfg {String} [filterParam="filter"]
133+
* The name of the 'filter' parameter to send in a request. Defaults to 'filter'. Set this to `''` if you don't
134+
* want to send a filter parameter.
135+
*/
136+
filterParam: 'filter',
137+
138+
/**
139+
* @cfg {String} [directionParam="dir"]
140+
* The name of the direction parameter to send in a request. **This is only used when simpleSortMode is set to
141+
* true.**
142+
*/
143+
directionParam: 'dir',
144+
145+
/**
146+
* @cfg {Boolean} [simpleSortMode=false]
147+
* Enabling simpleSortMode in conjunction with remoteSort will only send one sort property and a direction when a
148+
* remote sort is requested. The {@link #directionParam} and {@link #sortParam} will be sent with the property name
149+
* and either 'ASC' or 'DESC'.
150+
*/
151+
simpleSortMode: false,
152+
153+
/**
154+
* @cfg {Boolean} [simpleGroupMode=false]
155+
* Enabling simpleGroupMode in conjunction with remoteGroup will only send one group property and a direction when a
156+
* remote group is requested. The {@link #groupDirectionParam} and {@link #groupParam} will be sent with the property name and either 'ASC'
157+
* or 'DESC'.
158+
*/
159+
simpleGroupMode: false,
160+
161+
/**
162+
* @cfg {Object} extraParams
163+
* Extra parameters that will be included on every request. Individual requests with params of the same name
164+
* will override these params when they are in conflict.
165+
*/
166+
extraParams: {},
167+
89168
/**
90169
* @cfg {String} protocol The protocol to use in the connection
91170
*/
@@ -237,6 +316,113 @@ Ext.define('Ext.ux.data.proxy.WebSocket', {
237316
return me;
238317
},
239318

319+
/**
320+
* Encodes the array of {@link Ext.util.Sorter} objects into a string to be sent in the request url. By default,
321+
* this simply JSON-encodes the sorter data
322+
* @param {Ext.util.Sorter[]} sorters The array of {@link Ext.util.Sorter Sorter} objects
323+
* @param {Boolean} [preventArray=false] Prevents the items from being output as an array.
324+
* @return {String} The encoded sorters
325+
*/
326+
encodeSorters: function (sorters, preventArray) {
327+
var out = [],
328+
length = sorters.length,
329+
i;
330+
331+
for (i = 0; i < length; i++) {
332+
out[i] = sorters[i].serialize();
333+
}
334+
335+
return Ext.encode(preventArray ? out[0] : out);
336+
},
337+
338+
/**
339+
* Encodes the array of {@link Ext.util.Filter} objects into a string to be sent in the request url. By default,
340+
* this simply JSON-encodes the filter data
341+
* @param {Ext.util.Filter[]} filters The array of {@link Ext.util.Filter Filter} objects
342+
* @return {String} The encoded filters
343+
*/
344+
encodeFilters: function (filters) {
345+
var out = [],
346+
length = filters.length,
347+
i, op;
348+
349+
for (i = 0; i < length; i++) {
350+
out[i] = filters[i].serialize();
351+
}
352+
353+
return Ext.encode(out);
354+
},
355+
356+
/**
357+
* @private
358+
* Copy any sorters, filters etc into the params so they can be sent over the wire
359+
*/
360+
getParams: function (operation) {
361+
var me = this,
362+
params = {},
363+
grouper = operation.getGrouper(),
364+
sorters = operation.getSorters(),
365+
filters = operation.getFilters(),
366+
page = operation.getPage(),
367+
start = operation.getStart(),
368+
limit = operation.getLimit(),
369+
simpleSortMode = me.getSimpleSortMode(),
370+
simpleGroupMode = me.getSimpleGroupMode(),
371+
pageParam = me.getPageParam(),
372+
startParam = me.getStartParam(),
373+
limitParam = me.getLimitParam(),
374+
groupParam = me.getGroupParam(),
375+
groupDirectionParam = me.getGroupDirectionParam(),
376+
sortParam = me.getSortParam(),
377+
filterParam = me.getFilterParam(),
378+
directionParam = me.getDirectionParam(),
379+
hasGroups, index;
380+
381+
if (pageParam && page) {
382+
params[pageParam] = page;
383+
}
384+
385+
if (startParam && (start || start === 0)) {
386+
params[startParam] = start;
387+
}
388+
389+
if (limitParam && limit) {
390+
params[limitParam] = limit;
391+
}
392+
393+
hasGroups = groupParam && grouper;
394+
if (hasGroups) {
395+
// Grouper is a subclass of sorter, so we can just use the sorter method
396+
if (simpleGroupMode) {
397+
params[groupParam] = grouper.getProperty();
398+
params[groupDirectionParam] = grouper.getDirection();
399+
} else {
400+
params[groupParam] = me.encodeSorters([grouper], true);
401+
}
402+
}
403+
404+
if (sortParam && sorters && sorters.length > 0) {
405+
if (simpleSortMode) {
406+
index = 0;
407+
// Group will be included in sorters, so grab the next one
408+
if (sorters.length > 1 && hasGroups) {
409+
index = 1;
410+
}
411+
params[sortParam] = sorters[index].getProperty();
412+
params[directionParam] = sorters[index].getDirection();
413+
} else {
414+
params[sortParam] = me.encodeSorters(sorters);
415+
}
416+
417+
}
418+
419+
if (filterParam && filters && filters.length > 0) {
420+
params[filterParam] = me.encodeFilters(filters);
421+
}
422+
423+
return params;
424+
},
425+
240426
/**
241427
* @method create
242428
* Starts a new CREATE operation (pull)
@@ -295,53 +481,12 @@ Ext.define('Ext.ux.data.proxy.WebSocket', {
295481

296482
// Treats 'read' as a string event, with no data inside
297483
if (action === me.getApi().read) {
298-
var extraParams = operation._proxy.extraParams,
299-
queryParams = operation._params,
300-
sorters = operation._sorters ,
301-
groupers = operation._groupers;
302-
303-
// extraParams
304-
for (var key in extraParams) {
305-
if (extraParams.hasOwnProperty(key)) {
306-
data[key] = extraParams[key];
307-
}
308-
}
484+
var initialParams = Ext.apply({}, operation.getParams());
309485

310-
// Query parameter if read request come from combo field
311-
for (var key in queryParams){
312-
if (queryParams.hasOwnProperty(key)) {
313-
data[key] = queryParams[key];
314-
}
315-
}
316-
317-
// Remote sorters
318-
if (sorters && sorters.length > 0) {
319-
data.sort = [];
320-
321-
for (i = 0; i < sorters.length; i++) {
322-
data.sort.push({
323-
property: sorters[i]._property,
324-
direction: sorters[i]._direction
325-
});
326-
}
327-
}
328-
329-
// Remote groupers
330-
if (groupers && groupers.length > 0) {
331-
data.group = [];
332-
333-
for (i = 0; i < groupers.length; i++) {
334-
data.group.push({
335-
property: groupers[i]._property,
336-
direction: groupers[i]._direction
337-
});
338-
}
339-
}
486+
data = Ext.applyIf(initialParams, me.getExtraParams() || {});
340487

341-
// Paging params
342-
data.page = operation._page;
343-
data.limit = operation._limit;
344-
data.start = operation._start;
488+
// copy any sorters, filters etc into the params so they can be sent over the wire
489+
Ext.applyIf(data, me.getParams(operation));
345490
}
346491
// Create, Update, Destroy
347492
else {

0 commit comments

Comments
 (0)