Wednesday, July 11, 2007

AS 3.0 Delegate

in AS3 how can we pass extra arguments in function ?
see http://stimpson.flashvacuum.net/mt/archives/2006/08/as_30_delegate.html

== code ==
package com.bdcreations.utils{


public function
Delegate(target:*, handler:Function, ...rest:Array):Function {


// Create delegate function, ...delRest accounts for any pre-existing handler arguments (event objects for example)
// so there will be no argument count mismatch with the compiler
var delegate:Function = function(...delRest) {
// Augment pre-existing handler arguments with additional args

var fullArgs:Array = delRest.concat(rest);
// Call handler with all arguments

return handler.apply(target, fullArgs);
}


// Return the delegate function.
return delegate;
}
}


Unfortunately I can't pass in the delegate function itself as a argument because of the argument mismatch in the compiler. This might be an issue with garbage collection, so be sure to use weak references where you can:

import com.bdcreations.utils.Delegate;

dispatcher.addEventListener(Event.COMPLETE, Delegate(this, eventHandler, "aditional", "argument"), false, 0, true);

function
eventHandler(event:Event, arg1:String, arg2:String):void {

trace
(event);
trace
(arg1);
trace
(arg2);
}


==
or create custom event class

In AS 3.0 Event is an actual class istead of just an object, so you can now actually extend Event and create yur own events.

package
{

import flash.events.Event;

class CustomEvent extends Event
{

public static const MOUSE_DOWN:String = "onMouseDown";
public var myString:String;

public function CustomEvent( type:String, str:String )
{
myString = str;
super( type );
}
}
}

you could then dispatch this like so
//you would have to import your custom event
dispatchEvent( new CustomEvent( CustomEvent.MOUSE_DOWN, "1,2,3") );

stuff.addEventListener( CustomEvent.MOUSE_DOWN, dosomething );

private function dosomething( event:CustomEvent ):Void
{
trace( event.myString ) // 1,2,3
}

If you want to add custom properties to an Event object, you must extend the Event object and define the new properties in your own custom class. You can then manually dispatch your custom events with the dispatchEvent() method, as you would any event.

If you create a custom ActionScript class that dispatches its own events but does not extend UIComponent, you can extend the flash.events.EventDispatcher class to get access to the addEventListener(), removeEventListener(), and dispatchEvent() methods.


see another post here
http://wildwinter.blogspot.com/2007/04/come-back-delegate-all-is-forgiven.html


// (c) 2007 Ian Thomas
// Freely usable in whatever way you like, as long as it's attributed.
package net.wildwinter

{

public class
Callback
{

// Create a wrapper for a callback function.
// Tacks the additional args on to any args normally passed to the
// callback.
public static function create(handler:Function,...args):Function

{

return function
(...innerArgs):void
{

handler.apply(this,innerArgs.concat(args));
}
}
}
}



// AS3 code
public function initDisplay():void

{

var
button1:UIButton={some button I've created};
var button2:UIButton={some other button I'
ve created};
button1.addEventListener(MouseEvent.CLICK,Callback.create(onButtonPressed,1));

button1.addEventListener(MouseEvent.CLICK,Callback.create(onButtonPressed,2));
}


private function
onButtonPressed(event:MouseEvent,num:Number):void
{


trace
("Button "+num+" has been pressed!");
}

No comments: