Chapter Contents |
Previous |
Next |
SAS Component Language: Reference |
Event handlers are methods that listen for these messages and respond to the them. Essentially, an event handler is a method that determines which method to execute after the event occurs.
SCL supports both system events and user-defined events.
System Events |
System events include user interface events (such as mouse clicks) as well as "attribute changed" events that occur when an attribute value is updated. SCL automatically defines system events for component attributes when those attributes are declared.
SCL can also automatically send system events for you when a component's
attribute is changed. If you want "attribute changed" events to
be sent automatically, specify SendEvent='Y'
in the options list for the attribute.
If you want an action to be performed when the system event occurs, then you need to define the event handler that you want to be executed when the event occurs. You define event handlers for system events in the same way that you define them for user-defined events. See Defining Event Handlers for more information.
Defining and Sending Events |
EVENT event-name</(event-options)>; |
Event names can be up to 256 characters long.
For the event options, you can specify the name of the method that handles
the event and when an object should send the event. Events can be sent automatically
either before (specify Send='Before'
) or after
(Send='After'
) a method executes or they can
be programmed manually ('Manual'
) with SCL.
New events default to 'After'
. You must specify
a method name for events that are to be sent automatically.
After an event is defined, you can use the _sendEvent method to send the event:
object._sendEvent("event-name"<, event-handler-parameters>); |
Defining Event Handlers |
You can define event handlers with event handler declaration statements in CLASS blocks.
EVENTHANDLER event-handler-name</(event-handler-options)>; |
As part of the event handler options, you can specify the
name of the
event, the name of the method that handles the event, and the name of the
object that generates the event (the sender). As the sender, you can specify '_SELF_'
or '_ALL_'
. When
Sender='_SELF_'
, the event handler listens only to events
from the class itself. When Sender='_ALL_'
,
the event handler listens to events from any other class.
Using the _addEventHandler method, you can dynamically add a sender to trigger the event. For a complete description of _addEventHandler, refer to the SAS/AF online Help.
For more information about defining event handlers, see CLASS.
Example |
The following
class
defines one user-defined event, myEvent, and the event handler for this event,
M2. When this class is created, SCL also assigns the system event name "n
Changed" for the attribute n
and registers
the event name with the component.
class EHclass; public num n; /* system event */ event 'myEvent' / (method='M2'); eventhandler M1 / (sender = '_SELF_', event = 'n Changed'); eventhandler M2 / (sender = '_SELF_', event = 'myEvent'); M1: method a:list; put "Event is triggered by attribute n"; endmethod; M2: method a:string n1:num n2:num; put "Event is triggered by _sendEvent"; put a= n1= n2=; endmethod; endclass;
When the value of the attribute n
is
changed, the system automatically sends the "n Changed" event,
and method M1 is executed. Method M2 is not executed until myEvent is sent
with the _sendEvent method.
The next class, EHclass1, defines a second event handler, M3, that is also executed when myEvent is sent.
class EHclass1; /* Sender='*' means that the sender */ /* is determined at run time. */ eventhandler M3 / (sender = '*', event='myEvent'); M3: method a:string n1:num n2:num; put "Event myEvent is defined in another class"; put "that is triggered by _sendEvent."; put a= n1= n2=; endmethod; endclass;
In the following program, the system event "n Changed" is
triggered when the value of the n
attribute
is modified. The user-defined event myEvent is triggered with the _sendEvent
method.
import work.a.EHclass.class; import work.a.EHclass1.class; init: dcl EHclass obj = _new_ EHclass(); dcl EHclass1 obj1 = _new_ EHclass1(); /* Trigger the system event. */ obj.n = 3; /* Trigger the user-defined event. */ obj._sendEvent("myEvent", 'abc', 3, 4); return;
The order in which the two classes are instantiated determines the order in which the event handlers for myEvent are executed. EHclass is instantiated first, so when myEvent is sent, event handler M2 is executed first, followed by the event handler defined in EHclass1, M3.
The output from this test program is
Event is triggered by attribute n Event is triggered by _sendEvent a=abc n1=3 n2=4 Event myEvent is defined in another class that is triggered by _sendEvent. a=abc n1=3 n2=4
Event and Event Handler Metadata |
init: DCL num rc; DCL list metadata; DCL object obj; obj=loadclass('class-name'); rc=obj._getEvent('event-name',metadata); call putlist(metadata,'',3); rc=obj._getEventHandler('_self_','event-handler-name', '_refresh',metadata); call putlist(metadata,'',3); return;
Chapter Contents |
Previous |
Next |
Top of Page |
Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.