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

<channel>
	<title>Dan Schultz, Intrigue Media</title>
	<atom:link href="http://www.intriguemedia.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.intriguemedia.net</link>
	<description>All things Flex, AIR, Application Architecture and Cayri from Dan Schultz</description>
	<pubDate>Wed, 03 Feb 2010 23:36:12 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Making ActionScript Look Beautiful: Increasing Readability and Loosing Verboseness</title>
		<link>http://www.intriguemedia.net/2010/02/03/making-actionscript-look-beautiful-increasing-readability-and-loosing-verboseness/</link>
		<comments>http://www.intriguemedia.net/2010/02/03/making-actionscript-look-beautiful-increasing-readability-and-loosing-verboseness/#comments</comments>
		<pubDate>Wed, 03 Feb 2010 23:10:32 +0000</pubDate>
		<dc:creator>Dan Schultz</dc:creator>
		
		<category><![CDATA[ActionScript]]></category>

		<category><![CDATA[Design Patterns]]></category>

		<category><![CDATA[Flex]]></category>

		<guid isPermaLink="false">http://www.intriguemedia.net/?p=114</guid>
		<description><![CDATA[After playing around with languages such as Ruby and Groovy, I&#8217;ve been yearning to loose a lot of the verboseness of ActionScript.  I feel that many languages tend to be overly expressive, and hide the concepts of what your code is actually doing.
Lets take a simple example of a snippet of code that represents, [...]]]></description>
			<content:encoded><![CDATA[<p>After playing around with languages such as Ruby and <a href="http://groovy.codehaus.org/" target="_blank">Groovy</a>, I&#8217;ve been yearning to loose a lot of the verboseness of ActionScript.  I feel that many languages tend to be overly expressive, and hide the concepts of what your code is actually doing.</p>
<p>Lets take a simple example of a snippet of code that represents, <em>Is tomorrow after today?</em></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> isTomorrowAfterToday<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Boolean</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">var</span> now:<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: #66cc66;">&#41;</span>;
    <span style="color: #000000; font-weight: bold;">var</span> today:<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>now.<span style="color: #0066CC;">setHours</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #000000; font-weight: bold;">var</span> tomorrow:<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>now.<span style="color: #0066CC;">setDate</span><span style="color: #66cc66;">&#40;</span>now.<span style="color: #0066CC;">date</span> + <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #b1b100;">return</span> tomorrow.<span style="color: #0066CC;">getTime</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&gt;</span> today.<span style="color: #0066CC;">getTime</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>To me this is bloated, confusing and unreadable.  Creation logic is obfuscated and not DRY.  Variables are created to represent common concepts, and comparison operators are used that don&#8217;t represent the real domain.  However, this is how I see most code in ActionScript written.  It is the path of least resistance to getting your project done.  Most languages are like this and give you the lowest levels of the API, without providing any means to working with them easier.  Why not give the developers a proper toolkit to make this more sensible?</p>
<p>So lets try cleaning it up:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> isTomorrowAfterToday<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Boolean</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #0066CC;">Date</span>.<span style="color: #006600;">tomorrow</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">isAfter</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">Date</span>.<span style="color: #006600;">today</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>See how much better that is.  So what have we done here? </p>
<ol>
<li>First, we&#8217;ve consolidated the creation logic (<code>Date.tomorrow()</code> and <code>Date.today()</code>), and they belong to the class they represent.  Not only is it readable, but it&#8217;s DRY.</li>
<li>Second, we&#8217;ve taken the greater than operator and made it into a method, <code>Date.isAfter()</code>.  Is tomorrow greater than today?  No!  Dates have no concept of greater than or less than.</li>
<li>Third we&#8217;ve removed accessors that return the primitive data that the class uses to store state.  Why should my code have to know that <code>Date.getTime()</code> returns the primitive data used to compare dates?  To selfishly put it; the only thing my code should care about is to ask the question, <em>Is tomorrow after today?</em></li>
</ol>
<p>Let&#8217;s take it one step further:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">date</span>.<span style="color: #006600;">tomorrow</span>;
<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">date</span>.<span style="color: #006600;">today</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> isTomorrowAfterToday<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Boolean</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">return</span> tomorrow<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">isAfter</span><span style="color: #66cc66;">&#40;</span>today<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Getting closer.  So what have we done?  Well, we&#8217;ve taken the concept of creation, and have moved it once again.  Creation can be generalized into the package of which the main class it represents.  In this case, the <code>Date</code> class can be given it&#8217;s own package <code>flash.date</code>.</p>
<p>Taking it another step further:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">comparison</span>.<span style="color: #006600;">is</span>;
<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">date</span>.<span style="color: #006600;">tomorrow</span>;
<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">date</span>.<span style="color: #006600;">today</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> isTomorrowAfterToday<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Boolean</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">return</span> is<span style="color: #66cc66;">&#40;</span>tomorrow<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">after</span><span style="color: #66cc66;">&#40;</span>today<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Perfect!  Again, so what have we done here?  We&#8217;ve abstracted the concept of equality into it&#8217;s own package.  We can express other concepts of comparison such as <code>not()</code>.</p>
<p>Like so:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">comparison</span>.<span style="color: #006600;">is</span>;
<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">comparison</span>.<span style="color: #0066CC;">not</span>;
<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">date</span>.<span style="color: #006600;">tomorrow</span>;
<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">date</span>.<span style="color: #006600;">today</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> isTomorrowNotTheSameAsToday<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Boolean</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">return</span> is<span style="color: #66cc66;">&#40;</span>tomorrow<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">not</span><span style="color: #66cc66;">&#40;</span>today<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Lets take a whole different concept, such as distance, and model the simple question, <em>Is 10 feet farther than 5 feet?</em></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">comparison</span>.<span style="color: #006600;">is</span>;
<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">distance</span>.<span style="color: #006600;">Distance</span>;
<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">imports</span>.<span style="color: #006600;">use</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> is10FeetFartherThan5Feet<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Boolean</span>
<span style="color: #66cc66;">&#123;</span>
    use<span style="color: #66cc66;">&#40;</span>Distance<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #b1b100;">return</span> is<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">feet</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">fartharThan</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">feet</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>You might be asking yourself, &#8220;Is this really ActionScript?&#8221;  The answer is yes, and can be accomplished with the use of ActionScript&#8217;s <a href="http://www.adobe.com/livedocs/flash/9.0/main/00000069.html#wp136347" target="_blank"><code>Object.prototype</code></a> static property.  Again what is all of this doing?</p>
<ol>
<li>First, we&#8217;re telling our function that we&#8217;ll be using classes and methods for distances.  This acts like an import statement, and adds prototype methods to Flash&#8217;s <code>Number</code> class.</li>
<li>Second, we&#8217;re creating distances based off of a number.  When we call <code>10.feet()</code> we&#8217;re calling a prototype method that we&#8217;ve defined on <code>Number</code> to return a <code>Distance</code> object of 10 feet.  In English, this reads much nicer.</li>
<li>Third, we&#8217;re reusing the concepts that we&#8217;ve established earlier.  <code>is()</code> is an equality concept that we defined within the <code>flash.comparison</code> package.  We&#8217;re using method names used to compare objects of the same type, <code>Distance.fartherThan()</code>.  Lastly, we&#8217;re comparing the primitive values of an object, by not having to compare the values of both distances explicitly with call like <code>Feet.value</code>.
</ol>
<p>Many of the concepts are things you can start using today.  Begin by adding reusable and readable comparison methods, such as <code>Person.isOlder()</code> or <code>Person.isYounger()</code>.  It&#8217;ll increase the reusability of your domain and make it much more rich.  Also, add and chain together static builder methods in your classes, i.e. <code>var dan:Person = Person.thatIs().male().age(24).name("dan")</code>.</p>
<p>Happy coding, and keep it clean!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.intriguemedia.net/2010/02/03/making-actionscript-look-beautiful-increasing-readability-and-loosing-verboseness/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Help Dan find a Flex Engineer and get a free paid trip for 2 to Hawaii</title>
		<link>http://www.intriguemedia.net/2009/12/08/help-dan-find-a-flex-engineer-and-get-a-free-paid-trip-for-2-to-hawaii/</link>
		<comments>http://www.intriguemedia.net/2009/12/08/help-dan-find-a-flex-engineer-and-get-a-free-paid-trip-for-2-to-hawaii/#comments</comments>
		<pubDate>Wed, 09 Dec 2009 03:48:53 +0000</pubDate>
		<dc:creator>Dan Schultz</dc:creator>
		
		<category><![CDATA[Flex]]></category>

		<category><![CDATA[Mixbook]]></category>

		<guid isPermaLink="false">http://www.intriguemedia.net/?p=104</guid>
		<description><![CDATA[Put me in touch with a Flex Engineer that we hire, and we&#8217;ll fly you and a friend to Hawaii, put you both up in a hotel, AND fly you back. Huh? Sick of making your friends jealous from all those Hawaii excursions?? Well, you have the option to choose the tech package, which includes [...]]]></description>
			<content:encoded><![CDATA[<p>Put me in touch with a Flex Engineer that we hire, and we&#8217;ll fly you and a friend to Hawaii, put you both up in a hotel, <em>AND</em> fly you back. Huh? Sick of making your friends jealous from all those Hawaii excursions?? Well, you have the option to choose the tech package, which includes a 42&#8243; LCD TV <em>and</em> <a href="http://www.apple.com/macbookair/" target="_blank">Macbook Air</a>. Gez, not interested in that either?? Then eBay it, and get cash for tech!</p>
<p>Take a look at our <a href="http://mixbook.jobscore.com/jobs/mixbook/flexapplicationengineer/a7dPB8Nkyr3PBJeJe4aGWH" target="_blank">job posting</a>, which has all the details about the position.</p>
<p>A little info about the company. Mixbook (<a href="http://www.mixbook.com" target="_blank">www.mixbook.com</a>) is a profitable, rapidly-growing San Jose, CA, startup that allows people to collaboratively create photo books through a powerful online Flex application. Users can share their creations online for free online – like a YouTube for photo books - or purchase bookstore-quality copies of their work to share in real life (see <a href="http://www.mixbook.com/printed-photo-books" target="_blank">http://www.mixbook.com/printed-photo-books</a>). Over 2 million people have created over 5 million photo books on Mixbook’s platform – your work will impact a community of millions.</p>
<p>We offer competitive salary and generous equity stakes, along with benefits, a new Mac or PC, dual monitors, and a chance to impact a community of millions. We’re looking for talented and passionate people who take pride in giving customers a phenomenal and enriching online experience. </p>
<p>Please share!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.intriguemedia.net/2009/12/08/help-dan-find-a-flex-engineer-and-get-a-free-paid-trip-for-2-to-hawaii/feed/</wfw:commentRss>
		</item>
		<item>
		<title>What&#8217;s Happening With Cayri?</title>
		<link>http://www.intriguemedia.net/2009/12/08/whats-happening-with-cayri/</link>
		<comments>http://www.intriguemedia.net/2009/12/08/whats-happening-with-cayri/#comments</comments>
		<pubDate>Tue, 08 Dec 2009 21:35:29 +0000</pubDate>
		<dc:creator>Dan Schultz</dc:creator>
		
		<category><![CDATA[Cayri]]></category>

		<guid isPermaLink="false">http://www.intriguemedia.net/?p=98</guid>
		<description><![CDATA[Just wanted to throw a quick update to anyone interested in the state of affairs with Cayri.  A while back my host shutdown the cron script that was parsing the VATSIM and IVAO data files.
Looking deeper into the problem, there appears to be a very inefficient database query which runs on each pilot that [...]]]></description>
			<content:encoded><![CDATA[<p>Just wanted to throw a quick update to anyone interested in the state of affairs with <a href="http://www.cayri.org">Cayri</a>.  A while back my host shutdown the cron script that was parsing the VATSIM and IVAO data files.</p>
<p>Looking deeper into the problem, there appears to be a very inefficient database query which runs on each pilot that is parsed from these files.  In addition, I believe the problem was compounded by the increase of users flying on both of these networks.</p>
<p>I will be having some available time over the winter holidays to hopefully fix this issue.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.intriguemedia.net/2009/12/08/whats-happening-with-cayri/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Saving Flex&#8217;s Application State to a Database</title>
		<link>http://www.intriguemedia.net/2008/03/10/saving-flexs-application-state-to-a-database/</link>
		<comments>http://www.intriguemedia.net/2008/03/10/saving-flexs-application-state-to-a-database/#comments</comments>
		<pubDate>Mon, 10 Mar 2008 20:08:59 +0000</pubDate>
		<dc:creator>Dan Schultz</dc:creator>
		
		<category><![CDATA[AIR]]></category>

		<category><![CDATA[Design Patterns]]></category>

		<category><![CDATA[Flex]]></category>

		<guid isPermaLink="false">http://www.intriguemedia.net/2008/03/10/saving-flexs-application-state-to-a-database/</guid>
		<description><![CDATA[Bill Sanders has a great post on the Memento design pattern over at his blog.  After reading it, I thought I&#8217;d chime in and post a technique that takes the memento one step further.  This approach allows you to serialize a Memento object into a ByteArray and save it into a database.  [...]]]></description>
			<content:encoded><![CDATA[<p>Bill Sanders has a great <a href="http://www.as3dp.com/2008/01/24/actionscript-30-memento-design-pattern-encapsulating-saved-states/" target="_blank">post</a> on the Memento design pattern over at his blog.  After reading it, I thought I&#8217;d chime in and post a technique that takes the memento one step further.  This approach allows you to serialize a Memento object into a ByteArray and save it into a database.  Then, retrieving the Memento object from the server, dynamically instantiated it, and restoring its previous state.</p>
<p>One major draw back to the Memento, as you&#8217;ll see, is that restoring an object&#8217;s state can be a very expensive operation.  Especially in a Flex environment where restoring a Memento could trigger a large display list invalidation.  For that reason, the Memento is not always the ideal pattern for implementing undo/redo functionality.  The alternative pattern for implementing this functionality is the Command pattern.  With the Command pattern, each command is only concerned about a particular action to get to the current state of the application.  With this, you can have an <em>undo()</em> method in each of your commands that undoes whatever action was performed in your <em>execute()</em> method.</p>
<p>Ok, so where could we apply the Memento then?  Hmm .. how about a Monopoly game!  Since a game of Monopoly can be pretty lengthy, the user might want to be able to save the game on the server, so that he/she can come back later to finish it off.  There&#8217;s a couple ways to accomplish this functionality:</p>
<ul>
<li>Create a multitude of database tables that represent objects in your application.  For example, a Player&#8217;s table to keep track of the current players, how much money they have, etc.  A Properties table to keep track of the game&#8217;s properties.  A Properties to Players linking table to store which properties each player owns, and so on.</li>
<li>OR, create one table whose role is to store Mementos for each of the objects in the game.  I think this approach sounds easier, and since it&#8217;s in the scope of this article, we&#8217;re going to focus on this approach.</li>
</ul>
<p><strong>The Memento Classes</strong><br />
<img src="http://www.intriguemedia.net/wp-content/uploads/2008/03/memento.jpg" alt="Memento UML" /><br />
In order for our application to save its state using the Memento pattern, we need to write a couple classes and interfaces that our objects will implement.  Note that my Memento classes slightly differ from that described on <a href="http://en.wikipedia.org/wiki/Memento_pattern" target="_blank">Wikipedia</a> and <a href="http://www.as3dp.com/2008/01/24/actionscript-30-memento-design-pattern-encapsulating-saved-states/" target="_blank">Bill Sanders blog</a>.  The reason for this was that I wanted to inspect which type of object each Memento originated from.  By doing this, you can re-instantiate the Memento dynamically when it comes back from the database.  I also wanted any classes that supported restoration to be of a certain type â€“ <em>IRestorable</em>.</p>
<p>Each class that you want to save and restore state to must implement the <em>IRestorable</em> interface.  This interface has two simple methods that either creates or accepts a Memento object.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> IRestorable
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">function</span> restore<span style="color: #66cc66;">&#40;</span>state:ObjectState<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
    <span style="color: #000000; font-weight: bold;">function</span> save<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:ObjectState;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Next is the Memento class that is called <em>ObjectState</em>.  This class is where you store all the data that the class needs to restore its state back to what it was.  These objects can also be serialized and saved to the database (<a href="#saving">more on this later</a>).  You&#8217;ll notice that I&#8217;ve marked this class as dynamic so that you can add anything you want to it.  The only defined property it has is <em>className</em>.  You set this to the fully qualified class name that generated the Memento.  Another important thing to note: you <strong>must</strong> put the [RemoteClass] metatag in the <em>ObjectState</em> class definition.  I&#8217;ll <a href="#retrieving">show you later</a> how you can use this property to re-instantiate the object based on this class name property.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #66cc66;">&#91;</span>RemoteClass<span style="color: #66cc66;">&#40;</span>alias=<span style="color: #ff0000;">&quot;my.custom.package.ObjectState&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">dynamic</span> <span style="color: #000000; font-weight: bold;">class</span> ObjectState
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> className:<span style="color: #0066CC;">String</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Pretty simple stuff, huh?  Let&#8217;s take a look at a simple class that implements both of these classes.  This class, called <em>MyCustomComp</em>, implements the IRestorable interface with the two methods described above.  The <em>save()</em> method&#8217;s job is to take all the data that this component needs to re-instantiate itself and save it to a new ObjectState.  In our case, it&#8217;s the text strings from the three TextInput controls.  The <em>restore()</em> method&#8217;s job is to take those three text strings, and reapply them to our TextInput controls.  So whatever is done in the <em>restore()</em> method, needs to be saved to the ObjectState in the <em>save()</em> method.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyCustomComp <span style="color: #0066CC;">extends</span> UIComponent <span style="color: #0066CC;">implements</span> IRestorable
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #808080; font-style: italic;">// Sub-components in our component that we want to</span>
    <span style="color: #808080; font-style: italic;">// restore the data to.</span>
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> textInput1:TextInput;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> textInput2:TextInput;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> textInput3:TextInput;
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> MyCustomComp<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    override protected <span style="color: #000000; font-weight: bold;">function</span> createChildren<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0066CC;">super</span>.<span style="color: #006600;">createChildren</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #808080; font-style: italic;">// Create the sub-components.</span>
        textInput1 = <span style="color: #000000; font-weight: bold;">new</span> TextInput<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        textInput1.<span style="color: #0066CC;">text</span> = <span style="color: #ff0000;">&quot;Text 1&quot;</span>;
        addChild<span style="color: #66cc66;">&#40;</span>textInput1<span style="color: #66cc66;">&#41;</span>;
&nbsp;
        textInput2 = <span style="color: #000000; font-weight: bold;">new</span> TextInput<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        textInput2.<span style="color: #0066CC;">text</span> = <span style="color: #ff0000;">&quot;Text 2&quot;</span>;
        addChild<span style="color: #66cc66;">&#40;</span>textInput2<span style="color: #66cc66;">&#41;</span>;
&nbsp;
        textInput3 = <span style="color: #000000; font-weight: bold;">new</span> TextInput<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        textInput3.<span style="color: #0066CC;">text</span> = <span style="color: #ff0000;">&quot;Text 3&quot;</span>;
        addChild<span style="color: #66cc66;">&#40;</span>textInput3<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> restore<span style="color: #66cc66;">&#40;</span>state:ObjectState<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #808080; font-style: italic;">// Make sure to throw a TypeError if the class name is</span>
        <span style="color: #808080; font-style: italic;">// incorrect.</span>
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>state.<span style="color: #006600;">className</span> <span style="color: #66cc66;">!</span>= flash.<span style="color: #006600;">utils</span>.<span style="color: #006600;">getQualifiedClassName</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> TypeError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;This ObjectState was not created from the MyCustomComp class&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #808080; font-style: italic;">// Take the text data from the ObjectState and restore</span>
        <span style="color: #808080; font-style: italic;">// it to our sub-components.</span>
        textInput1.<span style="color: #0066CC;">text</span> = state.<span style="color: #006600;">text1</span>;
        textInput2.<span style="color: #0066CC;">text</span> = state.<span style="color: #006600;">text2</span>;
        textInput3.<span style="color: #0066CC;">text</span> = state.<span style="color: #006600;">text3</span>;
&nbsp;
        <span style="color: #808080; font-style: italic;">// Restore the position of the component.</span>
        x = state.<span style="color: #006600;">xPos</span>;
        y = state.<span style="color: #006600;">yPos</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> save<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:ObjectState
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">var</span> state:ObjectState = <span style="color: #000000; font-weight: bold;">new</span> ObjectState<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        state.<span style="color: #006600;">className</span> = flash.<span style="color: #006600;">utils</span>.<span style="color: #006600;">getQualifiedClassName</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #808080; font-style: italic;">// Since the ObjectState is dynamic, we can set anything</span>
        <span style="color: #808080; font-style: italic;">// on it.  Set the text from each of the sub-components</span>
        <span style="color: #808080; font-style: italic;">// into our memento object.</span>
        state.<span style="color: #006600;">text1</span> = textInput1.<span style="color: #0066CC;">text</span>;
        state.<span style="color: #006600;">text2</span> = textInput2.<span style="color: #0066CC;">text</span>;
        state.<span style="color: #006600;">text3</span> = textInput3.<span style="color: #0066CC;">text</span>;
&nbsp;
        <span style="color: #808080; font-style: italic;">// Save the X and Y positioning of this component.</span>
        state.<span style="color: #006600;">xPos</span> = x;
        state.<span style="color: #006600;">yPos</span> = y;
&nbsp;
        <span style="color: #b1b100;">return</span> state;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p><a name="saving"></a><strong>Serializing the ObjectState to the Database</strong><br />
When it&#8217;s time to send the ObjectState across the wire and save it to the database, we need to do a simple operation first.  This operation will save the ObjectState into a ByteArray so that the database can store it.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// The component that we want to save and restore data to.</span>
<span style="color: #000000; font-weight: bold;">var</span> myRestorableComp:MyCustomComp = <span style="color: #000000; font-weight: bold;">new</span> MyCustomComp<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; 
&nbsp;
<span style="color: #808080; font-style: italic;">// Save the component's state.</span>
<span style="color: #000000; font-weight: bold;">var</span> state:ObjectState = myRestorableComp.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// Write the state object into a ByteArray.</span>
<span style="color: #000000; font-weight: bold;">var</span> stateBytes:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> ByteArray<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
stateBytes.<span style="color: #006600;">writeObject</span><span style="color: #66cc66;">&#40;</span>state<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// Code to send the ByteArray to the server via Flash Remoting.</span>
myService:RemoteObject = myRemotingService;
myService.<span style="color: #006600;">saveMemento</span><span style="color: #66cc66;">&#40;</span>stateBytes<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Also, in order to save serialized objects to a database, you need to set the field in which they&#8217;re stored as a BLOB.  This way, the serialized structure of the object isn&#8217;t modified when you save it.</p>
<p><a name="retrieving"></a><strong>Retrieving the Memento from the Server</strong><br />
After we save the state to the database, a user will more than likely want to retrieve it.  Also, you might want to dynamically re-instantiate the component that the Memento&#8217;s originated from.  You can achieve this by passing the ObjectState&#8217;s <em>className</em> property into Flash&#8217;s <em>flash.utils.getDefinitionByName()</em> method.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyCompContainer <span style="color: #0066CC;">extends</span> Canvas
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> service:RemoteObject;
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> MyCompContainer<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        service = <span style="color: #000000; font-weight: bold;">new</span> RemoteObject<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        service.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>ResultEvent.<span style="color: #006600;">RESULT</span>, retrievedMementoHandler<span style="color: #66cc66;">&#41;</span>;
        ...
        <span style="color: #006600;">service</span>.<span style="color: #006600;">retrieveMemento</span><span style="color: #66cc66;">&#40;</span>someUniqueMementoId<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">// The result handler that was added to the remoting object.</span>
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> retrievedMementoHandler<span style="color: #66cc66;">&#40;</span>event:ResultEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #808080; font-style: italic;">// We saved the ObjectState into a ByteArray, so we're</span>
        <span style="color: #808080; font-style: italic;">// going to get it back as a ByteArray.</span>
        <span style="color: #000000; font-weight: bold;">var</span> stateBytes:ByteArray = event.<span style="color: #006600;">result</span> as ByteArray;
        stateBytes.<span style="color: #0066CC;">position</span> = <span style="color: #cc66cc;">0</span>;
&nbsp;
        <span style="color: #808080; font-style: italic;">// Read the Memento from the ByteArray.</span>
        <span style="color: #000000; font-weight: bold;">var</span> compState:ObjectState = stateBytes.<span style="color: #006600;">readObject</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> as ObjectState;
&nbsp;
        <span style="color: #808080; font-style: italic;">// We can inspect the type that the ObjectState</span>
        <span style="color: #808080; font-style: italic;">// originated from.</span>
        <span style="color: #000000; font-weight: bold;">var</span> restorableComp:IRestorable = flash.<span style="color: #006600;">utils</span>.<span style="color: #006600;">getDefinitionByName</span><span style="color: #66cc66;">&#40;</span>compState.<span style="color: #006600;">className</span><span style="color: #66cc66;">&#41;</span>;
        restorableComp.<span style="color: #006600;">restore</span><span style="color: #66cc66;">&#40;</span>compState<span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #808080; font-style: italic;">// If this is a UIComponent, let's add it to the stage.</span>
        <span style="color: #808080; font-style: italic;">// Since we saved the x and y positioning in the</span>
        <span style="color: #808080; font-style: italic;">// ObjectState, it will position itself to where it was</span>
        <span style="color: #808080; font-style: italic;">// when it was saved.</span>
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>restorableComp is UIComponent<span style="color: #66cc66;">&#41;</span>
            addChild<span style="color: #66cc66;">&#40;</span>restorableComp as UIComponent<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>By using the Memento pattern, we&#8217;ve encapsulate the implementation of storing and retrieving an object&#8217;s state.  We&#8217;ve also made it generic enough, that we can serialize the object&#8217;s state and store it into a database table.  Once in the database table, the state can be retrieved and instantiated later.</p>
<p><strong>Pitfalls of this Approach</strong><br />
Like any design pattern out there, this approach has its draw backs.  One consequence of this technique is the difficulty to change the data inside your Mementos.  The reason for this is the way that we use serialization.  Since the objects are serialized in the database, you can&#8217;t easily add additional data to the ObjectStates.  However, there are workarounds for this.  You can save a version number along with your ObjectState.  This version number can be used in conjunction with a separate class that deals with reading each ObjectState&#8217;s data.  When the class inspects the version number, it can change its parsing logic to deal with different versions of the ObjectState.</p>
<p>Another drawback is the inability to do a database query on the data inside your ObjectStates.  Again, since the objects are serialized, there&#8217;s no easy way to do this.  You can, however, work around this though by storing the query data in either a separate table, or along side the ObjectState when it&#8217;s saved.</p>
<p>Hopefully this technique is something you can add to your toolkit.  It definitely has its share of drawbacks, but if you&#8217;re looking for an easy way to save the state of your application, this is certainly a pretty trivial and elegant way of accomplishing it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.intriguemedia.net/2008/03/10/saving-flexs-application-state-to-a-database/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Loading Modules/Runtime CSS into an AIR App</title>
		<link>http://www.intriguemedia.net/2008/02/22/loading-modulesruntime-css-into-an-air-app/</link>
		<comments>http://www.intriguemedia.net/2008/02/22/loading-modulesruntime-css-into-an-air-app/#comments</comments>
		<pubDate>Sat, 23 Feb 2008 05:04:55 +0000</pubDate>
		<dc:creator>Dan Schultz</dc:creator>
		
		<category><![CDATA[AIR]]></category>

		<guid isPermaLink="false">http://www.intriguemedia.net/2008/02/22/loading-modulesruntime-css-into-an-air-app/</guid>
		<description><![CDATA[For those planning to load a Flex module into your AIR application from a remote server: take a look at this post before beating your head against the wall with this ambiguous error message Error: Unable to load style(SWF is not a loadable module).
Basically, loaded SWF&#8217;s are not allowed to be in the same security [...]]]></description>
			<content:encoded><![CDATA[<p>For those planning to load a Flex module into your AIR application from a remote server: take a look at <a href="http://tech.groups.yahoo.com/group/apollocoders/message/2124" target="_blank">this post</a> before beating your head against the wall with this ambiguous error message <code>Error: Unable to load style(SWF is not a loadable module)</code>.</p>
<p>Basically, loaded SWF&#8217;s are not allowed to be in the same security domain as your AIR app.  Since modules and external styles loaded using <code>StyleManager.loadStyleDeclaration()</code> try to load themselves into the current security domain, an error will be thrown.  The only workaround it appears is to bundle those modules with your application when it&#8217;s downloaded.  Any new modules you want to add to your application at a later time can be downloaded using AIR&#8217;s update API via <code>Updater.update()</code>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.intriguemedia.net/2008/02/22/loading-modulesruntime-css-into-an-air-app/feed/</wfw:commentRss>
		</item>
		<item>
		<title>CollectionEventKind.RESET, WTF Does That Mean?</title>
		<link>http://www.intriguemedia.net/2007/10/06/collectioneventkindreset-wtf-does-that-mean/</link>
		<comments>http://www.intriguemedia.net/2007/10/06/collectioneventkindreset-wtf-does-that-mean/#comments</comments>
		<pubDate>Sun, 07 Oct 2007 00:39:43 +0000</pubDate>
		<dc:creator>Dan Schultz</dc:creator>
		
		<category><![CDATA[AIR]]></category>

		<category><![CDATA[Flex]]></category>

		<guid isPermaLink="false">http://www.intriguemedia.net/?p=29</guid>
		<description><![CDATA[When handling Flex&#8217;s collection change events, most of the change types are pretty self-explanatory.  For example, CollectionEventKind.ADD is when an item is added to the collection, or CollectionEventKind.UPDATE is when an item&#8217;s property inside the collection changes.  However, there&#8217;s this ambiguous change type called RESET.  The documentation does a wonderful job of [...]]]></description>
			<content:encoded><![CDATA[<p>When handling Flex&#8217;s collection change events, most of the change types are pretty self-explanatory.  For example, <code>CollectionEventKind.ADD</code> is when an item is added to the collection, or <code>CollectionEventKind.UPDATE</code> is when an item&#8217;s property inside the collection changes.  However, there&#8217;s this ambiguous change type called RESET.  The documentation does a wonderful job of describing when this type of collection event is fired: <em>Indicates that the collection has changed so <strong>drastically</strong> that a reset is required.</em> (<a href="http://livedocs.adobe.com/flex/201/langref/mx/events/CollectionEventKind.html#RESET" title="CollectionEventKind" target="_blank">ASDoc Link</a>)</p>
<p>Digging through ListCollectionView provides some better insight of when RESET is dispatched.  Anytime a collection is passed to <code>ListCollectionView.list</code>, and both the old list and the new list has items in it, it will fire a reset collection change event.  Here&#8217;s the code Adobe uses for setting a list on ListCollectionView:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> <span style="color: #0066CC;">list</span><span style="color: #66cc66;">&#40;</span>value:IList<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>_list <span style="color: #66cc66;">!</span>= value<span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">var</span> oldHasItems:<span style="color: #0066CC;">Boolean</span>;
            <span style="color: #000000; font-weight: bold;">var</span> newHasItems:<span style="color: #0066CC;">Boolean</span>;
            <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>_list<span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#123;</span>
                _list.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>CollectionEvent.<span style="color: #006600;">COLLECTION_CHANGE</span>,
                                          listChangeHandler<span style="color: #66cc66;">&#41;</span>;
                oldHasItems = _list.<span style="color: #0066CC;">length</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span>;
            <span style="color: #66cc66;">&#125;</span>
&nbsp;
            _list = value;
&nbsp;
            <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>_list<span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#123;</span>
                <span style="color: #808080; font-style: italic;">// weak listeners to collections and dataproviders</span>
                _list.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>CollectionEvent.<span style="color: #006600;">COLLECTION_CHANGE</span>,
                                       listChangeHandler, <span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
                newHasItems = _list.<span style="color: #0066CC;">length</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span>;
            <span style="color: #66cc66;">&#125;</span>
&nbsp;
            <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>oldHasItems <span style="color: #66cc66;">||</span> newHasItems<span style="color: #66cc66;">&#41;</span>
                reset<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
            dispatchEvent<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Event<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;listChanged&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Lastly, ListCollectionView is a pretty useful class.  A lot of new Flex developers go straight to using ArrayCollection, and forget about its father, ListCollectionView.  This collection allows you to apply different sorts and filters on a collection without directly modifying the wrapped collection.  You can also call <code>addItem()</code> and <code>removeItemAt()</code> on ListCollectionView and have it modify the wrapped collection.  Pretty nifty! Here&#8217;s an example of it in action:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> col:ArrayCollection = <span style="color: #000000; font-weight: bold;">new</span> ArrayCollection<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
col.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'a'</span><span style="color: #66cc66;">&#41;</span>;
col.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'b'</span><span style="color: #66cc66;">&#41;</span>;
col.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'c'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">var</span> wrapper:ListCollectionView = <span style="color: #000000; font-weight: bold;">new</span> ListCollectionView<span style="color: #66cc66;">&#40;</span>col<span style="color: #66cc66;">&#41;</span>;
wrapper.<span style="color: #006600;">removeItemAt</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
wrapper.<span style="color: #006600;">removeItemAt</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
wrapper.<span style="color: #006600;">removeItemAt</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>col.<span style="color: #0066CC;">length</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// traces: 0</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.intriguemedia.net/2007/10/06/collectioneventkindreset-wtf-does-that-mean/feed/</wfw:commentRss>
		</item>
		<item>
		<title>When to Use Weak References</title>
		<link>http://www.intriguemedia.net/2007/09/24/when-to-use-weak-references/</link>
		<comments>http://www.intriguemedia.net/2007/09/24/when-to-use-weak-references/#comments</comments>
		<pubDate>Mon, 24 Sep 2007 22:34:40 +0000</pubDate>
		<dc:creator>Dan Schultz</dc:creator>
		
		<category><![CDATA[Design Patterns]]></category>

		<category><![CDATA[Flex]]></category>

		<guid isPermaLink="false">http://www.intriguemedia.net/?p=28</guid>
		<description><![CDATA[Understanding when to use a weak references, versus strong references can be a confusing one.  It took me awhile to fully grasp when, and when not to use one over the other.  On top of that, there&#8217;s very little documentation on the LiveDocs about the subject.  Under the documentation for IEventDispatcher.addEventListener(), it [...]]]></description>
			<content:encoded><![CDATA[<p>Understanding when to use a weak references, versus strong references can be a confusing one.  It took me awhile to fully grasp when, and when not to use one over the other.  On top of that, there&#8217;s very little documentation on the LiveDocs about the subject.  Under the documentation for <code>IEventDispatcher.addEventListener()</code>, it states:</p>
<blockquote><p><em>useWeakReference:Boolean (default = false) â€” Determines whether the reference to the listener is strong or weak. A strong reference (the default) prevents your listener from being garbage-collected. A weak reference does not.</em></p></blockquote>
<p>And here&#8217;s what they have listed for the Dictionary&#8217;s constructor:</p>
<blockquote><p><em>weakKeys:Boolean (default = false) â€” Instructs the Dictionary object to use &#8220;weak&#8221; references on object keys. If the only reference to an object is in the specified Dictionary object, the key is eligible for garbage collection and is removed from the table when the object is collected.</em></p></blockquote>
<p><em>Ok &#8230; thanks.</em> So, I did some Googling, <span style="text-decoration: line-through;">and both <a title=" MAX - ActionScript 3 Performance Tuning - Strong Typing" href="http://www.onflex.org/ted/2006/09/max-actionscript-3-performance-tuning.php" target="_blank">Ted Patrick</a></span> and <a title="Flex Tip of the Day: Do you need a weak reference in your event listener?" href="http://www.colettas.org/?p=115" target="_blank">David Coletta</a> have much better information on the subject.</p>
<p>The basic idea behind strong references and <code>addEventListener</code> is this: when you call <code>addEventListener</code> and you pass it the function that you want to be called when the event fires, the event dispatcher is actually grabbing and holding a reference to that class and function it contains.  As we all know, an object won&#8217;t be garbage collected until every reference to it doesn&#8217;t exist anymore.  This insures that the event dispatcher always has a reference to the function that needs to be called.  However, if you&#8217;re not careful, this can be a breeding ground for nasty memory leaks.</p>
<p>So, here&#8217;s my short list of do&#8217;s and don&#8217;ts related to strong vs weak references and event listeners:</p>
<ul>
<li>Always use a weak reference when adding an event listener to a class that&#8217;s a singleton, or any object that you expect to live throughout the life of your application.  Remember, this applies to most of Flex&#8217;s manager classes, including the SystemManager, and any event dispatching classes that your singleton&#8217;s contain.</li>
<li>Always use weak references when adding Cairngorm commands.  The event dispatcher in Cairngorm is a singleton, and will always keep references to those command classes.</li>
<li><span style="text-decoration: line-through;">According to Ted, weak references are 10x slower.</span> (True when referring to member access, but not event listening.)</li>
<li>It&#8217;s safe to use strong references when you&#8217;re adding an event listener to itself (i.e. the instance dispatching the event).</li>
<li>You should use a weak reference when adding an event listener to the Timer class.</li>
<li>Lastly, always make sure to clean up and remove any event listeners your classes are using when they&#8217;re no longer needed.  I follow .NET&#8217;s approach, and I have an interface called IDisposable.  This interface contains a single method called <code>dispose()</code>, and inside I remove any event listeners that the class might be using.  Additionally, you could clean-up any BitmapData objects you might be using, among other things.  Most, if not all, of my classes that I write implement this interface, and I just make sure to call <code>dispose()</code> when I know the object is no longer needed.</li>
</ul>
<p>If there&#8217;s anything else to look out for, I&#8217;d love to hear them.  It&#8217;s always good to be proactive when it comes to memory leaks.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.intriguemedia.net/2007/09/24/when-to-use-weak-references/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Embedding Cayri on Your Website</title>
		<link>http://www.intriguemedia.net/2007/09/12/embedding-cayri-on-your-website/</link>
		<comments>http://www.intriguemedia.net/2007/09/12/embedding-cayri-on-your-website/#comments</comments>
		<pubDate>Wed, 12 Sep 2007 22:48:59 +0000</pubDate>
		<dc:creator>Dan Schultz</dc:creator>
		
		<category><![CDATA[Cayri]]></category>

		<guid isPermaLink="false">http://www.intriguemedia.net/?p=27</guid>
		<description><![CDATA[Lately I&#8217;ve been getting emails from people asking me if they can embed Cayri on their virtual airline&#8217;s website.   So I think it&#8217;s about time to post a small tutorial on how to do this.   The simplest way to do it is by using an IFRAME tag with HTML.  Just [...]]]></description>
			<content:encoded><![CDATA[<p>Lately I&#8217;ve been getting emails from people asking me if they can embed <a href="http://www.cayri.org">Cayri</a> on their virtual airline&#8217;s website.   So I think it&#8217;s about time to post a small tutorial on how to do this.   The simplest way to do it is by using an <a href="http://www.w3schools.com/tags/tag_iframe.asp" target="_blank">IFRAME</a> tag with HTML.  Just stick the following code in any HTML page, and Cayri will show up on your website:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">iframe</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.cayri.org/?network=vatsim&quot;</span> <span style="color: #000066;">frameborder</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;500&quot;</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;700&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">iframe</span>&gt;</span></pre></td></tr></table></div>

<p>If you would like to show IVAO traffic instead of VATSIM traffic, you&#8217;ll need to replace <strong>?network=vatsim</strong> with <strong>?network=ivao</strong> in the <strong>src</strong> attribute.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.intriguemedia.net/2007/09/12/embedding-cayri-on-your-website/feed/</wfw:commentRss>
		</item>
		<item>
		<title>The State Pattern in Flex 2</title>
		<link>http://www.intriguemedia.net/2007/08/31/the-state-pattern-in-flex-2/</link>
		<comments>http://www.intriguemedia.net/2007/08/31/the-state-pattern-in-flex-2/#comments</comments>
		<pubDate>Sat, 01 Sep 2007 04:36:16 +0000</pubDate>
		<dc:creator>Dan Schultz</dc:creator>
		
		<category><![CDATA[Design Patterns]]></category>

		<category><![CDATA[Flex]]></category>

		<guid isPermaLink="false">http://www.intriguemedia.net/?p=26</guid>
		<description><![CDATA[I&#8217;m currently working on Flex project at Quality Attributes Software that requires the use of a set of tools, similar to Photoshop, to manipulate a drawing canvas.  Some examples of tools might be a drag tool, a selection tool, and a zoom tool.  I thought to myself, &#8220;Ok, simple enough.  Throw all [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.intriguemedia.net/2007/08/31/the-state-pattern-in-flex-2/photoshop-tools-thumb/" rel="attachment wp-att-36" title="Photoshop Tools Thumb"><img src="http://www.intriguemedia.net/wp-content/uploads/2007/08/photoshop_tools.jpg" title="Photoshop Tools Thumb" alt="Photoshop Tools Thumb" align="right" /></a>I&#8217;m currently working on Flex project at <a href="http://www.qualityattributes.com" target="_blank">Quality Attributes Software</a> that requires the use of a set of tools, similar to Photoshop, to manipulate a drawing canvas.  Some examples of tools might be a drag tool, a selection tool, and a zoom tool.  I thought to myself, &#8220;<em>Ok, simple enough.  Throw all the logic into a switch statement inside a generic on click button handler.</em>&#8221;  So, when a user clicks on a new tool, disable all the functionality from the previous tool, and enable the functionality of the new one.  Ugh!  This was a complete disaster.  What happens when you expose all this logic, is that you run into some very <em>highly </em>coupled code.  Meaning that each tool is essentially keeping the state of the other tools.  Yuck!</p>
<p>There&#8217;s definitely a better way to handle these situations, and that&#8217;s by using the <strong>state pattern</strong>.  Essentially, I turned each tool into a state.  So when a user clicks on the Selection Tool, the program changes its state to use the functionality of selecting objects on the drawing canvas.  I hide all this logic behind an interface, and each tool is represented as a class that implements this interface.  If you&#8217;d like some code examples of this pattern, William Sanders has a great write up over at <a href="http://www.adobe.com/devnet/flashmediaserver/articles/video_state_machine_as3_03.html" target="_blank">Adobe&#8217;s Developer Center</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.intriguemedia.net/2007/08/31/the-state-pattern-in-flex-2/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Aircraft Heading Information for IVAO</title>
		<link>http://www.intriguemedia.net/2007/07/25/aircraft-heading-information-for-ivao/</link>
		<comments>http://www.intriguemedia.net/2007/07/25/aircraft-heading-information-for-ivao/#comments</comments>
		<pubDate>Wed, 25 Jul 2007 06:45:06 +0000</pubDate>
		<dc:creator>Dan Schultz</dc:creator>
		
		<category><![CDATA[Cayri]]></category>

		<guid isPermaLink="false">http://www.intriguemedia.net/?p=24</guid>
		<description><![CDATA[The heading information for aircraft on the IVAO network has finally been implemented! Thank you for the staff over at IVAO for modifying their server data to supply this information. The difference is truly night and day.
]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.intriguemedia.net/wp-content/uploads/2007/07/cayri_heading_ivao.jpg" title="cayri_heading_ivao.jpg" alt="cayri_heading_ivao.jpg" align="right" height="112" width="188" />The heading information for aircraft on the IVAO network has finally been implemented! Thank you for the staff over at IVAO for modifying their server data to supply this information. The difference is truly night and day.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.intriguemedia.net/2007/07/25/aircraft-heading-information-for-ivao/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
