PDA

View Full Version : DelegateHelper



Tcoz
June 21st, 2005, 02:34 PM
Having done a lot of .Net programming over the past few years, I found myself really missing the .Net event/delegate pattern, particularly the ability to easily pass a reference to the object triggering the event, and an object containing arbitrary data (a la eventargs) into the scoped event handler. This became a lot more so when I started using Flash to build UIs for regular commercial software as opposed to websites.

So, I extended the Delegate class with this DelegateHelper to do just that more or less.

Here's the class:


/**
* @author = tcoz
* Extends basic flash delegate functionality
* so that you can pass reference to event thrower
* and any other arbitrary data as an object with props
* to the indicated event handler
*/

class DelegateHelper extends mx.utils.Delegate
{
private var func:Function;
private var scope:Object;
private var trigger:Object;
private var data:Object;

// obj = object with handler the handler function, f = that function, args = optional data
public function DelegateHelper(obj:Object, f:Function, args:Object)
{
func = f;
scope = obj;
data = args;
}

// trig = object that is triggering the event, s = the function (i.e., "onPress")
public function addToUser(trig:Object, s:String)
{
trigger = trig;
trig[s] = create(this, passTrigger);
}

private function passTrigger()
{
func.call(scope, trigger, data);
}
}

//////////////////////////////////////////

And Here's the use (remember to import DelegateHelper)


// instantiant in constructor or whatever
private var dataObject;

// create delegatehelper with desired scope and function reference
var delRelease:DelegateHelper = new DelegateHelper(this, onButtonRelease);

// subscribe to targetobject and targetobjects's desired event
delRelease.addToUser(targetObject, "onRelease");

...manipulate dataObject if needed, or get it from somewhere else, etc...

// event handler in subscribed scope, which receives a reference to the event thrower
// and an object with whatever data
private function onButtonRelease(sender:Object, data:Object):Void
{
// reference to sender, and any other info, available!
}

I've fiddled with typing the DataObject (basically emulating the .Net EventArgs class) and a couple of other things, like destroying the helpers, but this is the basics of it. I've found it pretty useful in a couple of big projects, really lets me collapse a lot of code and implement some nice clean patterns. I'll post an update when it's ready if people seem interested. Not as far reaching as the efforts with compilers and whatnot to emulate this functionality, but lightweight, performant and easy to use.

smoothhabitat
June 21st, 2005, 03:58 PM
Nice code. I have a similar bit that I've been using... but mine wasn't as clean (as I tend to just tack it on when I need it).

Tcoz
June 21st, 2005, 05:00 PM
Yah I build a lot of this sort of thing, since I'm working heavily on frameworks (some of my projects really need clean consistent tools for the bigger patterns). Things like an ObjectHelper (for cloning and other standard object things missing from plain old object), that sort of stuff.

Btw email me at tcoz@tcoz.com if you like (please excuse website if you find it, never have time to finish it).

senocular
June 22nd, 2005, 11:04 AM
thanks for using the [code] tags to make the code more readable

Tcoz
June 22nd, 2005, 11:27 AM
Seno, what an *** you are. You actually go to other forums just to post your wiseass quips?

I'll put the word out about you. Good luck finding work in my area.

Post here if you have a problem with that. We can meet maybe.

senocular
June 22nd, 2005, 11:28 AM
I think you just need to learn to take a little sarcasm a little better. It'll help you get through life without your head exploding ;)


http://www.senocular.com/smilies/explode.gif


and don't worry, I got this one for ya :D

Tcoz
June 22nd, 2005, 11:35 AM
That's not sarcasm. You're just a prick, and apparently enjoy it. You literally followed me to this forum when I mentioned it in UShock just to see if you could find this post so you could post your rudeness.

And I'm serious about that meeting.

ElectricGrandpa
June 22nd, 2005, 12:12 PM
Roar, grr, meow meow!

fasterthanlight™
June 22nd, 2005, 02:55 PM
tcoz, sen is one of kirupa forums most valued members. If you don't want his help then go sit in the corner by yourself. And i'm sure telling people in his area about his "quips" won't damage his chances of getting work because he's got skills. Skills far superior than yours.
And what's with this meeting, you going to knock his head? Reallll mature there buddy.

senocular
June 22nd, 2005, 03:02 PM
now now now, its ok. I did pull some of Tcoz's strings. Its no biggie. Lets just try to benefit from the generous contribution of code :)

fasterthanlight™
June 22nd, 2005, 03:06 PM
now now now, its ok. I did pull some of Tcoz's strings.

Thats even funnier then. Hahaha

Tcoz
June 22nd, 2005, 03:13 PM
/flip FanboiThenSlight. I didn't start this. Your pet Mr. Valuable did. He was rude in the US forums, happened to see I mentioned Kirupa, and he actually came here to repost, solely to be a ****. He was the aggressor, not me. There's your professional, highly skilled, valuable forum member.

You know **** these forums if guys like you and this seno character are what passes for "valuable".

imported_ramie
June 22nd, 2005, 03:36 PM
LOL, all he said on ultra was "thanks for using the as code tag"

unless there is some serious reading between the lines here......... CHILL

ElectricGrandpa
June 22nd, 2005, 11:11 PM
You know **** these forums if guys like you and this seno character are what passes for "valuable".

Bye! :D