PDA

View Full Version : Notes on events


antun
05-02-2003, 02:53 PM
You might have seen event handlers in use in tags, and they will probably be familiar if you've ever written any HTML and JavaScript:


<view oninit="this.myMethod()">
<method name="myMethod">
// JavaScript code.
debug.write( "Hello, World!" );
</method>
</view>


In fact in LZX a very common practice is to have code execute when a view initializies. Another way to achieve the same result in this case is to give view a method, and attach it to an event:


<view>
<method event="oninit">
// JavaScript code.
debug.write( "Hello, World!" );
</method>
</view>


Clearly the first method is quicker for writing short snippets of code (and debugging too!), whilst the second approach is more suited for larger chunks of code.

Remember that not every event can be used with the first method (i.e. as an event handler). It has to be an event handler attribute (see the LZX Reference for more information on these). So while onclick, oninit, onwidth and onx are all events that get sent, only onclick and oninit are event handler attributes. The second method (using method event="...") can be used with any event. You can even use it for custom attributes:


<canvas debug="true">
<view width="100" height="20">
<attribute name="foo" value="3" type="number" />
<method event="onfoo">
debug.write( 'foo has changed to: ' + this.foo );
</method>
<button onclick="parent.setAttribute( 'foo', 5 )"
label="Set foo to 5" />
</view>
</canvas>


Note that you must use the setAttribute() method when you are using custom attributes and events.

Finally the onclick event handler is special in that it automatically sets the clickable attribute to true.

Enjoy!