PDA

View Full Version : Custom Attributes and Custom Events


antun
11-26-2003, 09:33 AM
If you define your own attributes in your own classes, they automatically send out an event when they change. You can capture that event using a LzDelegate, which can sometimes be a cumbersome process. Often it's easier just to write a method that's bound to it. You've probably seen something like:


<method event="ondata">

</method>


If you declare custom attributes using the <attribute> tag, you can do the same. If your attribute is called "foo", then the corresponding event will be called "onfoo".

This works in user-defined classes, both when the onfoo method is written in the class definition, and in the instance, as well as instances of views:


<canvas debug="true">
<!-- box class -->
<class name="box">
<attribute name="foo" type="string" value="hello" />
</class>

<!-- otherbox class -->
<class name="otherbox">
<attribute name="foo" type="string" value="hello" />

<method event="onfoo">
debug.write( "foo was changed to " + this.foo );
</method>
</class>

<!-- instances of the above classes -->
<box name="boxInstance">
<method event="onfoo">
debug.write( "foo was changed to " + this.foo );
</method>
</box>

<otherbox name="otherBoxInstance" />


<!-- single view; not an instance of a user-defined class -->
<view name="rawInstance">
<attribute name="foo" type="string" value="hello" />
<method event="onfoo">
debug.write( "foo was changed to " + this.foo );
</method>
</view>

<!-- buttons to change the foo attributes -->
<simplelayout axis="y" spacing="20" />

<button onclick="boxInstance.setAttribute('foo','bar')">
Change the value of foo in the box instance
</button>

<button onclick="otherBoxInstance.setAttribute('foo','ta')">
Change the value of foo in the otherbox instance
</button>
j
<button onclick="rawInstance.setAttribute('foo','smelly')">
Change the value of foo in rawInstance
</button>
</canvas>


Enjoy!