The sender in Laszlo's point-to-point event system.
Events underly most of the functionality in OpenLaszlo applications. Unlike events in similar systems, OpenLaszlo's events are point-to-point, meaning that there is no general broadcast mechanism for events, and events do not trickle up or down the instance hierarchy. Instead, objects called delegates register for events, and if they try to register for an event that doesn't exist yet, the system creates the event.
You can create a delegate explicitly using the LzDelegate class, or implicitly by creating an handler.
Because of the loose type requirements in LZX, calling an event that no delegate is listening for (and which therefore hasn't been created) has no effect. This allows objects to publish many more events than they actually need to create at runtime.
There are two syntaxes with which you can specify an event handler: in the tag used to create that object, or by using the
<handler> tag.
To specify an event handler in an object-creation tag, simply include it like any other attribute. For example,
<view onmouseover="doSomething()">
<method name="doSomething">
// code to be executed when mouse is over the view
</method>
</view>
If you use the <handler> tag, you do not need to include the handler in the tag that creates the object.
<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.
Use the <event> tag to create the events; then use the sendEvent method to dispatch it. The following example illustrates how to create custom events.
<canvas height="40">
<simplelayout/>
<button name="eventSender"
onmouseover="this.customevent.sendEvent()"
onmouseout="this.customevent.sendEvent()"/>
<event name="customevent"/>
<view bgcolor="red" width="20" height="20" oninit="this.setupDelegate()">
<method name="setupDelegate">
this.del = new LzDelegate( this, "respondToEvent" );
this.del.register( eventSender , "customevent" );
</method>
<method name="respondToEvent">
this.setAttribute('x', this.x + 10);
</method>
</view>
</canvas>
Events can be sent with a single argument, which usually conveys information about the property that changed. The default behavior of the setAttribute method is to set the named property and send the event called "on" + property. This is general mechanism that updates constraints in a OpenLaszlo programs. For instance, when a view changes its x position, it sends the event onx with the new value for its x property.
<canvas height="40">
<simplelayout/>
<button name="eventSender"
onmouseover="this.setAttribute('avalue', this.avalue + 10)"
onmouseout="this.setAttribute('avalue', this.avalue + 5)">
<attribute name="avalue" value="0"/>
</button>
<view bgcolor="red" width="20" height="20" oninit="this.setupDelegate()">
<method name="setupDelegate">
this.del = new LzDelegate(this, "respondToEvent");
this.del.register(eventSender, "onavalue");
</method>
<method name="respondToEvent" args="v">
this.setAttribute('x' , v);
</method>
</view>
</canvas>
| Attributes | |||||
| Name | Usage | Type (Tag) | Type (JS) | Default | Category |
| locked | JS only | readonly | |||
| Bool which is true when event is being sent. | |||||
|
|
|||||
| addDelegate() | ||
| LzEvent.addDelegate(d) | ||
|
Adds the given delegate to the event's delegate list. Although this listed as a public function it should rarely be called explicitly -- it is used exclusively by LzDelegate.register |
||
| Parameters | ||
| Name | Type | Desc |
| d | LzDelegate | The delegate to add to the list of delegates called by the event. |
|
|
||
| clearDelegates() | ||
| LzEvent.clearDelegates() | ||
|
Removes all delegates from call list |
||
|
|
| constructor() | ||
| LzEvent(eventSender, eventName, d) | ||
|
An event is an object which stores delegates. When it is called, it calls all of its delegates in turn. Due to the dynamic (and lazy) nature of event instantiation, events store themselves in their containing object in two ways: first, in slot equivalent to their name (view[eventName] = event) and second, in an array in the containing object (which it will create if it doesn't exist) called _events |
||
| Parameters | ||
| Name | Type | Desc |
| eventSender | Object | The owner of this event |
| eventName | String | The name of this event. |
| d | None | |
|
|
||
| getDelegateCount() | ||
| LzEvent.getDelegateCount() | ||
|
Returns the number of delegates registered for the event |
||
| Returns | ||
| Type | Desc | |
| Number | The number of delegates registered for the event. | |
|
|
||
| removeDelegate() | ||
| LzEvent.removeDelegate(d) | ||
|
Removes the delegate from the delegate list. In practice, this is rarely called explicitly, since it does not update the delegate's list of stored events. Right now, this is called only by LzDelegate. unregisterAll Delegates should support a simple unregister command, that unregisters them for a single event, but to date, that has not proven necessary |
||
| Parameters | ||
| Name | Type | Desc |
| d | LzDelegate | The delegate to remove from the delegateList. |
|
|
||
| sendEvent() | ||
| LzEvent.sendEvent(sd) | ||
|
Sends the event, passing its argument as the data to all the called delegates |
||
| Parameters | ||
| Name | Type | Desc |
| sd | The data to send with the event. | |
|
|
||
Copyright © 2002-2005 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.