event是建立在对象之间通信的机制。比如当鼠标点击的时候,从服务器接受数据的时候,或者当view建立的时候。Events are the mechanism by which objects communicate with each other when something changes. For example, an event might be generated when a mouse button is clicked, or when data arrives from a server, or when a view has been constructed.
在LZX中,events不是广播,是使用delegates的点对点的通信。当事件发生的时候这些点是基本的功能点。
In LZX programs, events are not broadcast, but rather they are communicated in a point-to-point fashion using delegates
, which are basically function pointers that are referenced when events happen.
这种措施增加了灵活性和减少了使用events的负担。
This implementation increases flexibility and reduces the overhead of using events. However, for the purposes of the discussion in this chapter we're going to ignore delegates and speak of events in a slightly less 严密的方式, saying, for example when such-and-such an event occurs, thus and such happens, leaving aside for now an explanation of how it happens.
Views have 大约two dozen defined events, as listed on the entry for <view>
in the LZX Reference Manual. Many of these events that deal with user input, such as onblur
, onclick
, onkeydown
, will be familiar to JavaScript programmers. Other events, such as onheight
and onopacity
, pertain to views' visible attributes. Finally, the oninit
and onconstruct
events are related to the creation of the instances of view objects. Similarly, other system-defined LZX objects such as Datasets (see below) have events associated with them.
Events and attributes often 成对出现, and in fact, the default behavior of the setAttribute()
method is to set the named property and send the event called "on
" + property. For instance, when a view changes its x
(horizontal) position, it sends the event onx
with the new value for its x
property. This means that in addition to system-defined events, there exists an event for each attribute you define.
When an event happens, control is transferred to its associated event-handler (if one is defined). Events can be sent with a single argument, which usually conveys information about the property that changed.
Elsewhere in this Guide we discuss how events are implemented in LZX, and how the event architecture bears upon program design. In particular, Chapter 30, Delegates discusses the relationship between events and delegates.
In LZX, a method is a JavaScript function associated with a particular object.
Functions are invoked using the ()
operator. Thus,
<view name="dog"> <method name="bark"> <!-- some JavaScript code --> </method> </view>
Defines a function that is executed when invoked by name, as in
dog.bark();
In JavaScript, the this
keyword is used to refer to the object through which the function was invoked.
A handler is like a method. But whereas a method is invoked by its name, a handler must be associated with a particular event. The handler script will be executed when the referenced view receives an event with this name. For example,
<view> <handler name="onclick"> <!-- some JavaScript code --> </handler> </view>
defines a function that is executed when the view is clicked on. Consider
<view>
<method name="bark">
<!-- some JavaScript code -->
</method>
<handler name="onclick">
this.bark()
</handler >
</view>
When the view is clicked on, the onclick
event causes the execution of the handler()
which in turn invokes the method named bark()
. Event handlers are often identified in an opening tag, as in
<view οnclick="clickHandler()"> <method name="clickHandler"> <!-- some JavaScript code --> </method> </view>
There are three general categories of methods and handler:
-
"On init" methods that are invoked when their parent object is created;
-
handlers that are invoked when their parent object receives a specified event;
-
named methods that are explicitly invoked by other methods.
Note that you can define a method using conventional JavaScript syntax, but in LZX the preferred way to declare a method is with the <method>
tag.
Also note that in LZX, unlike many other object-oriented systems, you can override a method in an instance of an object. This topic is covered in Chapter 33, Extending Classes.