Switch On The Code RSS Button - Click to Subscribe
Aug
30

Flex Snippet Tutorial - Bindable Meta Tag Part 3

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:

<?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: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.

<mx:Script>
<![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="txtName.text"
  destination="personName"/>

<mx:Binding source="txtAge.text"
  destination="personAge"/>

Now the labels.

<mx:Label x="72" y="132" id="lblName"
  text="{personName}"/>

<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.

<mx:Button x="10" y="64" label="Update Info"
  click="updateInfo()"/>

Second, this function goes inside our script tag.

private function updateInfo():void
{
  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.

<?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>

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.



Posted in Snippet Tutorials, Flex, All Tutorials by The Fattest |

3 Responses

  1. good Says:

    good

  2. Parthipan Says:

    Good Sample!!!!

  3. Mark Says:

    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”????

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

Powered by WP Hashcash