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 <handler>s use LzDelegate.register() to register to receive events.
An event is implicitly declared for every attribute of a class
or instance and sent when that attribute is changed. So, for
instance, if a class has an attribute foo
, you can
receive events when foo changes by registering for the
onfoo
event.
Events are sent with a single argument, which usually conveys
information about the property that changed. The default behavior
of the LzEventable.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.
Example 14. Event sending in response to setting an attribute
<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
">
// Handle the implicit `onavalue` event of the `eventSender` button
<handler
name
="onavalue
" reference
="eventSender
" args
="v
">
this.setAttribute('x' , v);
</handler
>
</view
>
</canvas
>
An event can be explicitly declared using the <event>
tag and the sendEvent
method is used to explicitly send an event.
Example 15. A simple example of publishing and listening for a custom event
<canvas
height
="140
" debug
="true
">
<simplelayout
/>
<button
name
="eventSender
" onmouseover
="this.customevent.sendEvent('over')
" onmouseout
="this.customevent.sendEvent('out')
">
<event
name
="customevent
"/>
</button
>
<view
bgcolor
="red
" width
="20
" height
="20
">
<handler
name
="customevent
" reference
="eventSender
" args
="event
">
Debug.debug("event: %s", event);
this.setAttribute('x', this.x + 10);
</handler
>
</view
>
</canvas
>
Sending an event that no delegate is listening for has nearly no cost, thus objects can freely publish events.
See the Developer's Guide for more detailed information on using events and delegates.
Methods
sendEvent() |
---|
Parameter Name | Type | Description |
---|---|---|
eventValue | * | (Optional) The value to pass when executing
the delegates registered to receive the event. If omitted,
null will be sent.
|
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.