ExtJS 4:如何配置存储来加载特定ID集的模型?

ExtJS 4:如何配置存储来加载特定ID集的模型?,extjs,extjs4,Extjs,Extjs4,例如,假设我有一个服务器API,用于加载处理如下请求的人员:GET/people/?id=101329,27 我想构建一个存储(可能是一个扩展Ext.data.Store的自定义类),假设它有一个人员ID列表,它会导致代理发出如上所示的请求,以便返回的数据仅用于该人员子集 我看到了有关远程筛选的文档,但我担心的是,要使用它,我首先需要调用store.load()来加载所有人员,然后调用filter()来执行远程筛选。我想第一次加载人员子集 谢谢你的建议 找到了一个解决方案(尽管仍然愿意听取其他想

例如,假设我有一个服务器API,用于加载处理如下请求的人员:GET/people/?id=101329,27

我想构建一个存储(可能是一个扩展Ext.data.Store的自定义类),假设它有一个人员ID列表,它会导致代理发出如上所示的请求,以便返回的数据仅用于该人员子集

我看到了有关远程筛选的文档,但我担心的是,要使用它,我首先需要调用store.load()来加载所有人员,然后调用filter()来执行远程筛选。我想第一次加载人员子集

谢谢你的建议

找到了一个解决方案(尽管仍然愿意听取其他想法)

首先,可以使用将传递给操作的配置对象调用存储的load()函数。Ext.data.Operation的API文档清楚地表明,其中一个配置选项用于过滤器对象数组,因此您可以执行以下操作:

var idFilter = Ext.create('Ext.util.Filter', {
  property: 'id',
  value: '100,200,300'
});

myStore.load({
  filters: [ idFilter ]
});
这会导致一个请求,其中URL查询字符串包含
?筛选器=[{“属性”%3Aid%2C“值”%3a100300300}]
(换句话说,是
[{property:'id',value:'100200300}]的URL编码版本

您也可以只调用
myStore.filter('id','100200300')
,而不必先调用
.load()
。假设您的存储中有remoteFilter=true,这将使用前面显示的相同查询参数发出请求

旁注:您可以通过为代理配置“filterParam”配置选项来更改用于“filter”的关键字。例如,如果filterParam=q,则上面显示的查询字符串将更改为:
?q=[{“属性”%3Aid%2C“值”%3a100300300}]

第二步,您可以控制查询字符串中过滤器的“结构”。在我的例子中,我不想要像filter={JSON}这样的东西,如上所示。我想要一个类似这样的查询字符串:
?id=100200300
为此,我需要扩展一个代理并重写默认的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;
    }
});
Ext.define('myapp.MyRestProxy'{
扩展:“Ext.data.proxy.Rest”,
/**
*重写从Ext.data.proxy.Server继承的默认getParams()函数。
*
*请注意,此函数返回的对象最终将由
*Ext.data.Connection.setOptions()通过URL包含这些参数
*querystring(如果请求是GET)或通过HTTP POST正文。在这两种情况下,
*该对象将在中转换为一个大的URL编码查询字符串
*Ext.data.Connection.setOptions()通过调用Ext.Object.toQueryString。
* 
*@param{Ext.data.Operation}操作
*@return{Object}
*其中键是映射到值的请求参数名称
*/
getParams:函数(操作){
//首先调用父级的getParams()函数以获取默认数组
//参数(有关更多信息,请参阅http://bit.ly/vq4OOl).
var paramsArr=this.callParent(参数),
参数名,
长度;
//如果操作有筛选器,我们将在之前自定义params数组
//还它。
if(operation.filters){
//删除父getParams()函数生成的任何筛选器参数
//这样它就不会出现在请求查询字符串中。
删除paramsArr[this.filterParam];
//迭代Ext.util.Filter实例数组并添加每个实例
//请求参数数组的筛选器名称/值对。
对于(var i=0;i
您还可以让模型对象加载自身的记录。通过控制器,您可以执行以下操作:

this.getRequestModel().load(requestID,{       //load from server (async)
       success: function(record, operation) {
       .....
       }
}
其中Request是一个模型类,requestID是一个要查找的ID。在此场景中,模型对象也需要定义代理:

proxy: {
        type: 'ajax',
        reader: {
           type:'json',
           root: 'data'
        },
        api: {
            create : 'request/create.json', //does not persist
            read   : 'request/show.json'
       }
    }