PDA

View Full Version : Why do some events have event listeners while others don't?


nmduc073
11-29-2007, 06:48 AM
I have seen the code of some examples. And I notice that some events don't need the event listener. It makes me confused. I don't know what it is for. Please explain it to me.

Flash Gordon
11-29-2007, 06:55 AM
example?

nmduc073
11-29-2007, 07:35 AM
In this example, it has nameChanged and ageChanged events. However, they don't have any listener.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute" width="305" height="231"
viewSourceURL="/files/BindableThreeTutorial.mxml">
<mx:Script>
<![CDATA[
[Bindable(event="nameChanged")]
private var personName:String;

[Bindable(event="ageChanged")]
private var personAge:String;

private function updateInfo():void
{
dispatchEvent(new Event("nameChanged"));
dispatchEvent(new Event("ageChanged"));
}
]]>
</mx:Script>
<mx:Binding source="txtName.text"
destination="personName"/>
<mx:Binding source="txtAge.text"
destination="personAge"/>
<mx:Panel x="0" y="0" width="305"
height="231" layout="absolute"
title="Bindable Event Dispatching Tutorial">
<mx:Label x="10" y="10" text="Name:"/>
<mx:Label x="10" y="38" text="Age:"/>
<mx:TextInput x="75" y="8" id="txtName"/>
<mx:TextInput x="75" y="36" id="txtAge"/>
<mx:Button x="10" y="64" label="Update Info"
click="updateInfo()"/>
<mx:HRule x="0" y="94" width="285" height="4"/>
<mx:Label x="10" y="106" text="Bound Data"
fontWeight="bold"/>
<mx:Label x="10" y="132" text="Name:"/>
<mx:Label x="10" y="158" text="Age:"/>
<mx:Label x="72" y="132" id="lblName"
text="{personName}"/>
<mx:Label x="72" y="158" id="lblAge"
text="{personAge}" />
</mx:Panel>
</mx:Application>

Flash Gordon
11-29-2007, 07:44 AM
I don't really know Flex, but just because something dispatches an event doesn't mean you have to listen for it.

sugo
11-29-2007, 10:17 AM
Bindable variables are variables, that if changed will reflect the change of value on screen. Bindable variables are very useful for example if you have live sports results on page...with bindable variables scores will change whenever any changes occur

In your example you are saying to Flex that for example variable personName is Bindable and that it is changing upon a event called nameChanged.

(it is [Bindable(event="nameChanged")] private var personName:String;)

So when you dispatch event with name nameChanged, bindable variable personName will know that it must change, because the event was called, so basically it is the same thing as adding an eventListener that is listening for nameChanged event...

Hope it helps

nmduc073
11-30-2007, 01:13 AM
Thank for your explaination.