View comments | RSS feed

Delegate class

Inheritance Object > Delegate

ActionScript Class Name mx.utils.Delegate

The Delegate class lets you run a function in a specific scope. This class is provided so that you can dispatch the same event to two different functions (see Delegating events to functions in Using Components), and so that you can call functions within the scope of the containing class.

When you pass a function as a parameter to EventDispatcher.addEventListener(), the function is invoked in the scope of the broadcaster component instance, not the object in which it is declared (see Delegating the scope of a function in Using Components). You can call Delegate.create() to call the function within the scope of the declaring object.

Method summary for the Delegate class

The following table lists the method of the Delegate class.

Method

Description

Delegate.create()

A static method that allows you to run a function in a specific scope.


Version 8

Comments


Art Clifford said on Oct 10, 2006 at 2:13 AM :
The Delegate class is a way under-advertised object. Really, most of the documentation on this system for setting up event handlers should refer to the Delegate class.

The Delegate.create function, it should be pointed out returns a Function, and that function has whatever scope you give it, that means for any handler type that you are passing a function to, that Delegate.create can be used to specify a function you want to call with the scope you want.

Even that sounds techy. The issue of scope is that say for myLoadVars.onLoad = function()
{
trace( this);
}
or
function varsLoaded() ={ trace(this); }
myLoadVars.onLoad = this.varsLoaded;

"this" will refer to the myLoadVars object. That's great it shouldl right?

Well, it is great if your function varsLoaded is not in the middle of a class whose properties you want to access. The only way to access the rest of the class would be to pass myLoadVars a reference that can be accessed during the handler execution. For instance:
var msg:String = "hello world";
myLoadVars.surroundingClass=this;
myLoadVars.onLoad = function{ trace(this.surroundingClass.msg);}

However, with Delegate you can get around all that:

var msg:String = "Hello World";
myLoadVars.onLoad = Delegate.create( this, this.varsLoaded );

function varsLoaded( sucess: Boolean)
{
trace(this.msg);
}

The only places I recall seeing Delegate shown is with respect to addEventListener, it is never referred to with respect to the onHandlers. I just figured it out by trial and error and am wondering why there isn't a common link under every Events section of a class doc page to an event handling tutorial that includes using Delegate?

-ArtC
burtonrider1983 said on Apr 16, 2007 at 4:24 PM :
The documentation for delegating events can be found here...
http://livedocs.adobe.com/flash/8/main/wwhelp/wwhimpl/js/html/wwhelp.htm?href=Part4_ASLR2.html

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/8/main/00003430.html