Name

LzEvent — The sender in Laszlo's point-to-point event system.

Synopsis

JavaScript: LzEvent
Type: Class
Access: public
Topic: LFC.Events
Declared in: WEB-INF/lps/lfc/events/LaszloEvents.lzs

Description

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 LzDelegate 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.

Example 9. A simple example of publishing and listening for a custom event

 <canvas height="40">
   <simplelayout/>
   <button name="eventSender" 
           
 
   <event name="customevent"/>
   <view bgcolor="red" width="20" height="20" oninit="this.setupDelegate()">
     <method name="setupDelegate">
       this.del = new LzDelegate( this, "respondToEvent" );
       
     </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.

Example 10. Event sending in response to setting an attribute

 <canvas height="40">
   <simplelayout/>
   <button name="eventSender" 
           >
     <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, );
     </method>
     <method name="respondToEvent" args="v">
       this.setAttribute('x' , v);
     </method>
 
   </view>
 </canvas>
 

Superclass Chain

LzEvent

Known Subclasses

Details

Properties (1)

locked
public var locked : Boolean = false;
True when event is being sent.

Methods (8)

addDelegate()
public function addDelegate(d : LzDelegate);
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
clearDelegates()
public function clearDelegates();
Removes all delegates from call list
_dbg_name()
private function _dbg_name();
If debugging, add an informative name
getDelegateCount()
public function getDelegateCount() : Number;
Returns the number of delegates registered for the event
initialize()
public function initialize(eventSender : Object, eventName : String, d);
removeDelegate()
public function removeDelegate(d : LzDelegate);
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
sendEvent()
public function sendEvent(sd);
Sends the event, passing its argument as the data to all the called delegates
toString()
public function toString();

JavaScript Synopsis

public LzEvent {
  public var locked  : Boolean = false;
  prototype public function addDelegate (d : LzDelegate);
  prototype public function clearDelegates ();
  prototype private function _dbg_name ();
  prototype public function getDelegateCount () : Number;
  prototype public function initialize (eventSender : Object, eventName : String, d);
  prototype public function removeDelegate (d : LzDelegate);
  prototype public function sendEvent (sd);
  prototype public function toString ();
}