<?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>DaveOnCode &#187; flex</title>
	<atom:link href="http://www.daveoncode.com/tag/flex/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.daveoncode.com</link>
	<description>Objective C, iOS and more programming stuff</description>
	<lastBuildDate>Mon, 19 Dec 2011 12:11:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Handling doubleclick on Flex components</title>
		<link>http://www.daveoncode.com/2010/04/07/handling-doubleclick-on-flex-components/</link>
		<comments>http://www.daveoncode.com/2010/04/07/handling-doubleclick-on-flex-components/#comments</comments>
		<pubDate>Wed, 07 Apr 2010 10:34:53 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[flex]]></category>
		<category><![CDATA[actionscript]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=598</guid>
		<description><![CDATA[Handling a common event like a double click is not so simple as we may assume when it comes to Flex programming. In fact there are several critical aspects related to this event, first of all we have to set the attribute doubleClickEnabled to true in order to make our components double clickable, and this [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2010/04/07/handling-doubleclick-on-flex-components/' addthis:title='Handling doubleclick on Flex components ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>Handling a common event like a double click is not so simple as we may assume when it comes to Flex programming.<br />
In fact there are several critical aspects related to this event, first of all we have to set the attribute <strong>doubleClickEnabled</strong> to <strong>true</strong> in order to make our components double clickable, and this sounds pretty silly to me. Why should I enable an object to receive an event which is so common and &#8220;natural&#8221;? This is incoherent from a software design perspective end totally unexpected by the user. Anyway this is not a real problem, because once we know about it we have just to use a setter, but double click is somehow buggy in the framework and its broadcasting can be blocked mysteriously when using certain components. I faced this issue by trying to handle a double click on a DataGrid which makes use of custom item renderers and I tested that by double clicking on the &#8220;special cells&#8221; (those rendered by my own renderers) the <strong>MouseEvent.DOUBLE_CLICK</strong> is not dispatched/cacthed. To solve the problem I had to draw an invisible rectangle using the underlying Actionscript graphics API! This is my workaround:</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0066CC;">this</span>.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0xffffff, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #0066CC;">this</span>.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #0066CC;">this</span>.<span style="color: #006600;">parent</span>.<span style="color: #0066CC;">width</span>, <span style="color: #0066CC;">this</span>.<span style="color: #006600;">parent</span>.<span style="color: #0066CC;">height</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #0066CC;">this</span>.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></td></tr></tbody></table></div>
<p>Finally, it seems that double click is <strong>completely ignored</strong> by Firefox 3.6 on Mac OS X, but fortunately this seems to been fixed in the last release (3.6.3)</p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2010/04/07/handling-doubleclick-on-flex-components/' addthis:title='Handling doubleclick on Flex components ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2010/04/07/handling-doubleclick-on-flex-components/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Get hours, minutes and seconds from a number without maths operations</title>
		<link>http://www.daveoncode.com/2010/02/05/get-hours-minutes-and-seconds-from-number-without-maths-operations/</link>
		<comments>http://www.daveoncode.com/2010/02/05/get-hours-minutes-and-seconds-from-number-without-maths-operations/#comments</comments>
		<pubDate>Fri, 05 Feb 2010 15:09:11 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[flex]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=589</guid>
		<description><![CDATA[Today I&#8217;m gonna show you a secret ninja technique to extract hours, minutes and seconds from a number (representing an amount of seconds). In my example I will show an Actionscript code, but this can be implemented in JavaScript and maybe other languages too. So, the scenario is the following: we have a number representing [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2010/02/05/get-hours-minutes-and-seconds-from-number-without-maths-operations/' addthis:title='Get hours, minutes and seconds from a number without maths operations ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>Today I&#8217;m gonna show you a secret ninja technique to extract hours, minutes and seconds from a number (representing an amount of seconds). In my example I will show an Actionscript code, but this can be implemented in JavaScript and maybe other languages too.<br />
So, the scenario is the following: we have a number representing seconds and we want to know how many hours this amount of seconds contains, how many minutes and how many remaining seconds. We know that a minute is composed by 60 seconds and an hour by 60 minutes and we could write a series of maths operations in order to accomplish our objective, but there is a way far simple and fast: use the Date class!<br />
Date class already implements all the methods we need:</p>
<ul>
<li><strong>getHours()</strong></li>
<li><strong>getMinutes()</strong></li>
<li><strong>getSeconds()</strong></li>
</ul>
<p>So, in order to take advantage of these useful methods, all we have to do is initialize a &#8220;fake&#8221; date using the amount of seconds we want to &#8220;split&#8221; into hours, minutes and seconds. The Date class has several OPTIONAL arguments that can be specified during its initializations, these are:</p>
<ul>
<li><strong>year</strong></li>
<li><strong>month</strong></li>
<li><strong>date</strong> (day number)</li>
<li><strong>hours</strong></li>
<li><strong>minutes</strong></li>
<li><strong>seconds</strong></li>
<li><strong>milliseconds</strong></li>
</ul>
<p>Because theme all are optional, we can create a date object by specifying only the know arguments (in our case seconds) and by assigning null or zero to the others:</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">date</span>:<span style="color: #0066CC;">Date</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Date</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">9137</span><span style="color: #66cc66;">&#41;</span>;</div></td></tr></tbody></table></div>
<p>Then by calling getHours, getMinutes and getSeconds we will obtain what we expect:</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;hours: &quot;</span>, <span style="color: #0066CC;">date</span>.<span style="color: #0066CC;">getHours</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 2 hours</span><br />
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;minutes: &quot;</span>, <span style="color: #0066CC;">date</span>.<span style="color: #0066CC;">getMinutes</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 32 minutes</span><br />
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;seconds: &quot;</span>, <span style="color: #0066CC;">date</span>.<span style="color: #0066CC;">getSeconds</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 17 seconds</span></div></td></tr></tbody></table></div>
<p>Not sure about the result? Let&#8217;s test it:</p>
<p>60 * 60 * 2 = <strong>7200</strong>; (seconds contained in 2 hours)<br />
32 * 60 = <strong>1920</strong>; (seconds contained in 32 minutes)<br />
7200 + 1920 + 17 = <strong>9137</strong>; (original seconds!)</p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2010/02/05/get-hours-minutes-and-seconds-from-number-without-maths-operations/' addthis:title='Get hours, minutes and seconds from a number without maths operations ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2010/02/05/get-hours-minutes-and-seconds-from-number-without-maths-operations/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Resolve Flex&#8217;s error: &#8220;Type was not found or was not a compile-time constant&#8221;</title>
		<link>http://www.daveoncode.com/2009/08/02/flex-type-was-not-found-or-was-not-a-compile-time-constant/</link>
		<comments>http://www.daveoncode.com/2009/08/02/flex-type-was-not-found-or-was-not-a-compile-time-constant/#comments</comments>
		<pubDate>Sun, 02 Aug 2009 10:23:09 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[flex]]></category>
		<category><![CDATA[compile]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[flex builder]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=446</guid>
		<description><![CDATA[I just faced the terrible nightmare of &#8220;Type was not found or was not a compile-time constant&#8221; error (Flex Builder) and I lost several time to figure out what the problem was. I realized that I was using the same name for MXML Application file which was already used by a class inside one of [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/08/02/flex-type-was-not-found-or-was-not-a-compile-time-constant/' addthis:title='Resolve Flex&#8217;s error: &#8220;Type was not found or was not a compile-time constant&#8221; ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>I just faced the terrible nightmare of &#8220;Type was not found or was not a compile-time constant&#8221; error (Flex Builder) and I lost several time to figure out what the problem was.<br />
I realized that I was using the same name for MXML Application file which was already used by a class inside one of my packages. So, by renaming the file I solved the problem, but I was not completely satisfied and I looked for a way to avoid the error mantaining the same file/class name. Initially I tried to use namespaces, but as the reference says: <em> &#8220;Applying a namespace means placing a definition into a namespace. Definitions that can be placed into namespaces include functions, variables, and constants (<strong>you cannot place a class into a custom namespace</strong>)&#8221;</em>. I finally solved by renaming class references inside my package with the full qualified name (from MyClass to com.mysite.foo.MyClass)</p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/08/02/flex-type-was-not-found-or-was-not-a-compile-time-constant/' addthis:title='Resolve Flex&#8217;s error: &#8220;Type was not found or was not a compile-time constant&#8221; ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/08/02/flex-type-was-not-found-or-was-not-a-compile-time-constant/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Flex 4 (Gumbo) beta released!</title>
		<link>http://www.daveoncode.com/2009/06/03/flex-4-gumbo-sdk-beta-released/</link>
		<comments>http://www.daveoncode.com/2009/06/03/flex-4-gumbo-sdk-beta-released/#comments</comments>
		<pubDate>Wed, 03 Jun 2009 15:30:49 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[flex]]></category>
		<category><![CDATA[flex 4]]></category>
		<category><![CDATA[flex gumbo]]></category>
		<category><![CDATA[sdk]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=431</guid>
		<description><![CDATA[Flex 4 beta (codename: Gumbo) has been released, I just finished to read about the new major release. These are the main news: Improved compiler performance New skinning and component architecture (named Spark) Added suppurt to FXG (learn more about FXG) New states model ASDoc tool has been improved and is now possible to use [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/06/03/flex-4-gumbo-sdk-beta-released/' addthis:title='Flex 4 (Gumbo) beta released! ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>Flex 4 beta (codename: Gumbo) has been released, I just finished to read about the new major release. These are the main news:</p>
<ol>
<li>Improved compiler performance</li>
<li>New skinning and component architecture (named Spark)</li>
<li>Added suppurt to FXG (<a href="http://livedocs.adobe.com/flex/gumbo/html/WS145DAB0B-A958-423f-8A01-12B679BA0CC7.html">learn more about FXG</a>)</li>
<li>New states model</li>
<li>ASDoc tool has been improved and is now possible to use ASDoc tags in mxml documents</li>
<li>CSS has been improved to support descendant and id selectors (finally the previous missing &#8220;cascading part&#8221; of CSS has been provided!)</li>
<li>Backwards compatibility with Flex 3</li>
</ol>
<p>The new sdk is available on <a href="http://labs.adobe.com/technologies/flex4sdk/">Adobe Labs</a></p>
<p>More about Gumbo here:</p>
<p><a href="http://opensource.adobe.com/wiki/display/flexsdk/Gumbo+Component+Architecture">An Introduction to the Gumbo Component Architecture</a> (by Deepa Subramaniam)<br />
<a href="http://www.adobe.com/devnet/flex/articles/flex4sdk_whatsnew.html">What&#8217;s new in Flex 4 SDK beta</a> (by Matt Chotin)<br />
<a href="http://www.adobe.com/devnet/flex/articles/flex3and4_differences.html">Differences between Flex 3 and Flex 4</a> (by Joan Lafferty)</p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/06/03/flex-4-gumbo-sdk-beta-released/' addthis:title='Flex 4 (Gumbo) beta released! ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/06/03/flex-4-gumbo-sdk-beta-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ObjectCollector: Accessing Flex’s objects by id (even dynamic generated) from anywhere</title>
		<link>http://www.daveoncode.com/2009/05/20/objectcollector-accessing-dynamic-generated-flex-objects-by-id/</link>
		<comments>http://www.daveoncode.com/2009/05/20/objectcollector-accessing-dynamic-generated-flex-objects-by-id/#comments</comments>
		<pubDate>Wed, 20 May 2009 14:34:40 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[flex]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=413</guid>
		<description><![CDATA[Ok, let me explain my last experiment&#8230; Problem Flex&#8217;s components have a nice id property which can be used to easily referencing object created. All you know, that if I get the following mxml Application file: 123456&#60;?xml version=&#34;1.0&#34; encoding=&#34;utf-8&#34;?&#62; &#60;mx:Application xmlns:mx=&#34;http://www.adobe.com/2006/mxml&#34; layout=&#34;vertical&#34;&#62; &#160; &#160; &#60;mx:TextInput id=&#34;input1&#34; width=&#34;300&#34; /&#62; &#60;/mx:Application&#62; I can then create a Script [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/05/20/objectcollector-accessing-dynamic-generated-flex-objects-by-id/' addthis:title='ObjectCollector: Accessing Flex’s objects by id (even dynamic generated) from anywhere ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>Ok, let me explain my last experiment&#8230;</p>
<h2>Problem</h2>
<p></p>
<p>Flex&#8217;s components have a nice <strong>id</strong> property which can be used to easily referencing object created.<br />
All you know, that if I get the following mxml Application file:</p>
<div class="codecolorer-container xml mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:Application</span> <span style="color: #000066;">xmlns:mx</span>=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span> <span style="color: #000066;">layout</span>=<span style="color: #ff0000;">&quot;vertical&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:TextInput</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;input1&quot;</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;300&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
<br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mx:Application<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></td></tr></tbody></table></div>
<p>I can then create a <strong>Script</strong> block into which I&#8217;ll point to TextInput in this way:</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">input1.<span style="color: #0066CC;">text</span> = <span style="color: #ff0000;">&quot;text dynamically added :)&quot;</span>;</div></td></tr></tbody></table></div>
<p>Perfect, but how can I reference dynamically created objects (components outside Application)?</p>
<p><span id="more-413"></span></p>
<p>The simplest example I can do is the following:</p>
<div class="codecolorer-container xml mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br /></div></td><td><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:Application</span> <span style="color: #000066;">xmlns:mx</span>=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span> <span style="color: #000066;">layout</span>=<span style="color: #ff0000;">&quot;vertical&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:Script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;![CDATA[</span><br />
<br />
<span style="color: #339933;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;private function test():void {</span><br />
<br />
<span style="color: #339933;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var input:TextInput = new TextInput();</span><br />
<span style="color: #339933;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;input.id = &quot;input2&quot;;</span><br />
<span style="color: #339933;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><br />
<span style="color: #339933;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.addChild(input);</span><br />
<br />
<span style="color: #339933;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Here I get an error which says input2 is undefined!</span><br />
<span style="color: #339933;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;input2.text = &quot;foo&quot;;</span><br />
<br />
<span style="color: #339933;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</span><br />
<br />
<span style="color: #339933;"> &nbsp; &nbsp; &nbsp; &nbsp;]]&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mx:Script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mx:Application<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></td></tr></tbody></table></div>
<h2>Solution</h2>
<p></p>
<p>Define a place where to store references to desired objects, which will be accessible through id from anywhere.<br />
So, I created a singleton class called <strong>ObjectCollector</strong> which provides a simple, elegant and secure solution to the problem. You can use it in this way:</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">// From anywhere you first create and register objects:</span><br />
<br />
<span style="color: #000000; font-weight: bold;">var</span> collector:ObjectCollector = ObjectCollector.<span style="color: #006600;">getInstance</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #000000; font-weight: bold;">var</span> myObj1:<span style="color: #0066CC;">Button</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
myObj1.<span style="color: #0066CC;">width</span> = <span style="color: #cc66cc;">300</span>;<br />
myObj1.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;my first button&quot;</span>;<br />
<span style="color: #808080; font-style: italic;">// id is mandatory, otherwise you'll get an error</span><br />
myObj1.<span style="color: #006600;">id</span> = <span style="color: #ff0000;">&quot;myFirstButton&quot;</span>;<br />
<br />
<span style="color: #000000; font-weight: bold;">var</span> myObj2:TextInput = <span style="color: #000000; font-weight: bold;">new</span> TextInput<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
myObj2.<span style="color: #0066CC;">text</span> = <span style="color: #ff0000;">&quot;hello world&quot;</span>;<br />
<span style="color: #808080; font-style: italic;">// id is mandatory, otherwise you'll get an error</span><br />
myObj2.<span style="color: #006600;">id</span> = <span style="color: #ff0000;">&quot;myTextField&quot;</span>;<br />
<br />
collector.<span style="color: #006600;">registerObject</span><span style="color: #66cc66;">&#40;</span>myObj1<span style="color: #66cc66;">&#41;</span>;<br />
collector.<span style="color: #006600;">registerObject</span><span style="color: #66cc66;">&#40;</span>myObj2<span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #808080; font-style: italic;">/* ...then in another (or the same) place you can<br />
get references to your desired objects<br />
(this time by specifying the id)<br />
When you retrieve objects from the collector you should <br />
always cast (upcast!) them to the right type<br />
(Because objects are collected as generic Object classes)<br />
*/</span><br />
<br />
<span style="color: #000000; font-weight: bold;">var</span> btn:<span style="color: #0066CC;">Button</span> = <span style="color: #0066CC;">Button</span><span style="color: #66cc66;">&#40;</span>ObjectCollector.<span style="color: #006600;">getInstance</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getObject</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;myFirstButton&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #808080; font-style: italic;">// When/if you don't need these reference anymore</span><br />
<span style="color: #808080; font-style: italic;">// you can remove them by id:</span><br />
<br />
ObjectCollector.<span style="color: #006600;">getInstance</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">unregisterObject</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;myFirstButton&quot;</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #808080; font-style: italic;">// or all together:</span><br />
<br />
ObjectCollector.<span style="color: #006600;">getInstance</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">unregisterAll</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></td></tr></tbody></table></div>
<h2>Download</h2>
<p>You can download the class <a href="http://www.daveoncode.com/_dave_stuff/ObjectCollector.zip">here</a></p>
<h2>Documentation</h2>
<p>A brief documentation is available <a href="http://www.daveoncode.com/_docs/utils/com/daveoncode/util/ObjectCollector.html">here</a></p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/05/20/objectcollector-accessing-dynamic-generated-flex-objects-by-id/' addthis:title='ObjectCollector: Accessing Flex’s objects by id (even dynamic generated) from anywhere ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/05/20/objectcollector-accessing-dynamic-generated-flex-objects-by-id/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

