jgDelegate

Seems like everyone has a custom version of the Delegate these days. Whether it’s to proxy events or control scope, one thing is clear, the solutions and problems are just as numerous as its applications. The question I haven’t figured out yet is; which is the best?

jgDelegate
___________________________

Other Great Delegate Classes
___________________________

Scope Issues
This is all about scope, find out about Scope Chain and Memory waste in Flash MX!

Proxy Issues
Ok, how about this… inspired by the Delegate Class refined, in conjunction with MTASC compliant Class Delegate, here’s a version that is a bit more hidden but doesn’t pollute the arguments in the end function while still maintaining the ability to remove itself from event subscriptions. I don’t really understand the whole MTASC thing because I don’t use it, so any comments to why this is a better/worse way would be good.

The reference to the subscription function can be found inside the event function using “arguments.callee.self”. This is useful when you are using event proxies. If you don’t want to access it in the proxied event then you should probably create a variable for the delegate and subscribe/unsubscribe that way ( that’s another conversation - var f = Delegate.create( this, func) ).

Since it’s essentially storing another variable in the function, I’d be curious to run some performance tests. Here is the first version:


function create (s:Object, func:Function):Function
{
return function():Void{ func.apply(s, arguments); };
}

Here is the second version:

function create( obj:Object, func:Function ):Function
{
// SCOPED FUNCTION
// ______________________________
//
// dFunc = jgDelegate2.create ( this, o );
// dFunc ( ‘Hello World’ ); // Hello World
// ______________________________
//
// obj.addEventListener( ‘onEvent’, create ( listener, onEvent ) );
// obj.removeEventListener( ‘onEvent’, arguments.callee.self ); // onEvent var f = function ( )
{
var scope = arguments.callee.scope; // scope
var func = arguments.callee.func; // function
func.self = arguments.callee; // refernce for listeners return func.apply( scope, arguments); // method
}; f.scope = obj;
f.func = func;

return f;
}

Check it out with this example:

// create
var obj = {};
var listener = {};// initialize
mx.events.EventDispatcher.initialize ( obj );// listen
obj.addEventListener(onEvent, create ( listener, onEvent ) );// event
function onEvent ( eventObj:Object )
{
trace( eventObj.msg );
obj.removeEventListener(onEvent, arguments.callee.self ); // stop listening
}

// broadcast
var eventObj =
{
target:obj, // origin of event
type:onEvent, // event
msg:Hello World// additional properties
}

»     » del.icio.us     » Digg It!

Tags: , , , , , , , , , , ,

4 Comments »

  1. jgraup said,

    December 6, 2006 @ 5:00 pm

    Check Back:
    http://timotheegroleau.com/Flash/articles/scope_chain.htm
    http://stimpson.flashvacuum.net/mt/archives/2005/01/article_three_a.html
    http://dynamicflash.com/2005/02/delegate-class-refined/ - memory leaks
    http://www.darronschall.com/weblog/archives/000126.cfm
    http://www.person13.com/articles/proxy/Proxy.htm
    http://www.person13.com/ascblibrary/

  2. jgraup said,

    December 6, 2006 @ 6:53 pm

    Delegates in AS 3.0
    http://stimpson.flashvacuum.net/mt/archives/2006/08/as_30_delegate.html

  3. jgraup said,

    February 17, 2007 @ 11:17 pm

    Flash Quirk: Disabled buttons still broadcast click
    http://www.darronschall.com/weblog/archives/000123.cfm

  4. JUST GOOD DESIGN | BLOG » jgDelegate » Rendition Multimedia said,

    October 11, 2007 @ 10:18 am

    […] JUST GOOD DESIGN | BLOG » jgDelegate […]

RSS feed for comments on this post · TrackBack URI

Leave a Comment

You must be logged in to post a comment.

best kansas city bookstore pirodr! 666