Clone flex component

adobeairapache-flexcopy

I am trying to duplicate a flex component at run time.

For example if i have this

mx:Button label="btn" id="btn" click="handleClick(event)"/>

i should be able to call a function called DuplicateComponent() and it should return me a UI component thts exactly same as above button including the event listeners with it.

Can some one help me please??
Thanks in advance

Best Answer

Do a Byte Array Copy. This code segment should do it for you:

// ActionScript file
import flash.utils.ByteArray;

private function clone(source:Object):*
{
    var myBA:ByteArray = new ByteArray();
    myBA.writeObject(source);
    myBA.position = 0;
    return(myBA.readObject());
}

One note, I did not write this code myself, I'm pretty sure I got it from a post on the Flex Coder's list.

Related Topic