Aug 07 2007

method_missing in ActionScript 3/Flex

Tag: AS3, ActionScript, Flex, RubyDerek Wischusen @ 2:05 am

method_missing is one of the small bits of Ruby magic that can be used to some really amazing things when used properly, and some really dangerous things when used improperly. Rails dynamic finders are good example of the amazing things that you can do. Here are some others.

It is possible to implement something like method_missing in AS3 using the Proxy class. Here is a relatively simplistic example:

import flash.utils.Proxy;
import flash.utils.flash_proxy;
 
dynamic public class BaseProxy extends Proxy
{     
	flash_proxy override function callProperty(method: *, ...args): * {
	   try { 		 
	   var clazz : Class = getDefinitionByName(getQualifiedClassName(this)) as Class;
	    return clazz.prototype[method].apply(method, args);
	   }
	   catch (e : Error) {
		return methodMissing (method, args);
	   }
 
	}
 
	protected function methodMissing(method : *, args : Array) : Object{
		throw( new Error("Method Missing"));
	}
}

The callProperty method is called whenever an undefined method is called on an instance of this class, or any instance of class that extends this class. In the try block we check if the method was defined on the prototype. If it is not found there, we call methodMissing.

So, now if we create another class that extends this one like so

public dynamic class Model extends BaseProxy
{
	public function myMethod (arg1 : String, arg2 : Boolean) : String {
		return arg1 + " " + arg2;
	}
}

and then run the following trace statements

import flash.display.Sprite;
 
public class MethodMissingExample extends Sprite
{
	public function MethodMissingExample()
	{
		var m : Model =  new Model();
 
		Model.prototype.runtimeMethod = function (date : Date) : String {
			return "I was defined at runtime at " + date.toLocaleTimeString();
		};
 
		trace(m.myMethod("I exist", true));
                trace(m.runtimeMethod(new Date()));
		trace(m.someMethod(0, false, "x"));
	}
}

You will see the following in the console:

I exist true
I was defined at runtime at 09:58:00 PM
Error: Method Missing

The first call succeeds because myMethod is defined. someMethod is not defined, so a Method Missing error gets thrown.

Now, if we override the missingMethod in the Model class like so,

public dynamic class Model extends BaseProxy
{
	public function myMethod (arg1 : String, arg2 : Boolean) : String {
		return arg1 + " " + arg2;
	}
 
	override protected function methodMissing(method : *, args : Array) : Object {
		return "You called " + method + " with " + args.toString();
	}
}

and run the same trace statements you will see the following in the console:

I exist true
I was defined at runtime at 09:58:00 PM
You called someMethod with 0,false,x

