A delegate is an object that calls a specific method on a specific object when its execute method is called. It is essentially a method closure.
Delegates, along with <event>, comprise Laszlo's point to point event system. A delegate represents a named method of an object. Delegates are mostly registered with events, but they can be used anywhere a function callback would traditionally be called for: for instance, LzTimerService.addTimer() takes a delegate as its argument.
You can create a delegate explicitly using the
LzDelegate class, or implicitly by
specifying an event handler. There are two syntaxes with which
you can specify an event handler: in the open tag used to create
that object, or by using the
<handler> tag in the body of the object.
To specify an event handler in an open tag, simply include it like any other attribute.
<view onmouseover="doSomething()"> <method name="doSomething"> // code to be executed when mouse is over the view </method> </view>
To specify an event hander using the
<handler> tag, you include it in the body of
<view> <handler name="onmouseover"> // code to be executed when the mouse is over the view </name> </view>
The above two examples are functionally equivalent. Using the
<handler> tag, however, can often lead to more
readable code because it removes clutter from the object creation
tag. This is especially true if the handler code is complex.
Note: When creating a delegate explicitly using
methodName must be a
method of one parameter, because it will be invoked by LzDelegate.execute() with one argument
(typically the argument that is passed to LzEvent.sendEvent() when that delegate is
registered to receive events). Similarly, when specifying an
event handler method using
... method="methodName" />, the handler method must
accept one argument (even if it ignores it).
See also the code examples at <event> and the Developer's Guide for more detailed information on using events and delegates.
|context||lz.Eventable||The object whose method will be called by execute.|
|methodName||String||The name of the method to call (a string). Must be a method of one parameter.|
|eventSender||lz.Eventable||(Optional) The sender of the event to register the new delegate for.|
|eventName||String||(Optional, but required if eventSender is used) The name of the event to register the new delegate for.|
An event is initially declared to contain the sentinel value LzDeclaredEvent which is of the correct type, but does not actually support registering for and sending events. When a delegate attempts to register on this sentinel, the sentinel will automatically be replaced with a functional LzEvent.
The valid event logic is duplicated in NodeModel#addHandlerInternal (to give better location information for handler references).
|eventSender||lz.Eventable||The object which publishes the event.|
|eventName||String||The name of the event to register for.|
|event||lz.event||The event to unregister the delegate from. (e.g. myview.onmouseup)|
Copyright © 2002-2010 Laszlo Systems, Inc. All Rights Reserved. Unauthorized use, duplication or distribution is strictly prohibited. This is the proprietary information of Laszlo Systems, Inc. Use is subject to license terms.