I wanted to write a few words about the deep object copying in flex (aka as making an absolute identical replica of an object no matter it’s class).
There is a function in the flex API called ObjectUtil.copy This is a very nice function but the problem is it cannot work on all object classes. How can you handle those cases? You can make your own function for deep copy but that includes you need to know all about the object structure.
Let’s take a quick look at how Flex copies objects:
var buffer:ByteArray = new ByteArray(); buffer.writeObject(value); buffer.position = 0; var result:Object = buffer.readObject(); return result;
It looks pretty interesting, no? What it does: instead of introspection and recursively copying properties from one object to another it will serialize the object as an array of bytes and then de-serialize it.
Using AMF means that some strange things might happen: Casts back to the original object class may fail, and the [Transient] metadata tag can affect the output.
So when the object is de-serialized it will not be created as an instance of a class (even if it has all the properties). In order to be able to have this you can inform AMF about the object class. You can do this in 2 ways:
- use [RemoteClass] metadata
- use registerClassAlias()
The first method although is much nicer may not work ok in all cases as it seems that the remote class information is stripped from the byte array in some cases.
Let’s see how to use the registerClassAlias:
// TestClass doesn't have [RemoteClass] metadata or // it does not work, so we need to make an association between //the TestClass and the full qualified name (com.qbic.tests.TestClass) registerClassAlias("com.qbic.tests.TestClass",TestClass); // Now we can cast the result of the copy without errors. var testCopy:TestClass = TestClass(ObjectUtil.copy(test));
Even if this is a nice and very effective method it has one flaw: it does not work with BitmapData. But here is a workaround for this issue:
public function copyBitmap(target: DisplayObject) : Bitmap { // Create the bitmap data object with the right size. var data : BitmapData = new BitmapData(target.width, target.height, true, 0); // Draw the target object into the bitmap data. data.draw(target); // Create a new bitmap object associated with this data. var bitmap: Bitmap = new Bitmap(data); return bitmap; }
Follow-up of: Transient
Tags: ActionScript, bug, flex
This post was written by Virgil Cristea
Views: 2316



















[...] The [Transient] metadata tag can be used for classes that need to be on the server but also for other classes (when doing deep copies of them - I will write another post about object deep copy later - Here). [...]
I tried using this to create a copy of an AdvancedDataGridItemRenderer instance, but I got a runtime error:
var dispObject:DisplayObject = DisplayObject(copyBitmap(DisplayObject(lineItemEntityRenderer)));
addChild(dispObject);
function copyBitmap(target:DisplayObject):Bitmap
{
// Create the bitmap data object with the right size.
var data:BitmapData = new BitmapData(target.width, target.height, true, 0);
// Draw the target object into the bitmap data.
data.draw(target);
// Create a new bitmap object associated with this data.
var bitmap:Bitmap = new Bitmap(data);
return bitmap;
}
Runtime Error:
TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::Bitmap@11827bf1 to mx.core.IUIComponent.
at mx.core::Container/http://www.adobe.com/2006/flex/mx/internal::addingChild()[E:dev3.0.xframeworksprojectsframeworksrcmxcoreContainer.as:3251]
at mx.core::Container/addChildAt()[E:dev3.0.xframeworksprojectsframeworksrcmxcoreContainer.as:2200]
at mx.core::Container/addChild()[E:dev3.0.xframeworksprojectsframeworksrcmxcoreContainer.as:2140]
You try to copy an AdvancedDataGridItemRenderer using the code provided for a bitmap. That will not work (as you saw it) because they are incompatible type. Instead use ObjectUtil.copy(). It should work ok. If you get an error when you recast the result back to your normal object use the register alias method explained above.