[flexcoders] ArrayCollection, addAll and Refresh

1 view
Skip to first unread message

johncch85

unread,
Feb 11, 2010, 11:32:25 AM2/11/10
to flexc...@yahoogroups.com
 

I understand that ArrayCollection.addAll was added in flex sdk 3.4. I'm coding a simple application and I'm hitting this problem. I would like to ask if anyone knows what's happening:

Here's the source, just a simple mxml:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="creationComplete()">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
private var a:Array = [
{ key: "a", value: "b" },
{ key: "a", value: "b" },
{ key: "a", value: "x" },
{ key: "a", value: "b" }
];

private var b:Array = [
{ key: "b", value: "c" },
{ key: "b", value: "x" },
{ key: "b", value: "c" },
{ key: "b", value: "c" }
];

private var c:Array = [
{ key: "d", value: "x" },
{ key: "d", value: "e" },
{ key: "d", value: "e" },
{ key: "d", value: "e" }
];

[Bindable] public var xx:ArrayCollection = new ArrayCollection();

public function creationComplete():void {
var i:int;
xx.filterFunction = filter;
xx.addAll(new ArrayCollection(a));
/*for (i = 0; i < a.length; i++) {
xx.addItem(a[i]);
}*/
trace("Refresh is " + xx.refresh());
xx.addAll(new ArrayCollection(b));
/*for (i = 0; i < b.length; i++) {
xx.addItem(b[i]);
}*/
trace("Refresh is " + xx.refresh());
xx.addAll(new ArrayCollection(c));
/*for (i = 0; i < c.length; i++) {
xx.addItem(c[i]);
}*/
trace("Refresh is " + xx.refresh());
}

private function filter(item:*):Boolean {
if (item.value == "x") return false;
return true;
}

]]>
</mx:Script>
<mx:DataGrid id="dg" dataProvider="{xx}"></mx:DataGrid>
</mx:Application>

When I compile and run I get:

RangeError: Index '5' specified is out of bounds.

What I'm doing basically is to add some random array of objects into an ArrayCollection binded to the DataGrid. And because there is a filter function associated with the ArrayCollection, I call refresh() every time something is added. The original use case is that this is like a drop object method for the datagrid, so it's possible for it to be called multiple times.

I'm just curious why is this happening. Is this the desired behaviour? I'll look into the source code later when I'm free, but this is just to get a general feeling of the problem.

__._,_.___
.

__,_._,___

valdhor

unread,
Feb 11, 2010, 3:07:47 PM2/11/10
to flexc...@yahoogroups.com
 

This looks like a bug in the ListCollectionView.as file. If you'd like, you can monkey patch this code to work.

The bug is at line 529:
this.addItemAt(addList.getItemAt(i), i+index);

should be:
list.addItemAt(addList.getItemAt(i), i+index);

I figured this out from the addItemAt function.

If you don't know how to monkey patch Flex code, this is how I did it:
-Find out the package directory from the package line (In this case mx.collections).
-Create the directories as per this line (In this case create a directory mx in your src directory. In this directory create a directory named collections).
-Inside the directory create an ActionScript file with the same name as the file you are monkey patching (In this case ListCollectionView.as).
-Copy and paste all the code from the original file into this new file.
-Change this new file as you see fit. When Flex compiles your code will be included instead of the framework code.

You should file a bug with the fix.

__._,_.___
.

__,_._,___
Reply all
Reply to author
Forward
0 new messages