WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1]
INSERT INTO `info_tla_data` (`url`, `post_id`, `xml_key`, `text`, `before_text`, `after_text`) VALUES


Warning: Cannot modify header information - headers already sent by (output started at /home/designit/public_html/flexerinfo/wp-includes/wp-db.php:615) in /home/designit/public_html/flexerinfo/wp-content/plugins/wp-greet-box/wp-greet-box.php on line 1398
FLEX{er} » Blog Archive » Deep Object Copy
We love choice

Fx{r} is trying to start the Fx{r} Community! Please join our group on Adobe Groups following this link: http://fxr.groups.adobe.com.
Fx{r} is now on Twitter too. Follow us @ twitter.com/fx_r!
«
»

ActionScript, Bugs, Flex 2

Deep Object Copy

Virgil Cristea | 25.10.07 | 6 Comments

Google Buzz

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

Share and Enjoy:
  • Twitter
  • Google Buzz
  • LinkedIn
  • Google Bookmarks
  • del.icio.us
  • Digg
  • Sphinn
  • blogmarks
  • Reddit
  • StumbleUpon
  • Facebook
  • DZone
  • FriendFeed
  • Yahoo! Buzz
  • Yahoo! Bookmarks
  • Slashdot
  • MySpace
  • Add to favorites




Tags: , ,

This post was written by Virgil Cristea

Views: 17809

related

6 Comments

have your say

Add your comment below, or trackback from your own site. Subscribe to these comments.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these 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="">

:

:


«
»