Coin_op
Coin_op

Reputation: 10718

Extjs - Best way to iterate through displayed records in a buffered store

So, I'm upgrading from EXT 4.1.1a to 4.2.2 and have come across a problem with buffered stores. In 4.1.1a I could use store.each to iterate through the currently displayed store records, but in 4.2.2 I simply get the error:

TypeError: Cannot read property 'length' of undefined

Basically inside the store object the data property does not have an items property anymore, and the each method uses the length property of the items, hence the error. Instead the items in the store seem to reside in data.map. I could loop through data.map but it seems there should be a better way. The docs only mention store.each as the way to do this even though this seems to fail for buffered stores.

I'm iterating through the store on the refresh listener attached to the grids view.

Any help with this would be much appreciated

Upvotes: 5

Views: 7181

Answers (1)

ziGi
ziGi

Reputation: 848

Apparently they think you can't iterate over the store because it has "sparse" data, but that is not true. Currently, what you could do is the following.

if(store.buffered) {
    // forEach is private and part of the private PageMap
    store.data.forEach(function(record, recordIdx) {
        /* Do stuff with the record here */
    }, this);
} else {
    store.each(function(record) {
        /* Do the same stuff I guess */
    }, this);
}

IMPORTANT

Take care that can change the structure of the store in the future which will surely brake your code.

Additionally, I strongly believe that if proper design patterns were used, each had to take care of the looping without caring about the structure.

OPTIMIZATION

What I usually do, when I initialize the store is the following:

if(store.buffered) {
    store.iterate = store.data.forEach;
} else {
    store.iterate = store.each;
}

Then you could just use it like this:

store.iterate(fn, scope);

This is not the best decision but simplifies writing a lot of if-statements

Upvotes: 8

Related Questions