PDA

View Full Version : Ext.each bug?



jweber
11 Mar 2008, 2:02 PM
I've noticed a couple issues with Ext.each:

- If you pass it a single select element from the DOM, Ext.each iterates over each option in it, rather than treating it as one element. Seems like it should check for these, since they're normally not treated as arrays.

- If you pass it an array of strings, and no context object, it will set the context of each function to the corresponding string. The result is that "this" refers to an array where each element is a character from the string (e.g. this = ["h", "e", "l", "l", "o"]). Is that the expected behavior?

JW

evant
11 Mar 2008, 2:08 PM
1) Could you post a sample?

2) Yes, this is the expected behaviour. If no scope is passed it always defaults to the scope of the element.

jweber
11 Mar 2008, 2:15 PM
1) Sure, here it is, using ext-all 2.0.2:

var ele = Ext.get("A").dom
Ext.each(ele, function(e) {alert(e);});

<select id="A">
<option>a</option>
<option>b</option>
<option>c</option>
</select>

This alerts three times (once for each option), instead of once for the select menu.

2) OK. I guess that's about all you can do, although it seems weird to treat a string as a context object.

hendricd
11 Mar 2008, 2:50 PM
Ext.each is designed to work on arrays (collections):

Try:

var ele = Ext.get("A").dom.childNodes
Ext.each(ele, function(e) {alert(e);});

jweber
11 Mar 2008, 2:56 PM
Right, but the documentation states: "If the passed array is not really an array, your function is called once with it." The problem is that the SELECT element is being seen as an array, so it's calling the function for each of its children. But I only want it to be called once, for the element itself.

(Of course, if I knew that in advance, I wouldn't bother calling Ext.each. But sometimes you don't know whether you've got an array or not. I was expecting Ext.each to handle either situation.)

evant
11 Mar 2008, 3:43 PM
This could actually be an issue with the select element itself, because the internal code does:



each : function(array, fn, scope)
{
if(typeof array.length == "undefined" || typeof array == "string"){
array = [array];


Maybe a select has a length property? I thought you had to access it via .options though.

jweber
11 Mar 2008, 3:47 PM
It does. So do strings, and several other non-array things. I think this function needs a better way of checking for arrays.

evant
11 Mar 2008, 3:52 PM
Actually, in the latest build of Ext they added Ext.isArray. I think this one has been missed. I'll log a bug report now.