Aug 07

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

10 Responses to “method_missing in ActionScript 3/Flex”

  1. Per Olesen says:

    Cool.

    But I believe that rails and the dynamic finders go further than that. In ruby, the proxy object is able to actually get the unmatched method defined on itself at runtime. For instance, in your example, the “methodMissing” did not have to be defined on the inherited Model, but “callProperty” would “simply” define it at runtime, when it catched a missing method. This way, it would not me missing the next time it was called.

    Can actionscript (v3?) do that: Define new methods on objects at runtime, like ruby can?

  2. Brian says:

    I’ve been using this a lot lately. You need to make sure you make the classes dynamic if you want to use the property aspect. Also I have yet to figure out the difference between flash_proxy and object_proxy, though every example I’ve seen uses flash_proxy.

  3. Derek Wischusen says:

    Hi Per –

    You can definitely add new methods in AS3 at runtime. Check out this post for an example: http://flexonrails.net/?p=79

  4. Derek Wischusen says:

    Per – I went ahead and updated this post to show how you can add a method at runtime.

  5. Derek Wischusen says:

    Hi Brian – flash_proxy and object_proxy are both namespaces that are used to prevent name conflicts. All of the methods in the Proxy class are defined in the flash_proxy namespace, so if you want to override them you have to this namespace.

  6. Luke Bayes says:

    I just wanted to chime in on this post….

    In ActionScript 3, any entity that gets added to the Display List must actually extend flash.display.DisplayObject because the core Display List method signatures were not written to interfaces.

    The Flex framework was built entirely on top of the core Display List base classes (rather than using Composition).

    This means that in ActionScript 3, and especially in Flex projects, no visual entity can actually take advantage of this functionality.

    Kind of disappointing.

  7. Derek Wischusen says:

    Hey Luke,

    hmmm… that sounds like poor framework design to me. I suppose we could submit bug to the Flex bug tracker. Though it looks like it’s actually the underlying Flash Player method that expects a DisplayObject, so they may not be able to change this easily.

  8. ana says:

    Intresting. Thanks

  9. computers should be less friendly » Blog Archive » Dynamically Generated Static Methods says:

    [...] was inspired to investigate this topic by Derek Wischusen’s post “method_missing in ActionScript.”  I saw it and immediately set out to build a port of ActiveRecord to [...]

  10. My Notebook » ActionsScript 3: Calling nonexisting methods says:

    [...] This great blog entry on flexonrails explains the magic. [...]

Leave a Reply

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