Delegate

Kind of class:class
Inherits from:none
Implements:
Version:1.0
Author:Francis Bourre
Classpath:com.bourre.commands.Delegate
File last modified:Wednesday, 31 October 2007, 07:35:29
Delegate was originally created by Mike Chambers
for Macromedia mx.events package.

As he said, this is a reusable class that acts as a proxy for events,
registering itself for specific events, and then proxying
the calls to the event handler and scope you specify.

This provides the following benefits and advantages :

  • You can dispatch change events from two different components to two separate methods.
  • You can define the scope in which the event handler would be called (in this case, the containing class).
  • Because it is a separate class, it was modular and you can easily reuse it across projects.

For more explanations and a complete tutorial,
see there.

This version is also inspired from Peter Hall's EventDelegate.
You can instantiate and keep a reference of a Delegate instance.
For more informations, see there.

So, what are the new additions ?

- You can pass parameters and add some more further.
See examples below.

- One of the greatest feature is Command pattern implementation.
See classes documentation below to figure out benefits :

  • com.bourre.commands.ThreadManagerFPS
  • com.bourre.commands.ThreadManagerMS
  • Batch

Here is a basic example with v2 Button component and setArguments use :

Example:
import com.bourre.commands.*;
import mx.controls.*;

function test(event, a) : Void
{
    event.target.label = String(++a);
    d.setArguments(a);
}

var d:Delegate = new Delegate(this, test);
var pb:Button = createClassObject(Button, '__pb', 10, {label:'0'});
pb.addEventListener('click', d);
d.setArguments(0);

Constructor

Delegate

function Delegate (
o:Object, f:Function)
Constructs a new Delegate instance.
Parameters:
o:
Scope to be used by calling this method.
f:
Method to be executed.

Class methods

create

static function create (
o:Object, f:Function) : Function
Wrapper for MM compatibility.
Creates a method that delegates its arguments to a specified scope.
Parameters:
o:
Scope to be used by calling this method.
f:
Method to be executed.
Returns:
Function that delegates its call to a different scope, method and arguments.

Instance methods

addArguments

function addArguments (
) : Void
Add arguments to proxy method.

Here is a basical example with Delegate.addArguments :

Example:
import com.bourre.commands.*;

function test() : Void;	 * {
    trace(arguments);
}
function temporisation();	 * {
    d.execute();
    d.addArguments( Math.round(Math.random()*9) );
}

var d:Delegate = new Delegate(this, test, 0);
setInterval(temporisation, 100);

callFunction

function callFunction (
)
Execute proxy method in the provided context.
No return type is provided for interfaces compatibility.
Returns:
Returns result if there's one.

execute

function execute (
e:IEvent) : Void
Execute proxy method in the provided context.
Command polymorphism.
See also:
com.bourre.commands.command
Specified by:

getFunction

function getFunction (
) : Function
Get proxy reference.
Returns:
Function that delegates its call to a different scope, method and arguments.
Example:
import com.bourre.commands.*;

var d:Delegate = new Delegate(this, test, "hello world");

function test( s:String ) : Void; * {
trace( s );
}

mc.onPress = d.getFunction();

getScope

function getScope (
)
Get scope reference.
Returns:
Scope to be used for method calling.

setArguments

function setArguments (
) : Void
Set or change arguments of proxy method.
See examples above.

toString

function toString (
) : String
Returns the string representation of this instance.
Returns:
the string representation of this instance

Event handlers

onEnterFrame

function onEnterFrame (
) : Void
Triggers at each displayed frame by the player.
#