Binding ArrayCollections of Arrays
I've been kinda stuck in trying to organize data and bind them to a Repeater in one my applications. What I did at first, was to save an array of arrays, so that I could've accessed any value very easily. But then I realized that I needed to bind them to a Repeater, so I had to switch to an ArrayCollection of arrays. But the Binding issues weren't solved, since there are still arrays (not bindable) inside the ArrayCollection. So, what can I do??
Below an example of what I want to do:
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
[Bindable] public var arr:ArrayCollection = new ArrayCollection(
[[ {v:"1", s:"f"}, {v:"1", s:"p"}, {v:"1", s:"c"}, {v开发者_如何学C:"1", s:"q"}
], [
{v:"1", s:"f"}, {v:"1", s:"p"}, {v:"1", s:"c"}, {v:"1", s:"q"}
]]
);
protected function button1_clickHandler(event:MouseEvent):void
{
trace(arr.getItemAt(0).length);
arr.getItemAt(0).splice(0, 1);
trace(arr.getItemAt(0).length);
}
]]>
</mx:Script>
<mx:HBox>
<mx:Repeater id="rep" dataProvider="{arr.getItemAt(0)}">
<mx:Label text="{rep.currentItem.v}" />
</mx:Repeater>
<mx:Button label="del" click="button1_clickHandler(event)" />
</mx:HBox>
the traces show that the array size changes, but the repeater doesn't update. Any idea?? What's the best practise?
Binding updates a destination when the source changes. In complex objects--such as an array--that source is a pointer to some other memory location. In this situation you are not changing the source; only the item the source points at. And therefore binding will have no affect.
That said, repeaters are unusual beasts and I suggest you don't use one. Use a list based class, such as a List. This will be less performance intensive than using a repeater, thanks to renderer recycling.
Inside the list based class; your dataProvider is turned into a ListCollectionView; and it in turn listens for the CollectionChange event of your dataProvider to modify the displayed items when elements in the dataProvider change. You have no such code when using a repeater.
精彩评论