View Full Version : Best way to remove some (more than 1 but less than all) records from a store

5 Feb 2013, 11:13 AM
We encounter this need often, but can never come up with the an elegant solution.

Say we have a store with a bunch of records, that share some common values (for our example, let's say they were addresses and the shared value was state). We need to remove a number of these records from the store, where the number of records to remove is more than one, but less than all.

On the surface, it would seem this should work:

store.each( function(rec) ) {
if (rec.get('state') === 'NJ') {

BUT it does not :(

Modifying the store while iterating it is clearly not a good thing, and usually tosses some error about the index being out of range.

Each time we encounter this need in our projects, we usually spend a few minutes (to sometimes and hour) surfing the web, google & the Sencha forums trying to see what the best way is to handle this "elegantly". But the search semtantics of "store, each, remove" defies all attempts at finding matching queries.

So we usually fall back to something like this:

for(var i=store.count(); i > -1; i--) {
var rec = store.getAt(i);
if (rec.get('state') === 'NJ') {

It always feels like there must be a better way. So I thought I'd ask -- is there?

Tim Toady
5 Feb 2013, 8:45 PM
Put all the matching records in an array and call store.remove( records )

5 Feb 2013, 9:16 PM
This work?

var matches = store.query('state', 'NJ').getRange();

// could combine 'em
// store.remove(store.query('state', 'NJ').getRange());

6 Feb 2013, 7:32 AM
Very nice! Thanks.