Copy object properties to another object – Flex

Posted on Jun 22, 2010

In a project I am working on with Flex and AS3 I needed to copy all of the object’s properties and accessors to another object.

Because I didn’t want to hard code the properties for various reasons, I needed to write a function that will “crawl” the object properties and accessors and copy then to the new object.

I created a static function that does exactly that:

[as3]

/**

  • copies a source object to a destination object

  • @param sourceObject the source object

  • @param destinationObject the destination object

*/

public static function copyObject(sourceObject:Object, destinationObject:Object):void

{

// check if the objects are not null

if((sourceObject) && (destinationObject)) {

try

{

//retrive information about the source object via XML

var sourceInfo:XML = describeType(sourceObject);

var objectProperty:XML;

var propertyName:String;

// loop through the properties

for each(objectProperty in sourceInfo.variable)

{

propertyName = objectProperty.@name;

if(sourceObject[objectProperty.@name] != null)

{

if(destinationObject.hasOwnProperty(objectProperty.@name)) {

destinationObject[objectProperty.@name] = sourceObject[objectProperty.@name];

}

}

}

//loop through the accessors

for each(objectProperty in sourceInfo.accessor) {

if(objectProperty.@access == “readwrite”) {

propertyName = objectProperty.@name;

if(sourceObject[objectProperty.@name] != null)

{

if(destinationObject.hasOwnProperty(objectProperty.@name)) {

destinationObject[objectProperty.@name] = sourceObject[objectProperty.@name];

}

}

}

}

}

catch (err:*) {

;

}

}

[/as3]

This function will copy everything. You can simply add an “allowedProperties” definition and make the function only copy definitions and properites which exist in your allowed definition.

Like so:

[as3]

private static var allowedProperties:String = “height,width,visible,styleName,x,y,alpha,visible,source,dataProvider,styleDecleration,text,label,horizontalScrollPolicy,labelField,";

public static function copyDisplayObjectData(sourceObject:Object, destinationObject:Object):void

{

if((sourceObject) && (destinationObject)) {

try

{

var sourceInfo:XML = describeType(sourceObject);

var objectProperty:XML;

var propertyName:String;

for each(objectProperty in sourceInfo.variable)

{

propertyName = objectProperty.@name;

if(allowedProperties.indexOf(propertyName) > -1)

{

if(sourceObject[objectProperty.@name] != null)

{

if(destinationObject.hasOwnProperty(objectProperty.@name)) {

destinationObject[objectProperty.@name] = sourceObject[objectProperty.@name];

}

}

}

}

for each(objectProperty in sourceInfo.accessor) {

if(objectProperty.@access == “readwrite”) {

propertyName = objectProperty.@name;

if(allowedProperties.indexOf(propertyName) > -1)

{

if(sourceObject[objectProperty.@name] != null)

{

if(destinationObject.hasOwnProperty(objectProperty.@name)) {

destinationObject[objectProperty.@name] = sourceObject[objectProperty.@name];

}

}

}

}

}

}

catch (err:*) {

;

}

}

}

[/as3]

That’s it, tricky but absolutely can be done