views:

1004

answers:

1

I'm using an array to populate a DataProvider, which I'm using to populate a list component. Like this:

var myDataProvider = new DataProvider(this.myArray);
this['list'].dataProvider = myDataProvider;

When changes are made to the array, I want to tell the DataProvider to update so that those changes will be reflected in the list component. It would be nice if there was a way to tell the DataProvider to listen for changes in the array and update itself, but I would settle for a way to manually update it.

I can replace the DataProvider with a new DataProvider, but then the list loses its selection. I suppose I could go through the DataProvider and compare and modify every entry to make it match the array, but this seems way too cumbersome. Is there any way to tell a DataProvider to update to match an array?

Edit: I'm looking for a way to do this in Flash, not Flex.

+2  A: 

Arrays are not bindable in AS 3; they don't dispatch/trigger data binding events themselves.

If you are working with Flex, you can wrap your array in an ArrayCollection which is bindable, and manipulate the ArrayCollection instead of the Array, everything should automatically work as you hope.

So, in practice:

var myAC:ArrayCollection = new ArrayCollection(myArray);
var myDP = new DataProvider(myAC);

myAC[0] = 'changing the array!'; // will be reflected in the dataProvider

Better yet, if you're passing that DataProvider instance to a Flex component, you can usually (almost always) just pass the ArrayCollection instead. But, you haven't shown enough context for me to be sure on this.

Here's a few useful links:

Edit: My fault, I did indeed assume you were using Flex for the above.

In plain old AS3, the principal is the same. The Array does not dispatch events, but the DataProvider does. If you can make your changes to the dataProvider itself then you're good to go.

The AS3 DataProvider class offers a bunch of methods for data manipulation, like addItem, removeItem, replaceItem, replaceItemAt, just for example.

So, it basically comes down to what kind of changes you need to make to the source array. More often than not, you'll be able to do them via the DataProvider.

jason
Thanks but I'm pretty sure that ArrayCollection is a Flex class. Is there a way to get this functionality in Flash?
Robert
Thanks for the edit. I considered this, but the source array is created in a different class, which uses it just as an array. It seems a little klugey to rewrite it to use a DataProvider. Is there no way to just tell the DataProvider to reconfigure itself to match the changed array?
Robert
Since objects are passed by reference, the DataProvider and the class that creates the Array can each use the array as needed, without knowing about each other. There should be no need to modify the other class.
Tyler Egeto
@Tyler: But... The DataProvider cannot be / is not aware of changes to the Array. Sure, it has a reference to it, but it doesn't know it changed, because an Array does not dispatch change events.
jason