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 :-)

YUI Calendar page to the selected date (multi calendar)

I have been working with YUI calendar for a couple of weeks now on a project. I found that it is highly customizable although the documentation are not always clear and you have to dig around the internet to find your answers.

So, I am here to help.

In this new website I have a multi-calendar where you can page forwards and backwards through the dates. The calendar is also showing and hiding according to a user click (see photo).

The problem was (for me anyway) that when the user clicked the calendar’s button and the calendar showed the last paging position.

Let’s say I selected a date, or even better a date interval (from Feb 2nd to Feb 10th) and then paged to see other months available dates, closed the calendar and re-opened it, it seemed as if the selection cleared and the paging position kept.

My client’s request was “if the client selected a date and paged, always take him back to his selection when the calendar is shown”

Well, this is the way to do it.

var dates = cal1.getSelectedDates();

if(dates.length > 0)
{
var l = dates[0];

cal1.cfg.setProperty("pagedate", l);
cal1.render();
}

What did we do here?

First, we checked if there are selected dates in the calendar. If there were, we got the lowest date (the first date of the interval). Then we set a property on the calendar called “pagedate” with this lowest date and WHALLLA, it works!
Now, every time you open the calendar it pages to the selected dates section, no matter if you paged forward before hiding the calendar.

Empty dialogs in flex builder 3 + Flash builder 4

This week I needed to work with My PC on a flex project. I usually stay away of the PC with flex projects, I’m used to working on the mac with these project, but I needed to work on the PC.

While trying to configure the project in flex builder 3 (and with 4 as well), I stumbled upon a weird bug that made me scratch my head for a while.  The bug was that all of the dialogs appeared empty, no configuration, almost no buttons.

Here’s a screenshot of the problem:

Now, I remembered a teammate I had a while back that had the same problem and it appeared to be a WACOM with a windows 7 driver that caused the problem, so I started disabling devices one after the other.

The problem was a logitech mouse software I have installed on the pc. Once I quited this application everything went back to normal.

Here’s the icon on the taskbar (not hard to trace).

Hope this post helps you and you won’t scratch your had for hours like I did.

Git export (like SVN export)

I have been working with git for a while now on my Ruby on Rails projects and client side (css, Html and JavaScript) projects. I haven’t moved completely in flex projects and other projects, but I do plan to in the near future, mainly because of the reason that you can integrate GIT into flash builder 4. But, that is not the point.

This week I was working with a company on a client side project. I had to email them the files every time and the .git folders inside the project were heavy and unnecessary.

If you worked with SVN, you probably know the export command, after searching for a bit in git help I found how you can do it in git with ease.

How to do?

This is the command you should use:

git checkout-index -a -f --prefix=/path/to/your/folder/

The ending forward slash is very important! Do not forget to use it please.

A screenshot of my shell:

So, this is how you export git.

Good luck!