View Full Version : What's the best way to walk the records in a Store?

17 Sep 2010, 11:08 AM
I have a DirectStore that loads my data fine, but I want to compute some extra fields on the client side before the records get displayed. I've added a "load" listener, and that gets called, but for some reason when I do the "for (i in recs)" I get one extra index which is set to "remove".
Any idea what's going on here?
is there a better construct than "for (i in recs)"?

When I dump recs.length i get 2.

load: function(store, recs)
console.info ("in load listener count=", store.getCount());
console.info ("in load listener recs=", recs.length);
for (i in recs)
console.info (" i = ", i);
console.info ("rec[i]", recs[i]);
var data = recs[i].data;
console.info ("data", data);
}here's the firebug output:

in load listener count= 2
in load listener recs= 2
i = 0
rec[i] Object { phantom=true, more...}
data Object { id=0, more...}
i = 1
rec[i] Object { phantom=true, more...}
data Object { id=0, more...}
i = remove
rec[i] function()
data undefined

17 Sep 2010, 11:18 AM
Have you looked at the Ext.data.Store "each" method. It allows you to call a function on each record of the store.

17 Sep 2010, 12:20 PM
Ah, I thought I had read about something like that. I'm still curious about the weird for loop output.
Anyway I discovered field converters are the way to do the job I was trying to do with the load listener.

17 Sep 2010, 12:25 PM
It's not weird.

You do not use "for...in" on an Array.

17 Sep 2010, 12:40 PM
I've certainly seen it documented for use on an array:


and there was no error.

17 Sep 2010, 2:00 PM
That actually says "for...in...OBJECT"

Not an array. You do not use for...in on arrays.

17 Sep 2010, 2:01 PM
But the example they show using an Array is just wrong, and propagating problems.

Use a integer for loop.

17 Sep 2010, 2:11 PM
every store has each

store.each(function(record) {

17 Sep 2010, 6:50 PM
You can use :
"Ext.each(array, function(_item){});"