<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>.:. blog.screenshot.at .:. Flash, Flex, RIA .:. &#187; Binding tag</title>
	<atom:link href="http://www.screenshot.at/blog/tag/binding-tag/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.screenshot.at/blog</link>
	<description>This blog is about Flash, Flex, RIA related stuff</description>
	<lastBuildDate>Mon, 11 May 2015 18:06:51 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.2.38</generator>
	<item>
		<title>DataBinding under the hood (part 2): Features</title>
		<link>http://www.screenshot.at/blog/2009/04/20/databinding-under-the-hood-part2-features/</link>
		<comments>http://www.screenshot.at/blog/2009/04/20/databinding-under-the-hood-part2-features/#comments</comments>
		<pubDate>Mon, 20 Apr 2009 21:31:13 +0000</pubDate>
		<dc:creator><![CDATA[Manfred Karrer]]></dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[[Bindable]]]></category>
		<category><![CDATA[Binding tag]]></category>
		<category><![CDATA[BindingUtils]]></category>
		<category><![CDATA[ChangeWatcher]]></category>
		<category><![CDATA[Curly Brackets]]></category>
		<category><![CDATA[DataBinding]]></category>
		<category><![CDATA[E4x]]></category>
		<category><![CDATA[Expressions]]></category>
		<category><![CDATA[features]]></category>
		<category><![CDATA[Metadata Tag]]></category>
		<category><![CDATA[Mxml]]></category>
		<category><![CDATA[Ternary Operator]]></category>

		<guid isPermaLink="false">http://www.screenshot.at/blog/?p=175</guid>
		<description><![CDATA[There are many different ways how to use DataBinding. Beyond the obvious ones there are a few more which could be a nice feature for some weird hacks&#8230; Flex gives us 4 different techniques to use DataBinding: Curly Brackets in MXML Binding tag BindingUtils ChangeWatcher Lets have an overview of the possibilities of each technique: [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>There are many different ways how to use <span class="hl">DataBinding</span>.<br />
Beyond the obvious ones there are a few more which could be a nice feature for some weird hacks&#8230;</p>
<p>Flex gives us 4 different techniques to use DataBinding:</p>
<ul>
<li>Curly Brackets in MXML</li>
<li>Binding tag</li>
<li>BindingUtils</li>
<li>ChangeWatcher</li>
</ul>
<p>Lets have an overview of the possibilities of each technique:</p>
<p><span class="hl">Curly Brackets in MXML</span></p>
<p>The most obvious one: <strong>Binding a source value to a destination value:</strong></p>
<p>This works also for <strong>readOnly</strong> properties and <strong>static constants</strong>. You can omit the Bindable metadata tag because the Binding is only triggered at application startup. Static variables are not supported (the Adobe docs are not correct at this). Also Binding to style properties are not supported.</p>
<p>To add <strong>2 way Binding</strong> simply use the curly brackets syntax in both objects crosswise:</p>
<pre>
[code lang="actionscript3"]
<mx:TextInput id="input3" text="{input4.text}"/>
<mx:TextInput id="input4" text="{input3.text}"/>
[/code]
</pre>
<p>Here some code examples for <strong>Actionscript and E4X expressions:</strong></p>
<pre>
[code lang="actionscript3"]
// Method calls and calculations
<mx:Text text="{ Math.round(Number(myTI.text)) * 6 / 7 }" />

// Perform string concatenation
<mx:Text text="Cool stuff for lazy {myTI.text}" />

// Perform a conditional operation using a ternary operator
<mx:Text text="{(isMale.selected) ? 'Mr.' : 'Ms.'} {myTI.text}" />

// E4X expressions
<mx:List dataProvider="{data.item.(@id=='36' || @id=='59').desc}"/>
[/code]
</pre>
<p>A nice feature which is probably not typically used in your daily work, is the possibility to bind to functions. You can use a <strong>function as source for your Binding</strong>:<br />
<span id="more-175"></span>To make this work you have to mark the function with the Bindable metadata tag and define your custom event name. When you dispatch an Event with your selected event name the Binding is triggered and the destination gets the return value of your function.</p>
<pre>[code lang="actionscript3"]

       <!--[CDATA[
              [Bindable (event="checkBoxSelected")]
              private function isSelected():String
              {
                     return cb.selected.toString();
              }
              private function checkBoxSelected():void
              {
                     dispatchEvent(new Event("checkBoxSelected"));
              }
       ]]&gt;
</mx:Script>
<mx:CheckBox id="cb" click="checkBoxSelected()" />
<mx:TextArea id="myTA11" text="{isSelected()}" />
[/code]</pre>
<p>Another way is to use <strong>bindable properties as arguments to the function</strong>. When the property change the function is triggered.</p>
<pre>
[code lang="actionscript3"]
<mx:CurrencyFormatter id="usdFormatter"
        precision="2" currencySymbol="$" alignSymbol="left" />
<mx:TextInput id="myTI10" text="Enter number here" />
<mx:TextArea text="{usdFormatter.format(myTI10.text)}" />
[/code]</pre>
<p>In an <strong>MXML</strong> file, you can make all public properties that you defined as variables usable as the source for data binding by including the <strong>[Bindable] metadata tag in an Metadata block</strong>. This is similar to the [Bindable] tag over the class definition in Actionscript.</p>
<pre>
[code lang="actionscript3"]
<mx:Metadata>
    [Bindable]
</mx:Metadata>
[/code]</pre>
<p>There is only one scenario where the curly brackets in MXML are not enough: If you want to use <strong>multiple sources for a destination</strong>.<br />
If you need this you can move over to <span class="hl">Binding tags:</span></p>
<pre>
[code lang="actionscript3"]
<mx:Binding source="input2.text" destination="myTA3.text" />
<mx:Binding source="input1.text" destination="myTA3.text" />
<mx:TextInput id="input1" />
<mx:TextInput id="input2" />
<!  you can also combine curly brackets with the Binding tag -->
<!--<mx:TextArea id="myTA3" text="{input1.text}" />-->

[/code]</pre>
<p>As we have seen in the <a href="http://www.screenshot.at/blog/2009/04/18/databinding-under-the-hood-part-1-performance/">previous post</a> Binding in <strong>MXML is not the fastest solution</strong>.<br />
Your better choice for writing high performacne applications is  <span class="hl">BindingUtils:</span><br />
You have 2 static methods: BindingUtils.bindProperty and BindingUtils.bindSetter</p>
<pre>
[code lang="actionscript3"]
<mx:Script>
    <![CDATA[
        private function onPreInitialize(event:FlexEvent):void 
        {
            BindingUtils.bindProperty(myText5, "text", myTI5, "text");
        }
        
        private function onPreInitialize2(event:FlexEvent):void 
        {
             BindingUtils.bindSetter(setMyText6, myTI6, "text");
        }
        
        private function setMyText6(value:String):void 
        {
            myText6.text = value;
        }
    ]]&gt;
</mx:Script>
<mx:TextInput id="myTI5"/>
<mx:Text id="myText5" preinitialize="onPreInitialize(event);"/>
<mx:TextInput id="myTI6"/>
<mx:Text id="myText6" preinitialize="onPreInitialize2(event);"/>
[/code]
</pre>
<p>The name of the arguments in the API are a little bit confusing (at least for me).</p>
<pre>[code lang="actionscript3"]
public static function bindProperty(site:Object, prop:String,
            host:Object, chain:Object,
            commitOnly:Boolean = false):ChangeWatcher
[/code]</pre>
<p>The first 2 parameters are the destination object and property name, and the last 2 the source object and property name.</p>
<ul>
<li>site:Object -&gt; destination object</li>
<li>prop:String -&gt; destination property name</li>
<li>host:Object -&gt; source object</li>
<li>chain:Object -&gt; source property name(s)</li>
</ul>
<p>Binding supports changes on every bindable element in the source <strong>chain</strong>. If one element is changing the Binding is triggered.<br />
You can specify the chain as a single String for the name of your property (if there is only one chain element) or as Array of property name Strings for each bindable chain element.</p>
<pre>[code lang="actionscript3"]
// bind to object: user.setting.volume
BindingUtils.bindProperty(myText0, "text", user, ["setting", "volume"]);
[/code]</pre>
<p>But there is <strong>another possibility to set the chain argument</strong>:<br />
You can pass an Object in the form:</p>
<pre>[code lang="actionscript3"]
{ name: propertyName,
  getter: function(host) { return host[propertyName] }
}
[/code]</pre>
<p>This Object must contain the name of the property and a function returning a value, which is normally a getter function for a public bindable property of the source object.<br />
This feature opens interesting possibilities.<br />
The function is basically a wrapper which returns the value of the source. I am not sure at the moment where to use this, but it´s good to know that there is an open door…</p>
<p>When using chains with BindingUtils you can choose if the chain elements in-between are bindable.<br />
If an element in-between is not bindable, no Binding is triggered if this element changes.<br />
This is not the case when you use the chains in MXML. You got a compiler warning and a runtime exception if one of the in-between elements are not marked as bindable.</p>
<p>Here is a code example for using chains:</p>
<pre>[code lang="actionscript3"]

<!-- Chains in MXML -->

public class User
{
    [Bindable]
    public var setting:Setting;
    public function User(setting:Setting)
    {
        this.setting = setting;
    }
}

public class Setting
{
    [Bindable]
    public var title:String = "blabla";
    public function Setting()
    {
        setInterval(function():void {
                     title = int(Math.random()*10000).toString()
                     }, 100);
    }
}
[/code]</pre>
<p>The <span class="hl">ChangeWatcher</span> is the class used inside of BindingUtils. ChangeWatcher implements the event handling and the handling for the chains. BindingUtils are just a more convenience way and adds nearly no overhead compared to use ChangeWatcher directly.</p>
<p>Here are <span class="hl">some additional snippets</span> about DataBinding:<br />
You can use <strong>multiple [Bindable] tags</strong>. Doing this lets you trigger Binding from different events.</p>
<p>When you use Binding in MXML the addEventListener method uses a <strong>weak reference</strong>.<br />
This is important due the fact that <strong>you cannot unbind with the curly brackets syntax</strong>. Without weak reference Binding in MXML would be a potential memory leak.</p>
<p>When using BindingUtils you are responsible for calling the <strong>unwatch()</strong> method on your ChangeWatcher object (a ChangeWatcher object is the return value of BindingUtils.bindProperty). The addEventListener method in the ChangeWatcher is not using weak references, so if you don´t do your housekeeping carefully, this could lead to a memory leak.</p>
<p>In UIComponent there is the method <strong>executeBindings()</strong> which force a trigger to all Bindings on this component. This could help sometimes when a Binding is not firing by it´self correctly, but of course should be taken carefully because it´s probably just healing a symptom not the underlying problem.</p>
<p>When you use the <strong>[Bindable] metadata tag before a class definition</strong>, it only applies to public properties; it does not apply to private or protected properties, or to properties defined in any other namespace. You must insert the [Bindable] metadata tag before a nonpublic property to make it usable as the source for a data binding expression.</p>
<p>The DataBinding reference can be found at <a href="http://livedocs.adobe.com/flex/3/html/help.html?content=databinding_1.html" target="_blank">Adobe LiveDocs</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.screenshot.at/blog/2009/04/20/databinding-under-the-hood-part2-features/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
