PDA

View Full Version : How can I configure a store to load models for a specific set of IDs?



charris
21 Dec 2011, 6:42 AM
For example, say I have a server API for loading people that handles requests like this: GET /people/?id=101,329,27

I'd like to build a Store (probably a custom class that extends Ext.data.Store) which--assuming it has a list of people IDs--causes the proxy to make a request like the one shown above so that the returned data is only for that subset of persons.

I saw the documentation regarding remote filtering, but my concern is that to use it I would first need to call store.load() which would load *all* persons, then call filter() to do remote filtering. I'd like to just load the subset of persons the first time.

Thanks for any advice!

charris
21 Dec 2011, 8:19 AM
Found a solution (although still open to hearing other ideas).

First, you can call a store's load() function with a config object that will be passed to an operation. The API docs for Ext.data.Operation make it clear that one of the config options is for an array of Filter objects, so you can do this:

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

myStore.load({
filters: [ idFilter ]
});

This results in a request where the URL querystring contains
?filter=[{"property"%3Aid%2C"value"%3A100,200,300}] (in other words, a URL-encoded version of [{ property: 'id', value: '100,200,300'}]).

You can also just call myStore.filter('id', '100,200,300'); without having called .load() first. Assuming you have remoteFilter=true in your store, this will make a request with the same query params shown agove.

Sidenote: you can change the keyword used for 'filter' by configuring the 'filterParam' config option for the proxy. For example, if filterParam=q, then the querystring shown above changes to:
?q=[{"property"%3Aid%2C"value"%3A100,200,300}]

Second, you can control "structure" of the filter in the querystring. In my case, I didn't want something like filter={JSON}, as shown above. I wanted a querystring that looked like this:
?id=100,200,300
For this I needed to extend a proxy and override the default getParams() function:

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 "'+queryParamName+'"');
}

paramsArr[ queryParamName ] = operation.filters[i].value;
}
}

return paramsArr;
}
});