This is the third piece in a snippet tutorial series about the Bindable meta tag. This time I am going to go through how to use custom events with your bindable objects. Part 1 of the series can be found here and part 2 can be found here, they cover the basics of the tag and how to use it in simple situations.
The example program works like this: you can enter a name and age and these will be bound to two variables, which also are bound to a couple labels on the bottom half of the app. The labels on the bottom will not, however, reflect the changes until the button "Update Info" has been pressed. This is when the events are dispatched for the bound properties and the bottom labels are then updated. As you can see the application is fairly simple.
Starting with the user interface we add a panel which holds all the other components. The other components include the text inputs and all the labels. Two of these labels will be bound to variables which we will we create later. The beginning interface code looks like the following:
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
width="305" height="231">
<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" />
<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" />
<mx:Label x="72" y="158" id="lblAge" />
</mx:Panel>
</mx:Application>
The rest of the code is used for the data binding. We start with the actionscript that declares the two variables for the name and age. Using the [Bindable] meta tag we declare each one bindable but we use a special syntax to say what the event name is. This is done using [Bindable(event="eventName")]. Once this syntax is used though you have to dispatch the property bind events yourself. The start of the script code goes right beneath the opening application tag.
<![CDATA[
[Bindable(event="nameChanged")]
private var personName:String;
[Bindable(event="ageChanged")]
private var personAge:String;
]]>
</mx:Script>
Now we are going to bind our two new variables to the text input components and to the labels in the bottom half. The code below uses the <mx:Binding> tag to bind the text inputs to the variables - this goes right after the <mx:Script> tag. The second piece of of code is for the new labels at the bottom which are bound to the variables.
<mx:Binding source="txtAge.text" destination="personAge"/>
Now the labels.
<mx:Label x="72" y="158" id="lblAge" text="{personAge}" />
The only thing left to do is hook up the button to trigger the event. We create a simple function in the actionscript to dispatch the event.
This first piece is the updated button.
Second, this function goes inside our script tag.
{
dispatchEvent(new Event("nameChanged"));
dispatchEvent(new Event("ageChanged"));
}
That pretty much takes care of it. We are left with this final code for the example.
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
width="305" height="231">
<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>
Now, you might ask, why would you want to use this technique - well there are several reasons. The first one that comes to mind is to wait for the data to be validated before dispatching the updated data. Another reason is you might want something else to happen successfully before sending out any updates, in a work flow type situation. I hope you all find this short little tutorial useful. If there are any questions just drop a comment.
08/23/2008 - 12:27
good
09/15/2008 - 03:51
Good Sample!!!!
10/04/2008 - 00:14
Suppose the panel also has a Text component which shows a summary of the rest of the information on the panel. Therefore, should any of the fields change, we want to recalculate this summary.
What is the best way to trigger this recalculation? Do I have to add listeners to each of the components (or to each variable "bound" to each component)? Or is there a better way I can say "If anything changes then call this method"????
01/01/2009 - 19:34
Many thanks. The adobe docs are so disappointing. They have a very powerful product, yet attempts to use the more advanced features end all too often in failure for lack of decent why what how documentation… Besides they are poorly written and edited for errors, which compound confusion.
01/07/2009 - 05:32
Good one.. thnk u.
08/20/2009 - 13:46
Hi: This sample cleared my questions on [Bindable].
Thanks a bunch
10/22/2009 - 11:29
Hey Really good stuff , Flex is damn good front end development kit , it have changed UI style comp 360deg
11/12/2009 - 18:58
Good! Thank you
03/10/2010 - 08:18
cool man!! its a good example, i really mean it
04/13/2010 - 11:25
In mate they use this technology(skill) what I do not still have clear is that because always in the managers methods go only get and not set????? Because??
07/02/2010 - 06:31
Thanks a lot for the series :)
05/25/2011 - 07:52
why do u have to broadcast the event?
10/07/2011 - 20:29
Really Great tut .. it would have been a great help if there was one that details data binding between a parent and child
window(or component).. Anyway nice for a beginner like me
Add Comment
[language] [/language]
Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]
See here for supported languages.
Javascript must be enabled to submit anonymous comments - or you can login.