ExtJS 4:如何配置一个store以加载特定一组ID的model?

6
例如,假设我有一个用于加载人员的服务器 API,处理像这样的请求:GET/people/?id=101,329,27。
我想构建一个存储区 (可能是扩展 Ext.data.Store 的自定义类),假设它具有人员 ID 列表,会导致代理发出如上所示的请求,以便返回的数据仅适用于该子集的人员。
我看到了关于远程过滤的文档,但我的担忧是,为了使用它,我首先需要调用 store.load() 来加载所有人员,然后调用 filter() 来进行远程过滤。我只想在第一次加载子集的人员。
感谢任何建议!
2个回答

5

找到了一个解决办法(虽然还听取其他建议)。

首先,您可以使用配置对象调用store的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,则这将使用与上述相同的查询参数进行请求。
附注:您可以通过为代理配置“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;
    }
});

1

您也可以让您的模型对象加载记录自身。从控制器中,您可以执行以下操作:

this.getRequestModel().load(requestID,{       //load from server (async)
       success: function(record, operation) {
       .....
       }
}

在这里,Request 是一个模型类,requestID 是要查找的 ID。在这种情况下,Model 对象也需要定义代理:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接