Tip of the Day – Proxies… Make ‘em dynamic

A couple of days ago, I was working on my DateTime proxy class.

DateTime.as (Trimmed version)

package net.natebeck.core
{
	import com.flexoop.utilities.dateutils.DaylightSavingTimeUS;
 
	import flash.utils.Proxy;
	import flash.utils.flash_proxy;
 
	public class DateTime extends Proxy
	{
		// Instance Variables
		protected var _date:Date;
		protected var _timezone:Timezone;
 
		public function DateTime(... args)
		{
			super();
 
                       _date = new Date(args[0]);
		       if(args[0] is String)
		            setTimezone(parseTimezone(args[0]));
		}
 
		public function setTimezone(value:Timezone):void
		{
			trace("Setting Timezone: "+value);
			_timezone = value;
		}
 
		public function getTimezone():Timezone
		{
			trace("Getting Timezone: "+_timezone);
			return _timezone;
		}
 
		public function getTimezoneOffset():Number
		{
			return _timezone.offset;
		} 
 
		override flash_proxy function callProperty(methodName:*, ... args):*
		{
			var res:*;
	        switch (methodName.toString()) 
	        {
	            case 'toString':
	            	res = "foobar";
	            	break;  
	            default:
	                res = _date[methodName].apply(_date, args);
	                break;
	        }
	        return res;
		}
 
		override flash_proxy function getProperty(name:*):* {
			trace("getting property: "+name);
	        return _date[name];
	    }
 
	    override flash_proxy function setProperty(name:*, value:*):void {
	        _date[name] = value;
	    }
 
	}
}

I wrote a simple test application to test my DateTime Proxy class while I’m developing.

TestDateTime.as

package
{
	import flash.display.Sprite;
 
	import net.natebeck.core.DateTime;
	import net.natebeck.core.Timezone;
 
	public class TestDateTime extends Sprite
	{
		public function TestDateTime()
		{
			var create:Object = "Jun 17 12:00:00 GMT-0500 2009";
 
			var oldDate:Date = new Date(create);
			var strDate:DateTime = new DateTime(create);
 
			trace("old: "+oldDate);
			trace("new: "+strDate);
 
			strDate.getTimezone();
			strDate.setTimezone(Timezone.PST);
			strDate.getTimezone();
 
			trace(strDate.toString());
 
 
		}
 
	}
}

When trying to compile using Flex Builder I ran into this error:

ERROR 1061: Call to a possibly undefined method toString through a reference with static type net.natebeck.core:DateTime.

I thought to myself, “Well that’s lame… the whole reason I’m using the Proxy class is so I don’t have to re-write every method within my class”.

As usual, the issue wasn’t with the compiler, it was me being dumb. Computers… they’re always right.

The answer was simple, and makes complete sense after someone mentioned it.

When creating your own Proxy class, make them dynamic!

public dynamic class DateTime extends Proxy

If you’re confused about when or why to use the flash.utils.Proxy class, I gave a presentation yesterday at the Seattle Flex User Group about the Proxy class. My presentation materials are posted here.

I’d also like to thank Dale and Josh McDonald for quickly responding to my question on flexcoders.

Related Posts

Comments Closed