找到了解决方案(尽管仍然愿意听取其他想法)。
首先,您可以使用将传递给操作的配置对象来调用商店的 load() 函数。 Ext.data.Operation 的 API 文档清楚地表明,其中一个配置选项是针对 Filter 对象数组的,因此您可以这样做:
var idFilter = Ext.create('Ext.util.Filter', {
property: 'id',
value: '100,200,300'
});
myStore.load({
filters: [ idFilter ]
});
这会导致 URL 查询字符串包含 ?filter=[{"property"%3Aid%2C"value"%3A100,200,300}](换言之,[{ property: 'id', value: '100,200,300'}] 的 URL 编码版本)的请求。
您也可以直接致电myStore.filter('id', '100,200,300'),而无需先致电.load()。假设您的商店中有 remoteFilter=true,这将使用之前显示的相同查询参数发出请求。
旁注:您可以通过为代理配置“filterParam”配置选项来更改用于“过滤器”的关键字。例如,如果 filterParam=q,那么上面显示的查询字符串将更改为:?q=[{"property"%3Aid%2C"value"%3A100,200,300}]
第二,您可以在查询字符串中控制过滤器的“结构”。就我而言,我不想要 filter={JSON} 之类的东西,如上所示。我想要一个看起来像这样的查询字符串:?id=100,200,300
为此,我需要扩展一个代理并覆盖默认的 getParams() 函数:
Ext.define('myapp.MyRestProxy', {
extend: 'Ext.data.proxy.Rest',
/**
* Override the default getParams() function inherited from Ext.data.proxy.Server.
*
* Note that the object returned by this function will eventually be used by
* Ext.data.Connection.setOptions() to include these parameters via URL
* querystring (if the request is GET) or via HTTP POST body. In either case,
* the object will be converted into one, big, URL-encoded querystring in
* Ext.data.Connection.setOptions() by a call to Ext.Object.toQueryString.
*
* @param {Ext.data.Operation} operation
* @return {Object}
* where keys are request parameter names mapped to values
*/
getParams: function(operation) {
// First call our parent's getParams() function to get a default array
// of parameters (for more info see http://bit.ly/vq4OOl).
var paramsArr = this.callParent(arguments),
paramName,
length;
// If the operation has filters, we'll customize the params array before
// returning it.
if( operation.filters ) {
// Delete whatever filter param the parent getParams() function made
// so that it won't show up in the request querystring.
delete paramsArr[this.filterParam];
// Iterate over array of Ext.util.Filter instances and add each
// filter name/value pair to the array of request params.
for (var i = 0; i < operation.filters.length; i++) {
queryParamName = operation.filters[i].property;
// If one of the query parameter names (from the filter) conflicts
// with an existing parameter name set by the default getParams()
// function, throw an error; this is unacceptable and could cause
// problems that would be hard to debug, otherwise.
if( paramsArr[ queryParamName ] ) {
throw new Error('The operation already has a parameter named "'+paramName+'"');
}
paramsArr[ queryParamName ] = operation.filters[i].value;
}
}
return paramsArr;
}
});