例如,假设我有一个用于加载人员的服务器API,该API处理这样的请求: GET / people /?id=101,329,27
我想构建一个Store (可能是扩展Ext.data.Store的自定义类)--假设它有一个人员is列表--它会使代理发出像上面所示的请求,这样返回的数据就只能用于这个人的子集。
我看到了有关远程过滤的文档,但我担心的是,要使用它,我首先需要调用store.load()来加载所有人员,然后调用filter()来执行远程过滤。我只想第一次加载人的子集。
谢谢你的建议!
发布于 2011-12-21 19:33:24
找到了一个解决方案(尽管仍然可以听到其他的想法)。
首先,可以使用将传递给操作的config对象调用存储的load()函数。Ext.data.Operation的API文档清楚地表明,配置选项之一是用于筛选器对象的数组,因此可以这样做:
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,这将使用相同的查询参数发出请求。
Sidenote:您可以通过为代理配置“filterParam”配置选项来更改用于“filter”的关键字。例如,如果是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;
}
});发布于 2011-12-21 23:22:37
还可以让Model对象加载自身的记录。在控制器中,您可以:
this.getRequestModel().load(requestID,{ //load from server (async)
success: function(record, operation) {
.....
}
}其中,请求是一个模型类,而requestID是一个查找的ID。在这个场景中,Model对象也需要定义代理:
proxy: {
type: 'ajax',
reader: {
type:'json',
root: 'data'
},
api: {
create : 'request/create.json', //does not persist
read : 'request/show.json'
}
}https://stackoverflow.com/questions/8591604
复制相似问题