Online valium without prescription
Buy phentermine no script
Buy phentermine no rx
Propecia uk prices
Cialis purchase online
Viagra purchase uk
Best way to buy viagra online
Cialis side effects
2mg xanax no prescription
Generic cialis overnight
Purchase tramadol without prescription
Authentic phentermine 37.5
Tramadol online no prescription overnight
Cheapest cialis price
Buy brand name viagra
Viagra express delivery
Phentermine canadian pharmacy
Viagra in france
Cialis order canada
Prednisone tablets 10 mg
Cheapest cialis professional
Viagra pharmacy prices
Tramadol prescription online
Viagra online purchase in india
Viagra dosage information
Valium online pharmacy
Generic viagra sales
Generic propecia
Generic viagra for sale
Low cost cialis
Prednisone online
Cheap valium online
Blood pressure and prednisone
Phentermine online uk
Levitra samples
Buy cialis uk
Prednisone tablets
Xanax for sale without prescription
Buy viagra in england
Cheapest levitra
Propecia online uk
Levitra 20mg
Buy viagra uk online
Get tramadol prescription
Purchase levitra online
Where to buy cialis safely
Propecia price
Buy generic valium online
Valium generic
Phentermine hcl no prescription
Phentermine canada no prescription
Cheap xanax bars
Buy cialis online from canada
Cheap levitra uk
Propecia uk pharmacy
Best viagra alternative
Buy phentermine 37.5mg online
Viagra prescription
Get viagra prescription
Free samples of cialis
Cialis canada no prescription
Buy viagra uk no prescription
How to buy phentermine online
Discount viagra pills
Buy generic cialis
Prescription free viagra
Buying viagra in new zealand
Levitra online cheap
Discount viagra usa
Xanax no prescription required
Prescription valium
Xanax online cheap
Buying valium in spain
Cheap propecia without prescription
Cialis discount price
Xanax generic dosage
Valium cheapest
Prescriptions for phentermine
Mail order phentermine
Viagra without prescription uk
Brand viagra cheap
Phentermine cheap online
Xanax 0.5 mg
Tramadol medication
Propecia best prices
Order xanax online
Generic cialis tadalafil
Drug phentermine
Cheap cialis
Generic cialis uk
Viagra online shop
Xanax no prescription overnight
Buy phentermine 37.5mg pills
Levitra price
Buy viagra online in ireland
Cheap xanax for sale
Xanax no rx
Buy phentermine online no prescription
Phentermine purchase online
Pharmacy tramadol
Propecia 1mg generic
Buy valium cheap online
Canada pharmacy valium
Tramadol without prescription
Purchase phentermine
Cialis medication
Viagra no prescription online
Xanax price per pill
Where to buy viagra in england
Phentermine 37.5 mg
Free cialis online
Viagra 50 mg online without prescription
Cheap generic valium
40 mg prednisone side effects
Phentermine buy australia
Buy female viagra without prescription
Buying xanax online without prescription
Cheapest place to buy viagra online
Xanax bars effects
Viagra pills for sale
Order cheap viagra online
Cialis 10mg side effects
Cheap generic viagra
Low price cialis
Valium without prescription uk
Best levitra prices
Buy cialis in the uk
Brand name cialis
Purchase viagra online without prescription
Valium online uk
Generic xanax no prescription
Cheapest online cialis
Where can i buy viagra without prescription
Dosage of xanax
Discount viagra online
Phentermine with no prescription
Prescription propecia
Viagra online cheap
Viagra in the uk
Purchase tramadol online
Xanax buy uk
Cheap levitra no prescription
Prednisone 20mg side effects
Buy generic propecia uk
Buy valium no rx
Buying tramadol in uk
Best prices for cialis
Cialis samples canada
Valium drug side effects
Prescription viagra canada
Cheap tramadol overnight delivery
Viagra cheapest
Viagra discount coupons
Viagra lowest prices
Levitra online
Viagra discount prices
Where can i buy viagra without a prescription
Viagra canada online
Buying levitra without prescription
Where to buy levitra online
Where can i buy cialis without a prescription
Cialis 20mg side effects
Levitra on sale
Buy viagra australia
Cialis dosage 20mg
Purchase xanax
Propecia information
Valium online fast delivery
Cialis 20 mg dosage
Viagra in usa
Prescription phentermine online
Ordering propecia from canada
Propecia usa
Viagra canadian online pharmacy
100mg tramadol online
Buy propecia cheap
Tramadol pharmacy
Viagra to buy
Viagra generic cheap
Free cialis samples
Cheap tramadol cod
Propecia ireland
Cialis ordering
Fedex tramadol
Tramadol for sale
Generic xanax
Where can i buy viagra in the uk
Canada viagra
Levitra canada
Where to buy cialis online
Phentermine without a prescription
Low price viagra
Valium from india
Overnight tramadol no prescription
Canada viagra no prescription
Buying viagra online
Order tramadol online overnight
Viagra indian pharmacy
Buying cialis online without a prescription
Cialis prices uk
Low cost levitra
Buy valium without prescription uk
Australia viagra online
Viagra price canada
Best price tramadol
Viagra sale uk
Cheap cialis viagra
Viagra ordering
Buy valium europe
Levitra us
Xanax 1mg
Propecia price australia
Cialis uk sales
Xanax with no prescription
Buy xanax canada
Discount viagra india
Viagra canada prices
Phentermine diet pills without prescription
Viagra prescription cost
Phentermine 37.5 wholesale
Buy viagra from canada
2.5mg cialis
How to buy valium without a prescription
Buy phentermine hcl 37.5 no prescription
Overnight delivery viagra
10mg valium effects
Order phentermine online no prescription
Xanax 1mg side effects
Buy cialis brand
Buy generic cialis uk
Xanax 1 mg dose
Cialis for sale
Viagra online without prescription reviews
Pfizer viagra price
Cialis soft tabs online
Viagra shop online
Buy generic xanax no prescription
Generic levitra uk
Best way to take tramadol
Xanax bars dosage
Buy tramadol overnight
Generic tramadol
Levitra purchase
Buy xanax overnight
Phentermine 37.5 pills
Viagra canada mastercard
Cost of viagra 50mg
Cialis online canadian pharmacy
Generic viagra super active
Phentermine 37.5 buy online