<?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; BindingUtils</title>
	<atom:link href="http://www.screenshot.at/blog/tag/bindingutils/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>
		<item>
		<title>DataBinding under the hood (part 1): Performance</title>
		<link>http://www.screenshot.at/blog/2009/04/18/databinding-under-the-hood-part-1-performance/</link>
		<comments>http://www.screenshot.at/blog/2009/04/18/databinding-under-the-hood-part-1-performance/#comments</comments>
		<pubDate>Sat, 18 Apr 2009 21:21:05 +0000</pubDate>
		<dc:creator><![CDATA[Manfred Karrer]]></dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Adobe LiveDocs]]></category>
		<category><![CDATA[Application Startup]]></category>
		<category><![CDATA[auto-generated code]]></category>
		<category><![CDATA[Benefit]]></category>
		<category><![CDATA[Binding in MXM]]></category>
		<category><![CDATA[BindingUtils]]></category>
		<category><![CDATA[Bottleneck]]></category>
		<category><![CDATA[Closer Look]]></category>
		<category><![CDATA[Curly Brackets]]></category>
		<category><![CDATA[DataBinding]]></category>
		<category><![CDATA[Destination Object]]></category>
		<category><![CDATA[Drawback]]></category>
		<category><![CDATA[Event dispatching]]></category>
		<category><![CDATA[Improvements]]></category>
		<category><![CDATA[Iterations]]></category>
		<category><![CDATA[Medium Applications]]></category>
		<category><![CDATA[Mxml]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Performance Considerations]]></category>
		<category><![CDATA[Performance Features]]></category>
		<category><![CDATA[Performance Penalty]]></category>
		<category><![CDATA[propertyChange event]]></category>
		<category><![CDATA[Random Value]]></category>
		<category><![CDATA[Rapid Development]]></category>
		<category><![CDATA[Runtime Errors]]></category>
		<category><![CDATA[Tag]]></category>

		<guid isPermaLink="false">http://www.screenshot.at/blog/?p=26</guid>
		<description><![CDATA[DataBinding is one of the favorite features of Flex. It makes development fast and is really nice to use. In the following posts we will have a closer look to some different aspects of DataBinding. Performance Features (beyond the obvious ones) Generated Code under the hood So lets start first with a closer look at [&#8230;]]]></description>
				<content:encoded><![CDATA[<p><span class="hl">DataBinding</span> is one of the favorite features of Flex. It makes development fast and is really nice to use.  In the following posts we will have a closer look to some different aspects of DataBinding. <strong> </strong></p>
<ul>
<li>Performance</li>
<li> Features (beyond the obvious ones)</li>
<li>Generated Code under the hood</li>
</ul>
<p>So lets start first with a closer look at <span class="hl">Performance:</span><br />
It is clear that such a nice feature has it´s overhead.<br />
For small and medium applications this probably will not be even noticeable. The benefit´s for rapid development will knock out the performance considerations. But when you work on larger applications you may come to a point where you have to take care of every possible bottleneck. And the sum of a lot of small improvements will give you a considerable performance boost at the end.</p>
<p>Lets have a look to different approaches to set a value from a source to a destination object:</p>
<ul>
<li>Binding in MXML with the curly brackets (or the Bindable tag)</li>
<li>Using BindingUtils (or ChangeWatcher) in Actionscript</li>
<li>Custom Event dispatching</li>
<li>Setting the property directly</li>
</ul>
<p>I created some <a href="http://www.screenshot.at/blogDownloads/TestDataBinding.html" target="_blank">tests</a> passing a random value from a source to a destination with 10 000 iterations and repeated this tests 10 times to get a good average value.<br />
Here are the results:</p>
<ul>
<li> Binding in MXML: 412 ms</li>
<li> BindingUtils: 188 ms</li>
<li> Event dispatching: 146 ms</li>
<li> Write property directly: 81 ms</li>
</ul>
<p><span id="more-26"></span>So there is a considerable performance penalty when using <strong>Binding in MXML</strong>. For small projects this probably will not be a problem, in large applications where a lot of Bindings are triggered, this could be a noticeable drawback. It could slow down the application startup, when all Bindings are initiated.<br />
A good thing on MXML Binding is that it gives you the highest level of pre defined errorhandling. Because most of the generated code can be checked at compile time, it gives you warnings if you going to mess up something. With BindingUtils the possibility of runtime errors is already higher.<br />
Another plus for MXML is that some features are only supported with MXML Bindings (Actionscript expressions, E4X Expressions,&#8230;).</p>
<p>The difference between <strong>BindingUtils </strong>and <strong>EventDispatching </strong>is not so huge, but in some cases you can gain a bit if you handle it by your own event dispatching.</p>
<p>The fastest way of course is <strong>setting a property directly</strong> and could be your good friend in really performance critical areas. But the support for a loosely coupled architecture is not as good as with the other solutions.</p>
<p>There are some further fine-grained improvements possible when using your own event name instead of the default “<strong>propertyChange</strong>” event. In the Bindable metadata tag you can specify a <strong>custom event name</strong>.</p>
<p>[code lang=&#8221;actionscript&#8221;][Bindable (event=&#8221;myCustomEventName&#8221;)][/code]</p>
<p>Doing this, the Flex Compiler <strong>will not auto-generate code for you</strong>, like it is the case when you use [Bindable] without setting an event name. So you have to write a bit more code and dispatch an Event with the given event name. But doing this you can gain some performance improvements.</p>
<p>Here are the test results:</p>
<ul>
<li> BindingUtils with custom event names ([Bindable (event=&#8221;myCustomEvent&#8221;)]): 150 ms</li>
<li> BindingUtils with the default (&#8220;propertyChange&#8221;) event name ([Bindable]): 188 ms</li>
</ul>
<p>Doesn´t sounds too exciting, but that´s not all:<br />
When you have a large class with a lot of bindable properties the performance gain will grow with the number of properties, because all Binding objects have to be checked if one property in this class will change.</p>
<p>Separating properties with custom event names, will speed up performance again.</p>
<p>The test scenario:<br />
Change 10 different properties in one class with 10 custom events versus changing 10 properties in a class with the default event name (do 10 000 iterations).</p>
<p>Here are the test results:</p>
<ul>
<li>Test Binding with different Event names: 1511 ms</li>
<li>Test Binding with the default &#8220;propertyChange&#8221; event name: 2657 ms</li>
</ul>
<p>So now the difference is quite huge.<br />
Of course to have too much properties in one class is often a result of bad design and should not be the typical use case. But it´s good to know where to tweak if performance issues arise.</p>
<p>Here is the <strong>code you have to write by yourself</strong> if you use <strong>custom events</strong>.<br />
It is basically the same the compiler auto-generates for you when you use [Bindable] without a custom event name.</p>
<pre>[code lang="actionscript3"]
private var _myLabelText:String;
[Bindable(event="myLabelTextChanged")]
public function get myLabelText():String
{
    return _myLabelText;
}
public function set myLabelText(value:String):void
{
    if (_myLabelText !== value)
    {
        _myLabelText = value;
        dispatchEvent(new Event("myLabelTextChanged "));
    }
}
[/code]</pre>
<p>References:<br />
Watch out the great presentation about DataBinding by Michael Labriola: <strong>Diving in the Data Binding Waters</strong><br />
Links: <a href="http://www.digitalprimates.net/author/codeslinger/2008/08/20/360-flex-san-jose-recap/" target="_blank">Slides</a></p>
<p>It´s also worth to have a closer look to the DataBinding reference at <a href="http://livedocs.adobe.com/flex/3/html/help.html?content=databinding_1.html" target="_blank">Adobe LiveDocs</a>.</p>
<p>The test project is linked <a href="http://www.screenshot.at/blogDownloads/TestDataBinding.html" target="_blank">here</a>, in the View Source context menu the sources are attached.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.screenshot.at/blog/2009/04/18/databinding-under-the-hood-part-1-performance/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
