Oct 23, 2006

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.

Share this Post


                           

Comments


jesse     Dec 06, 2006
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     Dec 06, 2006
@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.

matt     Dec 06, 2006
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

Dan     Nov 06, 2006
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);

Sean Scott     Nov 06, 2006
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...

Armando Alves     Oct 27, 2006
I think they've done something similar here:

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

based on the works of Joey Lot.

Jamie     Oct 26, 2006
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.

Jesse     Oct 25, 2006
They call it a proxy over here: http://www.createage.com/blog/?p=77

Ryan     Oct 25, 2006
Good stuff. I posted about something very similar on my blog last week.

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


Speak






Submit »