Copy object properties to another object – Flex

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:

		/**
		 * 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:*) {
					;
				}
			}

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:

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:*) {
					;
				}
			}
		}

That’s it, tricky but absolutely can be done :-)

  • http://www.emttrainingcourse.com/ emt training

    this post is very usefull thx!

  • http://www.emttrainingcourse.com/ emt training

    this post is very usefull thx!

  • Pingback: Copy object properties to another object – Flex | From inspiration … « Internet Cafe Solution

  • http://genericwpthemes.com Wordpress Themes

    Genial fill someone in on and this mail helped me alot in my college assignement. Thank you on your information.

  • http://genericwpthemes.com Wordpress Themes

    Genial fill someone in on and this mail helped me alot in my college assignement. Thank you on your information.

  • http://www.takil.net/program program indir

    thanks good post

  • Bone

    Great stuff man.

    • http://www.kensodev.com Avi Tzurel

      Thank you.

  • Pingback: http://www.agreensupply.com/

  • Julien Kronegg

    Thanks. There is a missing “}” after line 45 of the first code fragment.

  • Safgdsda

    fdghf

  • http://www.imigyled.com LED Strip

    It would be better if you can also display the result of your coding.

  • http://www.facebook.com/jordan.troy Troy Jordan

    Very helpful, thanks for the good post