Calendar

September 2010
S M T W T F S
« Jul    
 1234
567891011
12131415161718
19202122232425
2627282930  

Cloning ArrayCollection

There are plenty of occasions where you might want to be able to create an exact clone of a complex/nested data objects, without your new object simply referencing the old one’s values. To do this, you need to force the creation of new values and references. Depending on the complexity of the data stored, this will be achieved through shallow or deep copying:

Shallow Copy

Fine if your collection only contains values and references that can be shared/reused around the app:

var newAC:ArrayCollection = new ArrayCollection( originalAC.source.slice( 0,

originalAC.length ) );

Deep Copy

Heavier going; but as well as creating cloned values, it creates new references (pointers) for everything too. Only really necessary if you don’t want your new object to hold the same object references that your old one did:

private function clone( source:Object ) :*

{

var myBA:ByteArray = new ByteArray();

myBA.writeObject( source );

myBA.position = 0;

return( myBA.readObject() );

}

newAC:ArrayCollection = new ArrayCollection( clone( originalAC.source ) );

The clone method in the code above can be used for cloning just about any object (including deep copying of ByteArray), and would make a good utility method in any custom framework (both within or outside of the Flex framework). Please note that readObject() will not work with Flash DisplayObjects; however, it will work with complex objects derived from simple types.

The Flex frameworks includes the ObjectUtil that allows you to perform a clean copy using the copy() method.

var myAC:ArrayCollection = new ArrayCollection( mx.utils.ObjectUtil.copy( myAC.source ) );

source http://jodieorourke.com/view.php?id=105&blog=news

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • Facebook
  • Google Bookmarks
  • Live
  • MySpace
  • PDF
  • RSS
  • Twitter
  • Yahoo! Bookmarks
  • email

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">