Flash Tri →← Image Cache

Improved Delegate

The Delegate class has existed for some time. We just added a little twist to it. Our version Delegate (com.bigspaceship.utils.Delegate) does everything that Adobe’s does, but also allows for additional custom arguments.

myMovieClip.onEnterFrame = Delegate.create(this,_onEnterFrame,"hello world");
function _onEnterFrame($str:String):Void {
trace($str); // will output "hello world"
};

You can take a look at our version of this here.

If the function you’ve delegated comes with pre-existing arguments they immediately get tacked to the end of your targeted function. For example:

var x = new XML();
x.onLoad = Delegate.create(this, _onXMLLoad, x, "hello world");
x.load("foo.xml");

function _onXMLLoad($x:XML, $str:String, $success:Boolean):Void {
trace($str + “: ” + $x + “: ” + $success);
// will output “hello world” along with your XML object and success boolean.
};

You can read more about Proxying Events here.

October 23rd, 2006  by Stephen  /  9 Comments

Comments on “Improved Delegate”

  1. Good stuff. I posted about something very similar on my blog last week.

    http://www.boostworthy.com/blog/?p=10

    Ryan on October 25th, 2006 at 5:36 pm
  2. They call it a proxy over here: http://www.createage.com/blog/?p=77

    Jesse on October 25th, 2006 at 9:28 pm
  3. Yeah, I actually had a link to Adobe’s DevCenter specifically talking about using Delegate as an Event Proxy. I forgot to post the link in the blog post. I’ll add it a little later on today.

    Jamie on October 26th, 2006 at 11:11 am
  4. I think they’ve done something similar here:

    http://dynamicflash.com/2005/02/delegate-class-refined/

    based on the works of Joey Lot.

    Armando Alves on October 27th, 2006 at 10:05 am
  5. yeah i used Joey lots Proxy class as well.

    Having said that my hats off to you guys for sharing all this code.

    Takes balls…

    Sean Scott on November 6th, 2006 at 11:37 am
  6. I built a while back that I use extensively.. yours actually beats mine in functionality in one respect (I was passing an array for my arguments)

    Though I’m curious of one thing, which is this line:
    return func.apply(target, args.concat(arguments));
    Why are the “arguments” being concatenated? There are no arguments in the base function right?

    Shouldn’t the line simply read:
    return func.apply(target, args);

    Dan on November 6th, 2006 at 3:35 pm
  7. this is excellent, definitely the best version of delegate out there…

    however using it in FlashDevelop I ran into that MTASC problem, any chance you guys could do an updated version with MTASC compatibility - http://www.flashdevelop.org/community/viewtopic.php?t=258&highlight=delegate

    matt on December 6th, 2006 at 6:50 am
  8. @Dan - anytime you pass prams to a function they are referenced inside the function as arguments.

    function func ( a, b )
    {
    trace( arguments ) // traces 1,2,3,4
    }
    func ( 1, 2, 3, 4 );

    The args are stored when you first create the Delegate. func.apply(target, args.concat(arguments)) appends the new arguments to the stored arguments. Essentially creating both static and dynamic function.

    jesse on December 6th, 2006 at 8:34 pm
  9. My personal jgDelegate class was first expanded from the work at createage.com. Since then it has grown to include create, proxy, returns, overwriting, as well as static & dynamic appending. Today I read Matt’s comment and updated my post to cover the MTASC compliance issue, along with the delegate-class-refined approach sans what I think is “argument pollution”.
    -
    I’m curious to figure out what the “Best” class would be whether these points are of interest to many people.
    -
    http://www.justgooddesign.com/blog/jgdelegate.htm
    http://www.createage.com/blog/?p=77

    jesse on December 6th, 2006 at 9:11 pm