Flex Snippet Tutorial - Bindable Meta Tag Part 1

Skill

Flex Snippet Tutorial - Bindable Meta Tag Part 1

Posted in:

We here on the blog decided to start trying to give daily updates and this is the first smaller daily update. Don't worry we will still keep up with our larger in depth tutorials at least a couple times a week. But we just want to put some more content out and thought this would be a nice way to do it.

So heres a quick rundown of a very cool way to bind data in Flex. The way you can do this is a very simple meta tag called Bindable. First, you probably want to know what exactly a meta tag is. Meta tags are pieces of information you use in your code to tell the compiler certain things. The tag itself does not get compiled into the binary executable. You can find more information about meta tags here in the Adobe documentation.

So the [Bindable] tag lets you tell the compiler that an object or value can be mapped to a property and can be triggered by an event or value change. I don't know how much sense a small description is going to make so a couple examples should help.

In the first example, as seen below, we are just going to bind a variable we create to the text of a label. This will automatically copy the value of our variable to the text in the label. So we start off with a very simple flex interface, I am not going to go over much of the interface code but basically we have put a label and button on a simple panel.

Get Adobe Flash player

Now I am going to go over the interesting parts of the code below. The first interesting thing we see is the <mx:Script> tag which lets us put actionscript in our mxml file. Next we get to our all important [Bindable] declaration and the variable that is being binded to. So basically this piece tells us that our public var ourLabelText is bindable and whatever is bound to it will automatically be updated with the variable changes, but this is only one way. Next you see a small function to change the value our our variable. Now down in the label tag we see that our text attribute has been set to {ourLabelText}. This is the other important part of binding our data, this actually tells the text property of the label to be bound to the ourLabelText variable. And last we set the click event of our button to the actionscript function we created to change the variable to show that the label will be automatically updated.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
   width="236" height="188" viewSourceURL="../files/JSTutorial.mxml">
  <mx:Script>
   <![CDATA[
     [Bindable]
     private var ourLabelText:String = "Initial Value";
     
     private function changeVariable():void
     {
       ourLabelText = "Changed Value";
     }
   ]]>
 </mx:Script>
  <mx:Panel x="10" y="10" width="216" height="168"
     layout="absolute" title="Binding to Text Property">
    <mx:Label x="10" y="45" id="lblOutput" text="{ourLabelText}"/>
    <mx:Button x="10" y="15" label="Change Variable"
       id="butChangeTxt" click="{changeVariable()}"/>
  </mx:Panel>
</mx:Application>

That gives you a quick use of the [Bindable] meta tag. I will show you one more example real quick which will show how to bind data from a component on our screen back to a variable. Now this binding syntax is a little different but accomplishes the exact same thing as above, and could be used instead of the above approach. The example app below shows our data being bound from a text input to a variable and then the variable is bound to our label. Basically making it so when you type in the textbox it shows up in the label.

Get Adobe Flash player

Ok, now a quick rundown of the code below. First we see that nothing is crazy in the interface code except the <mx:Binding> tag that is used. This is what we use to bind the data from our text input to our variable. You will also notice we don't have to use brackets { } inside the source and destination attributes because it already expects a variable/property there. Also you see that the source is our txtInput.text which tells the compiler we want to bind the value in our text field to our destination inoutText. Now we have backwards data binding. Just to make the example complete we bind the variable to our label using the same approach as above. So here is the code.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
   width="220" height="202" viewSourceURL="../files/BackwardsBindingTutorial.mxml">
  <mx:Script>
   <![CDATA[
     [Bindable]
     private var inoutText:String;
   ]]>
 </mx:Script>
  <mx:Binding source="txtInput.text" destination="inoutText" />
  <mx:Panel x="8.5" y="10" width="203" height="182"
     layout="absolute" title="Binding Data Both Ways">
    <mx:TextInput x="13" y="10" id="txtInput" text="Type Here"/>
    <mx:Label x="13" y="58" id="lblOutput" text="{inoutText}"/>
  </mx:Panel>
</mx:Application>

Now you know how to bind data to and from mxml components and actionscript variables. The next part in this data binding series we will go over binding data from a custom class we have made. If your looking for more information on the [Bindable] meta tag check out Part 2. Feel free to ask any and all questions you have about the above code.

Nicolas
08/30/2007 - 21:24

Thanks for this tutorial. It's much clearer than what is in the Flex documentation :)

reply

Mudit Tuli
10/02/2007 - 06:08

Thanks for the tutorial, the binding from component to variable is great.

reply

Gurpreet
06/21/2008 - 03:54

thnx... definitly makes sense., better than how it's explained in documentation.

reply

Wo Yao
07/10/2008 - 12:14

Thanks, this was very helpful (moreso than the documentation as others have described.)!

reply

A.purnima
07/22/2008 - 02:59

tell me briefly abt QD gamings

reply

Motosauro
10/20/2008 - 02:20

Thanks for this insight: official documentation is misleading on this topic IMO.
Yours is clearer :)

reply

Dave
05/23/2009 - 05:02

I have seen the guy doing bindables without [Bindable] tag. He just used {id.property} expression as value of an attribute in the element binded to the id.property value. And it worked.

Now the question - is it necessary to always use and define [Bindable] somewhere in the project?

reply

Phil
08/26/2009 - 16:13

Great Tutorial!

reply

Anonymous
10/06/2009 - 08:27

"The tag itself does not get compiled into into the binary executable."

2nd paragraph

reply

The Fattest
10/06/2009 - 09:30

Thanks, fixed.

reply

Anonymous
10/09/2009 - 05:48

This is a good and very easy example.Thankyou for explaining about [Bindable] tag in such an easy way.

reply

bobsmith
10/15/2009 - 12:44

Hi,

I've been working on a widget that dynamically builds a flex layout given some xml (at runtime--e.g. building from a "layout" message from a server--much like html in a web browser). This means that all the component building has to happen in Actionscript.

It seems to me that all databinding examples for the DataGrid's dataProvider have the Bindable variable declaration in Actionscript and the binding happening in { } tags in MXML. Is there a way to do the binding purely in Actionscript?

Thanks.

reply

Anonymous
03/04/2010 - 02:20

Nice documentation

reply

Joe
06/04/2010 - 05:47

Thanks!

reply

muhshin
11/10/2010 - 23:30

this document is good for starters

reply

Add Comment

Put code snippets inside language tags:
[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.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.