<?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>Flexperiential</title>
	<atom:link href="http://flexperiential.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://flexperiential.com</link>
	<description>Are You Flexperienced?</description>
	<lastBuildDate>Wed, 03 Aug 2011 18:16:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Overriding getCurrentRendererState to Control Mouse-Over Behavior in Flex DataGroup</title>
		<link>http://flexperiential.com/2011/03/21/overriding-getcurrentrendererstate-to-control-mouse-over-behavior-in-flex-datagroup/</link>
		<comments>http://flexperiential.com/2011/03/21/overriding-getcurrentrendererstate-to-control-mouse-over-behavior-in-flex-datagroup/#comments</comments>
		<pubDate>Mon, 21 Mar 2011 14:37:15 +0000</pubDate>
		<dc:creator>David Salahi</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[DataGrid]]></category>
		<category><![CDATA[DataGroup]]></category>
		<category><![CDATA[ItemRenderer]]></category>

		<guid isPermaLink="false">http://flexperiential.com/?p=825</guid>
		<description><![CDATA[This application demonstrates the problem with the default ItemRenderer behavior when using view states within an item renderer. In the example on the left the added content disappears as soon as you mouse out of the expanded cell. The example on the right overrides getCurrentRendererState to prevent that. Right-click the app to view source or [...]]]></description>
			<content:encoded><![CDATA[<p></p><div style="float:left; width:385px; padding-right:15px;">
    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_0" width="385" height="280">
      <param name="movie" value="http://flexperiential.com/code/GetCurrentRendererState/GetCurrentRendererStateExample.swf" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://flexperiential.com/code/GetCurrentRendererState/GetCurrentRendererStateExample.swf" width="385" height="280">
      <!--<![endif]-->
        
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>

<p style="font-size:small; font-style:italic">This application demonstrates the problem with the default ItemRenderer behavior when using view states within an item renderer. In the example on the left the added content disappears as soon as you mouse out of the expanded cell. The example on the right overrides getCurrentRendererState to prevent that. Right-click the app to view source or <a href="http://flexperiential.com/code/GetCurrentRendererState/GetCurrentRendererStateExample.fxp">download an FXP file here</a>.</p>
</div>
<p>One of the things that makes Flex so attractive is the  extensive set of default behaviors provided in its many controls. Sometimes,  however, that default behavior is not what you want and you have to work with  Flex to get your desired behavior. I ran into a case of that recently when  working with a DataGroup. </p>
<p> I created an ItemRenderer which contained a button the user  could click to expand the cell to provide some additional information about the  product in that cell. That worked fine but I discovered that the DataGrid row collapsed  back to its original size as soon as I moved the mouse out of that cell. The  problem is that the Flex ItemRenderer base class includes some default  behaviors for mouse over events (among others). For mouse over, the current  cell is highlighted and, of course, for mouse out the highlight is removed.  This was fine but, in my case, the problem was that when I moused out, the  ItemRenderer reverted to its normal state—which was the state without the added  content. Here’s my original item renderer:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">&lt;?</span>xml <span style="color: #004993;">version</span>=<span style="color: #990000;">&quot;1.0&quot;</span> encoding=<span style="color: #990000;">&quot;utf-8&quot;</span><span style="color: #000066; font-weight: bold;">?&gt;</span>
<span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>ItemRenderer xmlns<span style="color: #000066; font-weight: bold;">:</span>fx=<span style="color: #990000;">&quot;http://ns.adobe.com/mxml/2009&quot;</span> 
				xmlns<span style="color: #000066; font-weight: bold;">:</span>s=<span style="color: #990000;">&quot;library://ns.adobe.com/flex/spark&quot;</span> 
				xmlns<span style="color: #000066; font-weight: bold;">:</span>mx=<span style="color: #990000;">&quot;library://ns.adobe.com/flex/mx&quot;</span> 
				autoDrawBackground=<span style="color: #990000;">&quot;true&quot;</span><span style="color: #000066; font-weight: bold;">&gt;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>states<span style="color: #000066; font-weight: bold;">&gt;</span>
		<span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>State <span style="color: #004993;">name</span>=<span style="color: #990000;">&quot;normal&quot;</span> <span style="color: #000066; font-weight: bold;">/&gt;</span>
		<span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>State <span style="color: #004993;">name</span>=<span style="color: #990000;">&quot;details&quot;</span> <span style="color: #000066; font-weight: bold;">/&gt;</span>
	<span style="color: #000066; font-weight: bold;">&lt;/</span>s<span style="color: #000066; font-weight: bold;">:</span>states<span style="color: #000066; font-weight: bold;">&gt;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>Rect <span style="color: #004993;">height</span>=<span style="color: #990000;">&quot;100%&quot;</span> <span style="color: #004993;">width</span>=<span style="color: #990000;">&quot;100%&quot;</span><span style="color: #000066; font-weight: bold;">&gt;</span>
		<span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>stroke<span style="color: #000066; font-weight: bold;">&gt;</span>
			<span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>SolidColorStroke <span style="color: #004993;">color</span>=<span style="color: #990000;">&quot;black&quot;</span><span style="color: #000066; font-weight: bold;">/&gt;</span>
		<span style="color: #000066; font-weight: bold;">&lt;/</span>s<span style="color: #000066; font-weight: bold;">:</span>stroke<span style="color: #000066; font-weight: bold;">&gt;</span>
	<span style="color: #000066; font-weight: bold;">&lt;/</span>s<span style="color: #000066; font-weight: bold;">:</span>Rect<span style="color: #000066; font-weight: bold;">&gt;</span>
	<span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>VGroup paddingTop=<span style="color: #990000;">&quot;10&quot;</span> paddingLeft=<span style="color: #990000;">&quot;10&quot;</span> paddingBottom=<span style="color: #990000;">&quot;10&quot;</span> paddingRight=<span style="color: #990000;">&quot;10&quot;</span><span style="color: #000066; font-weight: bold;">&gt;</span>
		<span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>Label <span style="color: #004993;">text</span>=<span style="color: #990000;">&quot;Always Displayed&quot;</span> includeIn=<span style="color: #990000;">&quot;normal, details&quot;</span><span style="color: #000066; font-weight: bold;">/&gt;</span>
		<span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>Label <span style="color: #004993;">text</span>=<span style="color: #990000;">&quot;{data}&quot;</span><span style="color: #000066; font-weight: bold;">/&gt;</span>
		<span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>Group<span style="color: #000066; font-weight: bold;">&gt;</span>
			<span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>Button label=<span style="color: #990000;">&quot;Add 2nd label&quot;</span> includeIn=<span style="color: #990000;">&quot;normal&quot;</span> <span style="color: #004993;">click</span>=<span style="color: #990000;">&quot;currentState='details'&quot;</span> <span style="color: #000066; font-weight: bold;">/&gt;</span>
			<span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>Button label=<span style="color: #990000;">&quot;Remove 2nd label&quot;</span> includeIn=<span style="color: #990000;">&quot;details&quot;</span> <span style="color: #004993;">click</span>=<span style="color: #990000;">&quot;currentState='normal'&quot;</span><span style="color: #000066; font-weight: bold;">/&gt;</span>
		<span style="color: #000066; font-weight: bold;">&lt;/</span>s<span style="color: #000066; font-weight: bold;">:</span>Group<span style="color: #000066; font-weight: bold;">&gt;</span>
		<span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>Label <span style="color: #004993;">text</span>=<span style="color: #990000;">&quot;Detailed description&quot;</span> includeIn=<span style="color: #990000;">&quot;details&quot;</span><span style="color: #000066; font-weight: bold;">/&gt;</span>
	<span style="color: #000066; font-weight: bold;">&lt;/</span>s<span style="color: #000066; font-weight: bold;">:</span>VGroup<span style="color: #000066; font-weight: bold;">&gt;</span>
<span style="color: #000066; font-weight: bold;">&lt;/</span>s<span style="color: #000066; font-weight: bold;">:</span>ItemRenderer<span style="color: #000066; font-weight: bold;">&gt;</span></pre></div></div>

<p>There are two states, &ldquo;normal&rdquo; which is the original state  without the &ldquo;Detailed description&rdquo; label, and &ldquo;details&rdquo; which does include this  label. There is also a pair of buttons to allow the user to show and hide the  detailed description label. Only one of those buttons is shown at any given  time based on the value of currentState.</p>
<p>The solution I found was to override the ItemRenderer method  getCurrentRendererState. I also added two states, including the hovered state  which the Flex ItemRenderer base class implements:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>states<span style="color: #000066; font-weight: bold;">&gt;</span>
    <span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>State <span style="color: #004993;">name</span>=<span style="color: #990000;">&quot;normal&quot;</span> <span style="color: #000066; font-weight: bold;">/&gt;</span>
    <span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>State <span style="color: #004993;">name</span>=<span style="color: #990000;">&quot;hovered&quot;</span> <span style="color: #000066; font-weight: bold;">/&gt;</span>
    <span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>State <span style="color: #004993;">name</span>=<span style="color: #990000;">&quot;details&quot;</span> <span style="color: #000066; font-weight: bold;">/&gt;</span>
    <span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>State <span style="color: #004993;">name</span>=<span style="color: #990000;">&quot;hoveredDetails&quot;</span><span style="color: #000066; font-weight: bold;">/&gt;</span>
<span style="color: #000066; font-weight: bold;">&lt;/</span>s<span style="color: #000066; font-weight: bold;">:</span>states<span style="color: #000066; font-weight: bold;">&gt;</span></pre></div></div>

<p>I modified my item renderer to apply the new states  appropriately:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>VGroup paddingTop=<span style="color: #990000;">&quot;10&quot;</span> paddingLeft=<span style="color: #990000;">&quot;10&quot;</span> paddingBottom=<span style="color: #990000;">&quot;10&quot;</span> paddingRight=<span style="color: #990000;">&quot;10&quot;</span><span style="color: #000066; font-weight: bold;">&gt;</span>
    <span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>Label <span style="color: #004993;">text</span>=<span style="color: #990000;">&quot;Always Displayed&quot;</span> includeIn=<span style="color: #990000;">&quot;normal, hovered, details, hoveredDetails&quot;</span><span style="color: #000066; font-weight: bold;">/&gt;</span>
    <span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>Label <span style="color: #004993;">text</span>=<span style="color: #990000;">&quot;{data}&quot;</span> includeIn=<span style="color: #990000;">&quot;normal, hovered, details, hoveredDetails&quot;</span><span style="color: #000066; font-weight: bold;">/&gt;</span>
    <span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>Group<span style="color: #000066; font-weight: bold;">&gt;</span>
        <span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>Button label=<span style="color: #990000;">&quot;Add 2nd label&quot;</span> includeIn=<span style="color: #990000;">&quot;normal, hovered&quot;</span> <span style="color: #004993;">click</span>=<span style="color: #990000;">&quot;currentState='details'&quot;</span> <span style="color: #000066; font-weight: bold;">/&gt;</span>
        <span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>Button label=<span style="color: #990000;">&quot;Remove 2nd label&quot;</span> includeIn=<span style="color: #990000;">&quot;details, hoveredDetails&quot;</span> <span style="color: #004993;">click</span>=<span style="color: #990000;">&quot;currentState='normal'&quot;</span><span style="color: #000066; font-weight: bold;">/&gt;</span>
    <span style="color: #000066; font-weight: bold;">&lt;/</span>s<span style="color: #000066; font-weight: bold;">:</span>Group<span style="color: #000066; font-weight: bold;">&gt;</span>
    <span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>Label <span style="color: #004993;">text</span>=<span style="color: #990000;">&quot;Detailed description&quot;</span> includeIn=<span style="color: #990000;">&quot;details, hoveredDetails&quot;</span><span style="color: #000066; font-weight: bold;">/&gt;</span>
<span style="color: #000066; font-weight: bold;">&lt;/</span>s<span style="color: #000066; font-weight: bold;">:</span>VGroup<span style="color: #000066; font-weight: bold;">&gt;</span></pre></div></div>

<p>And I implemented my override of getCurrentRendererState:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">protected</span> override <span style="color: #339966; font-weight: bold;">function</span> getCurrentRendererState<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #6699cc; font-weight: bold;">var</span> newRenderState<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #0033ff; font-weight: bold;">super</span><span style="color: #000066; font-weight: bold;">.</span>getCurrentRendererState<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>currentState == <span style="color: #990000;">&quot;details&quot;</span> <span style="color: #000066; font-weight: bold;">||</span> currentState == <span style="color: #990000;">&quot;hoveredDetails&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">&amp;&amp;</span> newRenderState == <span style="color: #990000;">&quot;normal&quot;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        newRenderState = <span style="color: #990000;">&quot;details&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>currentState == <span style="color: #990000;">&quot;details&quot;</span> <span style="color: #000066; font-weight: bold;">&amp;&amp;</span> newRenderState == <span style="color: #990000;">&quot;hovered&quot;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        newRenderState = <span style="color: #990000;">&quot;hoveredDetails&quot;</span><span style="color: #000066; font-weight: bold;">;</span> 
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0033ff; font-weight: bold;">return</span> newRenderState<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Here, I force the state to include the details even when the  user mouses out and, in so doing, get the desired behavior.</p>
<p><a href="http://flexperiential.com/code/GetCurrentRendererState/GetCurrentRendererStateExample.fxp">Download sample project (FXP).</a></p>
]]></content:encoded>
			<wfw:commentRss>http://flexperiential.com/2011/03/21/overriding-getcurrentrendererstate-to-control-mouse-over-behavior-in-flex-datagroup/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Easy Type-Safe DataProvider for Flex Lists and DataGrids</title>
		<link>http://flexperiential.com/2011/03/14/easy-type-safe-dataprovider-for-flex-lists-and-datagrids/</link>
		<comments>http://flexperiential.com/2011/03/14/easy-type-safe-dataprovider-for-flex-lists-and-datagrids/#comments</comments>
		<pubDate>Mon, 14 Mar 2011 14:52:32 +0000</pubDate>
		<dc:creator>David Salahi</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[DataGrid]]></category>
		<category><![CDATA[IList]]></category>
		<category><![CDATA[Type Safety]]></category>
		<category><![CDATA[Vector]]></category>

		<guid isPermaLink="false">http://flexperiential.com/?p=811</guid>
		<description><![CDATA[In my past three posts I’ve been evolving a technique for creating a type-safe collection class that can be used a DataProvider for Flex Lists and DataGrids. As I’ve discussed previously, the problem with these controls is that they use the ArrayList or ArrayCollection classes as their data providers. And the problem those collections is [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>In my past three posts I’ve been evolving a technique for  creating a type-safe collection class that can be used a DataProvider for Flex  Lists and DataGrids. As I’ve discussed previously, the problem with these  controls is that they use the ArrayList or ArrayCollection classes as their  data providers. And the problem those collections is that they are not  type-safe. You can put any type of object into an ArrayList or ArrayCollection.  That’s not a problem until you put the wrong type into one. Then, you get an  error at run-time. It would, of course, be far better to catch the error at  compile time. In this post I present the culmination of my technique which  makes it convenient to use a type-safe container with Flex Lists and DataGrids. </p>
<div style="float:right; width:305px; padding-left:15px;">
    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_1" width="300" height="175">
      <param name="movie" value="http://flexperiential.com/code/VectorDataProviderExample3/VectorDataProviderTypeSafe.swf" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://flexperiential.com/code/VectorDataProviderExample3/VectorDataProviderTypeSafe.swf" width="300" height="175">
      <!--<![endif]-->
        
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>

<p style="font-size:small; font-style:italic">This application wraps an instance of Vector.&lt;Person&gt; in a class to provide type-safe access. This class implements IList so that Lists and DataGrids can use it as a DataProvider. Right-click the app to view source or <a href="http://flexperiential.com/code/VectorDataProviderExample3/VectorDataProviderExample3.fxp">download an FXP file here</a>.</p>
</div>
<p> Having honed the technique over the course of four blog  posts I&#8217;m now making available a TypeSafeList abstract class which you can easily  and quickly extend. With only about 20 lines of code you can create a subclass  of TypeSafeList that will contain any single data type of your choice. Any  attempt to insert an object of the wrong type will be caught at compile time. And,  because TypeSafeList implements IList your subclass can still be used as a data  provider for Lists and DataGrids. </p>
<p>If you want to know how the technique works, see my previous  posts:</p>
<ol>
<li><a href="http://flexperiential.com/2011/02/21/type-safe-dataproviders-for-flex-lists-with-the-actionscript-vector/">Type-Safe  DataProviders for Flex Lists with the ActionScript Vector</a></li>
<li><a href="http://flexperiential.com/2011/02/26/an-enhanced-type-safe-data-provider-technique-for-flex-ilist-based-controls/">An  Enhanced Type-Safe Data Provider Technique for Flex IList-Based Controls</a></li>
<li><a href="http://flexperiential.com/2011/03/07/type-safe-dataprovider-for-flex-datagrid-data-loading-encapsulated-wparsley-dynamiccommand/">Type-Safe DataProvider for Flex DataGrid; Data Loading Encapsulated w/Parsley DynamicCommand</a></li>
</ol>
<p>If you just want to use TypeSafeList to quickly create your  own type-safe collection classes you can skip the previous posts and just read  this one.</p>
<p>To use TypeSafeList you need to create a container class  that extends TypeSafeList and has a <a href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Vector.html">Vector</a> as an instance variable. You should, of course, type the Vector to contain the object  type that you’re interested in. For this example, we’re going store Person  objects so we’ll have:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _people<span style="color: #000066; font-weight: bold;">:</span>Vector<span style="color: #000066; font-weight: bold;">.&lt;</span>Person<span style="color: #000066; font-weight: bold;">&gt;;</span></pre></div></div>

<p>Next, you need to define a type-safe interface for accessing your Vector. Here&#8217;s a minimal example:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> interface IPersonVectorDP
<span style="color: #000000;">&#123;</span>
    <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">pop</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span>Person<span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span><span style="color: #000066; font-weight: bold;">:</span>Person<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">uint</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>You can name your interface whatever you like; here, I’ve  used IPersonVectorDP. You will add or remove Person objects to _people only through  this interface. Your interface could have other methods; e.g., insertItemAt or  deleteItemAt (note that you must not use any of the same method names as the IList  methods). Whatever methods you define in your interface will need to be  implemented in your type-safe collection’s implementation class. </p>
<p>Here’s the beginning of an example TypeSafeList subclass  with our _people Vector:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> PersonVectorDP <span style="color: #0033ff; font-weight: bold;">extends</span> TypeSafeList implements IPersonVectorDP
<span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _people<span style="color: #000066; font-weight: bold;">:</span>Vector<span style="color: #000066; font-weight: bold;">.&lt;</span>Person<span style="color: #000066; font-weight: bold;">&gt;;</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> PersonVectorDP<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        _people = <span style="color: #0033ff; font-weight: bold;">new</span> Vector<span style="color: #000066; font-weight: bold;">.&lt;</span>Person<span style="color: #000066; font-weight: bold;">&gt;;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">.</span>
    <span style="color: #000066; font-weight: bold;">.</span>
    <span style="color: #000066; font-weight: bold;">.</span></pre></div></div>

<p>Next, we implement the methods in our IPersonVectorDP  interface that we defined above. Here, we implement  the push and pop methods we declared in IPersonVectorDP:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span><span style="color: #000066; font-weight: bold;">:</span>Person<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">uint</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #6699cc; font-weight: bold;">var</span> count<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = _people<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
    dispatchAddEvent<span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span><span style="color: #000066; font-weight: bold;">,</span> count<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #0033ff; font-weight: bold;">return</span> count<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">pop</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span>Person
<span style="color: #000000;">&#123;</span>
    <span style="color: #6699cc; font-weight: bold;">var</span> poppedPerson<span style="color: #000066; font-weight: bold;">:</span>Person = _people<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">pop</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
    dispatchRemoveEvent<span style="color: #000000;">&#40;</span>poppedPerson<span style="color: #000066; font-weight: bold;">,</span> _people<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #0033ff; font-weight: bold;">return</span> poppedPerson<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>These methods wrap Vector’s push and pop methods. In  addition, they call event dispatchers in the TypeSafeList superclass. These event  dispatchers notify the Flex List/DataGrid controls when the container’s  elements have changed so that the controls can update their views of those  elements.</p>
<p> At this point, you need to write a function to provide  access to your Vector so that TypeSafeList can access elements as needed by  Flex’s List &amp; DataGrid controls (through IList). You have a choice between  a clean, easy approach that can be wasteful of memory and an efficient approach  which requires you to modify a few lines of code in TypeSafeList.</p>
<p>First, here’s the clean, easy approach. You define a  function which returns a reference to your Vector as a Vector of Objects. In  our example, we return a copy of the _people Vector. </p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">override <span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> getVector<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span>Vector<span style="color: #000066; font-weight: bold;">.&lt;</span>Object<span style="color: #000066; font-weight: bold;">&gt;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #6699cc; font-weight: bold;">var</span> vector<span style="color: #000066; font-weight: bold;">:</span>Vector<span style="color: #000066; font-weight: bold;">.&lt;</span>Object<span style="color: #000066; font-weight: bold;">&gt;</span> = Vector<span style="color: #000066; font-weight: bold;">.&lt;</span>Object<span style="color: #000066; font-weight: bold;">&gt;</span><span style="color: #000000;">&#40;</span>_people<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #0033ff; font-weight: bold;">return</span> vector<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This works fine for TypeSafeList because Objects are what  IList uses. IList doesn’t need to know about Persons so neither does  TypeSafeList. </p>
<p>The problem with this technique is that every time an  element is added to your Vector a copy of the entire Vector is made in  getVector. For small collections the  overhead will be negligible. But for collections consisting of thousands of  elements the overhead could be substantial. But without pointers (or pointers  to references) as in C++ there’s nothing that can be done. Except to get in and  muck around in the base class code.</p>
<p> So, here’s the efficient but slightly messy alternative: you  edit a copy of TypeSafeList and modify the existing getVector function to  return a Vector of your type. Here’s the stock getVector function from  TypeSafeList:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> getVector<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span>Vector<span style="color: #000066; font-weight: bold;">.&lt;</span>Object<span style="color: #000066; font-weight: bold;">&gt;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">throw</span> <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">IllegalOperationError</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;Error in TypeSafeList; you must subclass TypeSafeList and override getVector&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>And here’s a modified version:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">override <span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> getVector<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span>Vector<span style="color: #000066; font-weight: bold;">.&lt;</span>Person<span style="color: #000066; font-weight: bold;">&gt;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">throw</span> <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">IllegalOperationError</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;Error in TypeSafeList; you must subclass TypeSafeList and override getVector&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Now, you can modify your override to return a Vector of your  type; e.g.,</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">override <span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> getVector<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span>Vector<span style="color: #000066; font-weight: bold;">.&lt;</span>Person<span style="color: #000066; font-weight: bold;">&gt;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">return</span> _people<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>You also need to make a couple more edits to TypeSafeList.  First, there’s a Vector.&lt;Object&gt; declaration in getItemIndex; just change  it to your type; e.g., Vector.&lt;Person&gt;. Second, there’s a loop in which  an element var is declared:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> element<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Object</span> = vector<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>You’ll also need to change the Object type to your type.</p>
<p>As you can see, there are actually only three lines of code  to modify if you want to go the performance route. Unfortunately, that means  you’ll need to create a separate copy of TypeSafeList for each data type you  want to use it with. But until ActionScript has full support for generics this  is the best we can do. But, at least, we can now have type-safe collections for  use with Flex ListBase-derived controls and DataGrids.</p>
<p> <a href="http://flexperiential.com/code/VectorDataProviderExample3/VectorDataProviderExample3.fxp">Download the example project (FXP).</a></p>
]]></content:encoded>
			<wfw:commentRss>http://flexperiential.com/2011/03/14/easy-type-safe-dataprovider-for-flex-lists-and-datagrids/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Type-Safe DataProvider for Flex DataGrid; Data Loading Encapsulated w/Parsley DynamicCommand</title>
		<link>http://flexperiential.com/2011/03/07/type-safe-dataprovider-for-flex-datagrid-data-loading-encapsulated-wparsley-dynamiccommand/</link>
		<comments>http://flexperiential.com/2011/03/07/type-safe-dataprovider-for-flex-datagrid-data-loading-encapsulated-wparsley-dynamiccommand/#comments</comments>
		<pubDate>Mon, 07 Mar 2011 15:55:55 +0000</pubDate>
		<dc:creator>David Salahi</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[DataGrid]]></category>
		<category><![CDATA[IList]]></category>
		<category><![CDATA[Parsley]]></category>
		<category><![CDATA[Type Safety]]></category>
		<category><![CDATA[Vector]]></category>

		<guid isPermaLink="false">http://flexperiential.com/?p=796</guid>
		<description><![CDATA[In this post I&#8217;m continuing the theme of my last two posts, type-safe DataProviders, and combining it with the theme of the two posts before that: the Parsley application framework. I&#8217;ll be using Parsley’s DynamicCommand object to encapsulate the fetching of data from a server and then store it in a type-safe DataProvider that will [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>In this post I&#8217;m continuing the theme of my last two posts,  type-safe DataProviders, and combining it with the theme of the two posts  before that: the Parsley application framework. I&#8217;ll be using <a href="http://www.spicefactory.org/parsley/docs/2.3/manual/">Parsley’s  DynamicCommand</a> object to encapsulate the fetching of data from a server and  then store it in a type-safe DataProvider that will be used with a DataGrid. If  you haven’t read my previous two posts you’ll need to do that to get the most  out of this post. I’d recommend starting with the first post of the pair, <a href="http://flexperiential.com/2011/02/21/type-safe-dataproviders-for-flex-lists-with-the-actionscript-vector/">Type-Safe  DataProviders for Flex Lists with the ActionScript Vector</a>.</p>
<h2>A Type-Safe DataProvider for the Flex DataGrid</h2>
<p>My first theme, type-safety, addresses the fact that the  built-in Flex types ArrayList and ArrayCollection, which are used as data  providers for List &amp; DataGrid controls, are not type-safe. So, if you intend  to provide a list of Employees as the data source for your DataGrid you have no  way to ensure that some other developer hasn’t accidentally passed you an ArrayList  of Dogs, for example. (Well, you could iterate over the entire collection,  checking the type of each element, before supplying it to your DataGrid. But who  actually does that?) ActionScript does provide a type-safe collection class,  Vector, but it doesn’t work as a List/DataGrid DataProvider. </p>
<div>
    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_2" width="555" height="300">
      <param name="movie" value="http://flexperiential.com/code/ParsleyDynamicCommand/ParsleyDynamicCommand.swf" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://flexperiential.com/code/ParsleyDynamicCommand/ParsleyDynamicCommand.swf" width="555" height="300">
      <!--<![endif]-->
        
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>

<p style="font-size:small; font-style:italic">This application wraps an instance of Vector.&lt;Employee&gt; in a class to provide type-safe access. This class implements IList so that the DataGrid can use it as a DataProvider. And, the app encapsulates retrieval of the data from the server in a ParsleyDynamicCommand. Right-click the app to view source or <a href="http://flexperiential.com/code/ParsleyDynamicCommand/ParsleyDynamicCommand.fxp">download an FXP file here</a>.</p>
</div>
<p> My previous two posts demonstrated a technique with which  you can use the Vector to gain type-safety while still providing the IList  interface that Flex’s list &amp; grid controls require. Since I’ve already covered  that ground I won’t explain how that works here. In this example, the type-safe  collection is called EmployeeDataProvider and it implements two interfaces,  IList and IEmployeeDataProvider. The latter interface is the one which ensures  type-safety:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> interface IEmployeeDataProvider
<span style="color: #000000;">&#123;</span>
    <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">pop</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span>Employee<span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span><span style="color: #000066; font-weight: bold;">:</span>Employee<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">uint</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Only Employee objects can be added to the collection through  this interface. Here’s the beginning of the EmployeeDataProvider class:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> EmployeeDataProvider <span style="color: #0033ff; font-weight: bold;">extends</span> <span style="color: #004993;">EventDispatcher</span> implements IList<span style="color: #000066; font-weight: bold;">,</span> IEmployeeDataProvider
<span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _employees<span style="color: #000066; font-weight: bold;">:</span>Vector<span style="color: #000066; font-weight: bold;">.&lt;</span>Employee<span style="color: #000066; font-weight: bold;">&gt;</span> = <span style="color: #0033ff; font-weight: bold;">new</span> Vector<span style="color: #000066; font-weight: bold;">.&lt;</span>Employee<span style="color: #000066; font-weight: bold;">&gt;;</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> EmployeeDataProvider<span style="color: #000000;">&#40;</span><span style="color: #004993;">target</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">IEventDispatcher</span>=<span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0033ff; font-weight: bold;">super</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">target</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #009900; font-style: italic;">// </span>
    <span style="color: #009900; font-style: italic;">// IEmployeeDataProvider</span>
    <span style="color: #009900; font-style: italic;">//</span>
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">pop</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span>Employee
    <span style="color: #000000;">&#123;</span>
        <span style="color: #6699cc; font-weight: bold;">var</span> poppedEmployee<span style="color: #000066; font-weight: bold;">:</span>Employee = _employees<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">pop</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
        <span style="color: #6699cc; font-weight: bold;">var</span> event<span style="color: #000066; font-weight: bold;">:</span>CollectionEvent =	<span style="color: #0033ff; font-weight: bold;">new</span> CollectionEvent<span style="color: #000000;">&#40;</span>CollectionEvent<span style="color: #000066; font-weight: bold;">.</span>COLLECTION_CHANGE<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
        event<span style="color: #000066; font-weight: bold;">.</span>kind = CollectionEventKind<span style="color: #000066; font-weight: bold;">.</span>REMOVE<span style="color: #000066; font-weight: bold;">;</span>
        event<span style="color: #000066; font-weight: bold;">.</span>items<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>poppedEmployee<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
        event<span style="color: #000066; font-weight: bold;">.</span>location = _employees<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span><span style="color: #000066; font-weight: bold;">;</span>
        <span style="color: #004993;">dispatchEvent</span><span style="color: #000000;">&#40;</span>event<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
        <span style="color: #0033ff; font-weight: bold;">return</span> poppedEmployee<span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span><span style="color: #000066; font-weight: bold;">:</span>Employee<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">uint</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #6699cc; font-weight: bold;">var</span> count<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = _employees<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
        <span style="color: #6699cc; font-weight: bold;">var</span> event<span style="color: #000066; font-weight: bold;">:</span>CollectionEvent =	<span style="color: #0033ff; font-weight: bold;">new</span> CollectionEvent<span style="color: #000000;">&#40;</span>CollectionEvent<span style="color: #000066; font-weight: bold;">.</span>COLLECTION_CHANGE<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
        event<span style="color: #000066; font-weight: bold;">.</span>kind = CollectionEventKind<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ADD</span><span style="color: #000066; font-weight: bold;">;</span>
        event<span style="color: #000066; font-weight: bold;">.</span>items<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
        event<span style="color: #000066; font-weight: bold;">.</span>location = count<span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span>
        <span style="color: #004993;">dispatchEvent</span><span style="color: #000000;">&#40;</span>event<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
        <span style="color: #0033ff; font-weight: bold;">return</span> count<span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #000000;">&#125;</span></pre></div></div>

<p>By using the type-safe container, Vector, we ensure the  type-safety of our container object, EmployeeDataProvider. The two functions,  pop &amp; push, mostly contain code for dispatching events so that the DataGrid  can react to changes in the collection. Other than that, they delegate push  &amp; pop to the Flex Vector.</p>
<p> I&#8217;m not showing the code that implements IList here because I’ve  explained it in my previous posts. (But you can View Source on the SWF above if  you want to see it.) The IList interface is not used by my code. The IList implementation  is provided only so that the DataGrid will be able to use EmployeeDataProvider as  a data source.</p>
<h2>Creating a Flex Data Service to Read XML</h2>
<p>The data used in this example is a simple XML file that is  hosted on my server at <a href="http://flexperiential.com/code/employees.xml">http://flexperiential.com/code/employees.xml</a>.  I created a Flex HTTPService to fetch this data by choosing Data &#8211;&gt; Connect  to Data Service from the Flash Builder menu:</p>
<p><img src="http://flexperiential.com/blog_posts/data-services.gif" height="618"  width="607"/></p>
<p>After creating the service I right-clicked on the new  getEmployees method in the Data/Services window and selected Configure Return  Type. Flash Builder then automatically created value objects to match the data:  Employee objects containing fields for firstname, lastname, title, email, etc.</p>
<h2>Using Parsley DynamicCommand to Fetch Data from the Server</h2>
<p>A common pattern for fetching server data is to encapsulate  the web service in a command object. Parsley provides a convenient  DynamicCommand object which is tailor-made for this purpose. Here’s my class that  will be instantiated using DynamicCommand:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> GetEmployeesCmd <span style="color: #0033ff; font-weight: bold;">extends</span> <span style="color: #004993;">EventDispatcher</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #000000;">&#91;</span>Inject<span style="color: #000000;">&#93;</span> <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> employeeService<span style="color: #000066; font-weight: bold;">:</span>EmployeeServce<span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #000000;">&#91;</span>Inject<span style="color: #000000;">&#93;</span> <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> employeeDataProvider<span style="color: #000066; font-weight: bold;">:</span>EmployeeDataProvider<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> GetEmployeesCmd<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> execute<span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">:</span>EmployeeServiceEvent<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span>AsyncToken
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0033ff; font-weight: bold;">return</span> employeeService<span style="color: #000066; font-weight: bold;">.</span>getEmployees<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> result<span style="color: #000000;">&#40;</span><span style="color: #004993;">data</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Object</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span> i <span style="color: #000066; font-weight: bold;">&lt;</span> <span style="color: #004993;">data</span><span style="color: #000066; font-weight: bold;">.</span>employee<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span><span style="color: #000066; font-weight: bold;">;</span> i<span style="color: #000066; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #6699cc; font-weight: bold;">var</span> employee<span style="color: #000066; font-weight: bold;">:</span>Employee = <span style="color: #004993;">data</span><span style="color: #000066; font-weight: bold;">.</span>employee<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #0033ff; font-weight: bold;">as</span> Employee<span style="color: #000066; font-weight: bold;">;</span>
            employeeDataProvider<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>employee<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">error</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">info</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Object</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;Error fetching employees&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This class contains three methods named so that Parsley will  automatically recognize them and wire them up: execute, result and error. When  you invoke the DynamicCommand Parsley automatically calls the execute method.  And, Parsley automatically calls the result method when the service returns its  data (ResultEvent) or, in case of an error (FaultEvent), the error method.</p>
<p> I&#8217;m also using Parsley here to inject instances of the  EmployeeService and the EmployeeDataProvider.</p>
<h2>Parsley Context/Configuration</h2>
<p>Here’s the Parsley context file that ties all of this  together:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">&lt;</span>models<span style="color: #000066; font-weight: bold;">:</span>EmployeeDataProvider<span style="color: #000066; font-weight: bold;">/&gt;</span>
<span style="color: #000066; font-weight: bold;">&lt;</span>employeeservce<span style="color: #000066; font-weight: bold;">:</span>EmployeeServce<span style="color: #000066; font-weight: bold;">/&gt;</span>
<span style="color: #000066; font-weight: bold;">&lt;</span>spicefactory<span style="color: #000066; font-weight: bold;">:</span>DynamicCommand <span style="color: #004993;">type</span>=<span style="color: #990000;">&quot;{GetEmployeesCmd}&quot;</span> selector=<span style="color: #990000;">&quot;{EmployeeServiceEvent.LOAD_EMPLOYEES_REQUEST}&quot;</span><span style="color: #000066; font-weight: bold;">&gt;</span>
    <span style="color: #000066; font-weight: bold;">&lt;</span>spicefactory<span style="color: #000066; font-weight: bold;">:</span>ManagedEvents <span style="color: #004993;">names</span>=<span style="color: #990000;">&quot;{[EmployeeServiceEvent.LOAD_EMPLOYEES_REQUEST]}&quot;</span><span style="color: #000066; font-weight: bold;">/&gt;</span>
<span style="color: #000066; font-weight: bold;">&lt;/</span>spicefactory<span style="color: #000066; font-weight: bold;">:</span>DynamicCommand<span style="color: #000066; font-weight: bold;">&gt;</span></pre></div></div>

<p>The first two lines specify that the EmployeeDataProvider  and EmployeeService classes will be managed by Parsley. The type parameter shown  in the DynamicCommand refers to the GetEmployeesCmd class discussed above. The  names parameter specifies the event that will cause GetEmployeesCmd.execute to  be invoked.</p>
<h2>Application MXML File </h2>
<p>The application file contains a couple of statements needed  to complete the Parsley configuration:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">&lt;</span>fx<span style="color: #000066; font-weight: bold;">:</span>Declarations<span style="color: #000066; font-weight: bold;">&gt;</span>
    <span style="color: #000066; font-weight: bold;">&lt;</span>parsley<span style="color: #000066; font-weight: bold;">:</span>ContextBuilder config=<span style="color: #990000;">&quot;{ParsleyDynamicCommandContext}&quot;</span>   <span style="color: #000066; font-weight: bold;">/&gt;</span>
    <span style="color: #000066; font-weight: bold;">&lt;</span>parsley<span style="color: #000066; font-weight: bold;">:</span>Configure  <span style="color: #000066; font-weight: bold;">/&gt;</span>
<span style="color: #000066; font-weight: bold;">&lt;/</span>fx<span style="color: #000066; font-weight: bold;">:</span>Declarations<span style="color: #000066; font-weight: bold;">&gt;</span></pre></div></div>

<p>The first line specifies the context file name and the  second line tells Parsley to configure the main app MXML file. </p>
<p> This init function is called after Parsley has finished its object  injection and other work (this is specified by the [Init] meta tag):</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>Init<span style="color: #000000;">&#93;</span>
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">init</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #004993;">dispatchEvent</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> EmployeeServiceEvent<span style="color: #000000;">&#40;</span>EmployeeServiceEvent<span style="color: #000066; font-weight: bold;">.</span>LOAD_EMPLOYEES_REQUEST<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The function simply dispatches the event which causes the  GetEmployeesCmd to be invoked. We also need to tell Parsley to manage the  EmployeeServiceEvent event in the application file:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">&lt;</span>fx<span style="color: #000066; font-weight: bold;">:</span>Metadata<span style="color: #000066; font-weight: bold;">&gt;</span>
    <span style="color: #000000;">&#91;</span>ManagedEvents<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;LoadEmployeesRequest&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #000066; font-weight: bold;">&lt;/</span>fx<span style="color: #000066; font-weight: bold;">:</span>Metadata<span style="color: #000066; font-weight: bold;">&gt;</span></pre></div></div>

<p>After the execute method has called the web service (just an  XML file, here), the result method populates the EmployeeDataProvider instance  which is shared by GetEmployeesCmd and the main application. It’s injected into  the MXML application file like so:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>Bindable<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span>Inject<span style="color: #000000;">&#93;</span> <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> employeeModel<span style="color: #000066; font-weight: bold;">:</span>EmployeeDataProvider<span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>Finally, the app file contains a DataGrid which displays the  employee data:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">&lt;</span>mx<span style="color: #000066; font-weight: bold;">:</span>DataGrid dataProvider=<span style="color: #990000;">&quot;{employeeModel}&quot;</span> <span style="color: #004993;">height</span>=<span style="color: #990000;">&quot;100%&quot;</span> <span style="color: #004993;">width</span>=<span style="color: #990000;">&quot;100%&quot;</span><span style="color: #000066; font-weight: bold;">&gt;</span>
    <span style="color: #000066; font-weight: bold;">&lt;</span>mx<span style="color: #000066; font-weight: bold;">:</span>columns<span style="color: #000066; font-weight: bold;">&gt;</span>
        <span style="color: #000066; font-weight: bold;">&lt;</span>mx<span style="color: #000066; font-weight: bold;">:</span>DataGridColumn dataField=<span style="color: #990000;">&quot;firstname&quot;</span> <span style="color: #004993;">width</span>=<span style="color: #990000;">&quot;75&quot;</span> <span style="color: #000066; font-weight: bold;">/&gt;</span>
        <span style="color: #000066; font-weight: bold;">&lt;</span>mx<span style="color: #000066; font-weight: bold;">:</span>DataGridColumn dataField=<span style="color: #990000;">&quot;lastname&quot;</span> <span style="color: #004993;">width</span>=<span style="color: #990000;">&quot;75&quot;</span> <span style="color: #000066; font-weight: bold;">/&gt;</span>
        <span style="color: #000066; font-weight: bold;">&lt;</span>mx<span style="color: #000066; font-weight: bold;">:</span>DataGridColumn dataField=<span style="color: #990000;">&quot;title&quot;</span> <span style="color: #000066; font-weight: bold;">/&gt;</span>
        <span style="color: #000066; font-weight: bold;">&lt;</span>mx<span style="color: #000066; font-weight: bold;">:</span>DataGridColumn dataField=<span style="color: #990000;">&quot;email&quot;</span> <span style="color: #000066; font-weight: bold;">/&gt;</span>
        <span style="color: #000066; font-weight: bold;">&lt;</span>mx<span style="color: #000066; font-weight: bold;">:</span>DataGridColumn dataField=<span style="color: #990000;">&quot;department&quot;</span> <span style="color: #004993;">width</span>=<span style="color: #990000;">&quot;140&quot;</span> <span style="color: #000066; font-weight: bold;">/&gt;</span>
    <span style="color: #000066; font-weight: bold;">&lt;/</span>mx<span style="color: #000066; font-weight: bold;">:</span>columns<span style="color: #000066; font-weight: bold;">&gt;</span>
<span style="color: #000066; font-weight: bold;">&lt;/</span>mx<span style="color: #000066; font-weight: bold;">:</span>DataGrid<span style="color: #000066; font-weight: bold;">&gt;</span></pre></div></div>

<p><a href="http://flexperiential.com/code/ParsleyDynamicCommand/TypeSafeDataGrid.fxp">Download the Flex project (fxp).</a></p>
<p>Next week I&#8217;ll finish this series of posts with a TypeSafeList abstract class that you can extend to quickly and easily build your own type-safe DataProviders.</p>
]]></content:encoded>
			<wfw:commentRss>http://flexperiential.com/2011/03/07/type-safe-dataprovider-for-flex-datagrid-data-loading-encapsulated-wparsley-dynamiccommand/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>An Enhanced Type-Safe Data Provider Technique for Flex IList-Based Controls</title>
		<link>http://flexperiential.com/2011/02/26/an-enhanced-type-safe-data-provider-technique-for-flex-ilist-based-controls/</link>
		<comments>http://flexperiential.com/2011/02/26/an-enhanced-type-safe-data-provider-technique-for-flex-ilist-based-controls/#comments</comments>
		<pubDate>Sat, 26 Feb 2011 21:08:16 +0000</pubDate>
		<dc:creator>David Salahi</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[IList]]></category>
		<category><![CDATA[Type Safety]]></category>
		<category><![CDATA[Vector]]></category>

		<guid isPermaLink="false">http://flexperiential.com/?p=782</guid>
		<description><![CDATA[Last week I posted an article showing how to create type-safe lists for use as Flex List-based DataProviders. This week I&#8217;m enhancing that example to dispatch events when items are added to or removed from the collection. This will allow Lists to update their displays automatically whenever the contents of the DataProvider change. If you [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>Last week I posted an article  showing <a href="http://flexperiential.com/2011/02/21/type-safe-dataproviders-for-flex-lists-with-the-actionscript-vector/">how to create type-safe lists for use as Flex List-based DataProviders</a>.  This week I&#8217;m enhancing that example to dispatch events when items are added to  or removed from the collection. This will allow Lists to update their displays automatically  whenever the contents of the DataProvider change. If you haven&#8217;t already read my previous post on this topic, you will probably want to do so before continuing with this one. </p>
<div style="float:right; padding-left:15px; width:305px; height:175">
    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_3" width="300" height="170">
      <param name="movie" value="http://flexperiential.com/code/VectorDataProviderExample2/VectorDataProviderTypeSafe.swf" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://flexperiential.com/code/VectorDataProviderExample2/VectorDataProviderTypeSafe.swf" width="300" height="170">
      <!--<![endif]-->
        
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>

<p style="font-size:small; font-style:italic">This application wraps a Vector object in a class to provide type-safe access. This class implements IList so that the List, ComboBox and DropDownList can use it as a DataProvider. Right-click the app to view source or <a href="http://flexperiential.com/code/VectorDataProviderExample2/VectorDataProviderExample2.fxp">download an FXP file here</a>.</p>
</div>
<h2>Improved IList Type-Safety</h2>
<p>But before I get to dispatching events I’d like to point out another improvement I’ve  made to the PersonVectorDP example class which strictly enforces the type-safe  approach. In last week’s post I recommended using only IPersonVectorDP to  provide type-safe access. And I recommended setting the reference to the PersonVectorDP  class to null as soon as possible to prevent accidental access through the IList methods:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">_people = <span style="color: #0033ff; font-weight: bold;">new</span> PersonVectorDP<span style="color: #000066; font-weight: bold;">;</span>
peopleList<span style="color: #000066; font-weight: bold;">.</span>dataProvider = _people<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #009900; font-style: italic;">// Always access PersonVectorDP only through IPersonVectorDP</span>
_peopleInterface = _people <span style="color: #0033ff; font-weight: bold;">as</span> IPersonVectorDP<span style="color: #000066; font-weight: bold;">;</span>
_people = <span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// To be safe, null this out ASAP. That way, you can't inadvertently use the wrong method.</span></pre></div></div>

<p>This approach will work but still requires some diligence on  the part of the developer. It subsequently occurred to me that a better  approach is to enforce modification of the collection through the IPersonVectorDP  interface by throwing an exception if any attempt is made to modify the  collection through the IList methods. So, for example, instead of implementing  addItem by pushing the new Person onto the Vector, we now throw an  IllegalOperationError instead:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">throw</span> <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">IllegalOperationError</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;addItem - no access through IList; use IPersonVectorDP instead&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>I’ve done the same thing for all the other IList methods  which modify the list. Of course, the getItemAt and getItemIndex methods are unchanged.</p>
<p>Now, if you forget to null out the PersonVectorDP reference  it won’t matter. And, maybe you need to keep the reference around in order to assign it as  the DataProvider to some new List later. Now, you can. And you can be certain  that all access to the collection will be type-safe. Best of all, the code is smaller and cleaner!</p>
<h2>Dispatching collectionChange Events</h2>
<p>Continuing with the modified PersonVectorDP class, the next  change I’ve made is to have the class extend EventDispatcher:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> PersonVectorDP <span style="color: #0033ff; font-weight: bold;">extends</span> <span style="color: #004993;">EventDispatcher</span> implements IList<span style="color: #000066; font-weight: bold;">,</span> IPersonVectorDP</pre></div></div>

<p>This allows us to remove all of the event-related stub code that  is present in the previous example (addEventListener, removeEventListener,  dispatchEvent, etc.).  The class still implements  IList &amp; IPersonVectorDP, as before.</p>
<p> We also now declare that the class dispatches the event that  List-based controls listen for:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #000000;">&#91;</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">name</span>=<span style="color: #990000;">&quot;collectionChange&quot;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">type</span>=<span style="color: #990000;">&quot;mx.events.CollectionEvent&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span></pre></div></div>

<p>And we dispatch this event whenever items are added to the  list:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span><span style="color: #000066; font-weight: bold;">:</span>Person<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">uint</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #6699cc; font-weight: bold;">var</span> count<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = _people<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #6699cc; font-weight: bold;">var</span> event<span style="color: #000066; font-weight: bold;">:</span>CollectionEvent =	<span style="color: #0033ff; font-weight: bold;">new</span> CollectionEvent<span style="color: #000000;">&#40;</span>CollectionEvent<span style="color: #000066; font-weight: bold;">.</span>COLLECTION_CHANGE<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
    event<span style="color: #000066; font-weight: bold;">.</span>kind = CollectionEventKind<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ADD</span><span style="color: #000066; font-weight: bold;">;</span>
    event<span style="color: #000066; font-weight: bold;">.</span>items<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
    event<span style="color: #000066; font-weight: bold;">.</span>location = count<span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #004993;">dispatchEvent</span><span style="color: #000000;">&#40;</span>event<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #0033ff; font-weight: bold;">return</span> count<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>and whenever items are removed from the list:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">pop</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span>Person
<span style="color: #000000;">&#123;</span>
    <span style="color: #6699cc; font-weight: bold;">var</span> poppedPerson<span style="color: #000066; font-weight: bold;">:</span>Person = _people<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">pop</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #6699cc; font-weight: bold;">var</span> event<span style="color: #000066; font-weight: bold;">:</span>CollectionEvent =	<span style="color: #0033ff; font-weight: bold;">new</span> CollectionEvent<span style="color: #000000;">&#40;</span>CollectionEvent<span style="color: #000066; font-weight: bold;">.</span>COLLECTION_CHANGE<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
    event<span style="color: #000066; font-weight: bold;">.</span>kind = CollectionEventKind<span style="color: #000066; font-weight: bold;">.</span>REMOVE<span style="color: #000066; font-weight: bold;">;</span>
    event<span style="color: #000066; font-weight: bold;">.</span>items<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>poppedPerson<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
    event<span style="color: #000066; font-weight: bold;">.</span>location = _people<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #004993;">dispatchEvent</span><span style="color: #000000;">&#40;</span>event<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #0033ff; font-weight: bold;">return</span> poppedPerson<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>That’s all the changes that are needed to PersonVectorDP. You could, of course, add accessor methods to the IPersonVectorDP for more functionality; e.g., type-safe equivalents to addItemAt or itemUpdated.</p>
<h2>Demo Code to Use the Modified PersonVectorDP Class</h2>
<p>Next, I’ve made a few changes to the test app, VectorDataProviderTypeSafe.mxml.  I’ve added a couple of input fields and a button to allow adding more people.  And, I’ve added a button to remove people:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>TextInput id=<span style="color: #990000;">&quot;newPersonFirst&quot;</span> <span style="color: #000066; font-weight: bold;">/&gt;</span>
<span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>TextInput id=<span style="color: #990000;">&quot;newPersonLast&quot;</span> <span style="color: #000066; font-weight: bold;">/&gt;</span>
<span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>Button id=<span style="color: #990000;">&quot;addPerson&quot;</span> label=<span style="color: #990000;">&quot;Add Person&quot;</span> <span style="color: #004993;">click</span>=<span style="color: #990000;">&quot;addPerson_clickHandler(event)&quot;</span> <span style="color: #000066; font-weight: bold;">/&gt;</span>
<span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>Button id=<span style="color: #990000;">&quot;removePerson&quot;</span> label=<span style="color: #990000;">&quot;Remove Person&quot;</span> <span style="color: #004993;">click</span>=<span style="color: #990000;">&quot;removePerson_clickHandler(event)&quot;</span> <span style="color: #000066; font-weight: bold;">/&gt;</span></pre></div></div>

<p>This button click listener code is straightforward; here’s  the add event listener:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> addPerson_clickHandler<span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #6699cc; font-weight: bold;">var</span> person<span style="color: #000066; font-weight: bold;">:</span>Person = <span style="color: #0033ff; font-weight: bold;">new</span> Person<span style="color: #000066; font-weight: bold;">;</span>
    person<span style="color: #000066; font-weight: bold;">.</span>firstName = newPersonFirst<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span><span style="color: #000066; font-weight: bold;">;</span>
    person<span style="color: #000066; font-weight: bold;">.</span>lastName = newPersonLast<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span><span style="color: #000066; font-weight: bold;">;</span>
    _peopleInterface<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>person<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>And here’s the remove event listener:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> removePerson_clickHandler<span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
    _peopleInterface<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">pop</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>I hope this technique works to help you write more bug-free  code!</p>
<p><a href="http://flexperiential.com/code/VectorDataProviderExample2/VectorDataProviderExample2.fxp">Download the Flex project (FXP).</a></p>
]]></content:encoded>
			<wfw:commentRss>http://flexperiential.com/2011/02/26/an-enhanced-type-safe-data-provider-technique-for-flex-ilist-based-controls/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Type-Safe DataProviders for Flex Lists with the ActionScript Vector</title>
		<link>http://flexperiential.com/2011/02/21/type-safe-dataproviders-for-flex-lists-with-the-actionscript-vector/</link>
		<comments>http://flexperiential.com/2011/02/21/type-safe-dataproviders-for-flex-lists-with-the-actionscript-vector/#comments</comments>
		<pubDate>Mon, 21 Feb 2011 08:00:53 +0000</pubDate>
		<dc:creator>David Salahi</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[IList]]></category>
		<category><![CDATA[Type Safety]]></category>
		<category><![CDATA[Vector]]></category>

		<guid isPermaLink="false">http://flexperiential.com/?p=756</guid>
		<description><![CDATA[Update, 2/25/11: I&#8217;ve updated the code to provide both complete type-safety and event dispatching so that Lists will update their views automatically when the list contents are changed. If you&#8217;re interested in this topic, you will probably still want to start by reading this post. But be sure to continue with my follow-up post An [...]]]></description>
			<content:encoded><![CDATA[<p></p><p style="font-weight:bold;">Update, 2/25/11: I&#8217;ve updated the code to provide both complete type-safety and event dispatching so that Lists will update their views automatically when the list contents are changed. If you&#8217;re interested in this topic, you will probably still want to start by reading this post. But be sure to continue with my follow-up post <a href="http://flexperiential.com/2011/02/26/an-enhanced-type-safe-data-provider-technique-for-flex-ilist-based-controls/">An Enhanced Type-Safe Data Provider Technique for Flex IList-Based Controls </a>to get the new, improved code.</p>
<p>Coming to Flex from a primarily C++/C# background I have  always been a bit dismayed by the loose typing that is possible with  ActionScript. Strict typing is an inherent advantage of languages like C++, C#  and Java because it allows the compiler to catch incorrect-type errors at  compile-time and this leads to greater developer productivity. Any time you can  catch an error at compile-time instead of at run-time you’re multiplying your  productivity.</p>
<p> With ActionScript 3 you do have the ability to require  strict typing for most of your objects but there are some areas in which Flex  still falls down. One of these in its List-based components and its DataGrids.  Data providers for these objects are typically ArrayLists or ArrayCollections  of generic Objects. Unfortunately, this means there is no way to do  type-checking of the objects added to these containers. You may intend to have  an ArrayCollection of Dog objects but there’s nothing to prevent you from  slipping a couple of Cats in there. Imagine the mayhem that will ensue!</p>
<h2>The ActionScript Vector—a (mostly) Type-Safe Container Class</h2>
<p>ActionScript does have a container class, the Vector, which  allows for type checking. Unfortunately, it’s little-used and that’s too bad.  Part of the reason is probably because it cannot be used with Flex’s list and  grid components. But I encourage you to use it whenever possible—which includes  any time you might use a regular Array. And, in fact, it is possible to use it  for List-based components by simply implementing the IList interface. I&#8217;ll show  an example of that shortly.</p>
<p>But, first, I want to back up and demonstrate the basics of  the Vector class. Any time you have a collection of things of a single type you  can declare a Vector of that type. So, to keep Cats out of my collection of  Dogs, I could do the following:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> dogs<span style="color: #000066; font-weight: bold;">:</span>Vector<span style="color: #000066; font-weight: bold;">.&lt;</span>Dog<span style="color: #000066; font-weight: bold;">&gt;</span> = <span style="color: #0033ff; font-weight: bold;">new</span> Vector<span style="color: #000066; font-weight: bold;">.&lt;</span>Dog<span style="color: #000066; font-weight: bold;">&gt;;</span></pre></div></div>

<p>The dot-notation will probably look a little odd the first  time you see it but this is how you tell ActionScript that you want an array (speaking  generically, here) consisting of only Dog objects.  To add Dog instances to the Vector just do  the following:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> dog1<span style="color: #000066; font-weight: bold;">:</span>Dog = <span style="color: #0033ff; font-weight: bold;">new</span> Dog<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
dogs<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>dog1<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>And if BorderCollie extends Dog you can, of course, also do:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> dog2<span style="color: #000066; font-weight: bold;">:</span>BorderCollie = <span style="color: #0033ff; font-weight: bold;">new</span> BorderCollie<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
dogs<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>dog2<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>But if you try this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> cat<span style="color: #000066; font-weight: bold;">:</span>Cat = <span style="color: #0033ff; font-weight: bold;">new</span> Cat<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
dogs<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>cat<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>you  will get an error at run-time. It’s nice to get the run-time error but it’s  unfortunate that the Flex compiler doesn’t detect this at compile time. <a href="http://help.adobe.com/en_US/FlashPlatform//reference/actionscript/3/Vector.html#push%28%29" target="_blank">The  docs</a> say the reason is because push() can accept multiple arguments.  Because of the way ActionScript handles functions with an indeterminate number  of arguments it’s just not possible to do type-checking at compile-time.</p>
<p>But you will, at least,  get a run-time error as soon as you attempt to  push the Cat in there amongst all those Dogs. That’s better than adding a Cat  to your Dogs collection and not finding out about the error until sometime  later. If you tried this with an ArrayList or ArrayCollection it would, of  course, work and then when the inevitable cat-and-dog-fight ensues you’d have  to backtrack to find out who let the Cat in. </p>
<p> Next, I&#8217;ll show how to wrap a Vector in a class so that it  can be used as a DataProvider. And, later I’ve got a solution which even allows  for compile-time type checking with the Vector. More on that below.</p>
<h2>Flex Lists with Vectors as  DataProviders</h2>
<div style="float:right; padding-left:15px; width:155px; height:175">
    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_4" width="150" height="170">
      <param name="movie" value="http://flexperiential.com/code/VectorDataProviderExample/VectorDataProviderExample.swf" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://flexperiential.com/code/VectorDataProviderExample/VectorDataProviderExample.swf" width="150" height="170">
      <!--<![endif]-->
        
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>

<p style="font-size:small; font-style:italic">This application wraps a Vector object in a class to provide type-safe access. This class implements IList so that the List, ComboBox and DropDownList can use it as a DataProvider. Right-click the app to view source or <a href="http://flexperiential.com/code/VectorDataProviderExample/VectorDataProviderExample.fxp">download an FXP file here</a>.</p>
</div>
<p>As  I mentioned earlier, one downside of Vectors is that you cannot use them with  List-based components like Spark Lists, ComboBoxes, DropDownLists and ButtonBars—at least,  not directly. You can, however, adapt Vectors so that they can be used with  these Flex components. These components require, as data providers, an object  which implements IList. ArrayList and ArrayCollection both implement IList which  is why they work as data providers. </p>
<p> The basics of implementing IList are straightforward. The  interface consists primarily of methods like addItem, addItemAt, getItem and  removeItem. So, you can create a class which implements IList and include a  Vector.&lt;YourType&gt; as a member variable. Then, in the implementation of  each of these setter and getter methods just do the appropriate action on your  Vector instance. That’s it! You can then use your Vector wrapper with a List,  ComboBox or DropDownList. </p>
<p>If  you want a full-featured IList implementation you’ll need to do some extra work  like calling listeners when data items change. But for many purposes you won’t  need to bother with these extra features. Here’s a simple implementation of  IList which wraps a Vector.&lt;Person&gt; instance. Here’s the Person class:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Person
<span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> firstName<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> lastName<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Person<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">toString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0033ff; font-weight: bold;">return</span> firstName <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #990000;">&quot; &quot;</span> <span style="color: #000066; font-weight: bold;">+</span> lastName<span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Here’s the PersonVectorDP class with the (partial) IList  implementation:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> PersonVectorDP implements IList
<span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _people<span style="color: #000066; font-weight: bold;">:</span>Vector<span style="color: #000066; font-weight: bold;">.&lt;</span>Person<span style="color: #000066; font-weight: bold;">&gt;</span> = <span style="color: #0033ff; font-weight: bold;">new</span> Vector<span style="color: #000066; font-weight: bold;">.&lt;</span>Person<span style="color: #000066; font-weight: bold;">&gt;;</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> PersonVectorDP<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #009900; font-style: italic;">//</span>
    <span style="color: #009900; font-style: italic;">// IList</span>
    <span style="color: #009900; font-style: italic;">//</span>
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #0033ff; font-weight: bold;">get</span> <span style="color: #004993;">length</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0033ff; font-weight: bold;">return</span> _people<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> addItem<span style="color: #000000;">&#40;</span>item<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Object</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
    <span style="color: #000000;">&#123;</span>
        _people<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>item<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> addItemAt<span style="color: #000000;">&#40;</span>item<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Object</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">index</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
    <span style="color: #000000;">&#123;</span>
        _people<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>item<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// TODO: insert at specified index; throw RangeError if index is out of range</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> getItemAt<span style="color: #000000;">&#40;</span><span style="color: #004993;">index</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span><span style="color: #000066; font-weight: bold;">,</span> prefetch<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span>=<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Object</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0033ff; font-weight: bold;">return</span> _people<span style="color: #000000;">&#91;</span><span style="color: #004993;">index</span><span style="color: #000000;">&#93;</span> <span style="color: #0033ff; font-weight: bold;">as</span> <span style="color: #004993;">Object</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> getItemIndex<span style="color: #000000;">&#40;</span>item<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Object</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span> i <span style="color: #000066; font-weight: bold;">&lt;</span> _people<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span><span style="color: #000066; font-weight: bold;">;</span> i<span style="color: #000066; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #6699cc; font-weight: bold;">var</span> element<span style="color: #000066; font-weight: bold;">:</span>Person = _people<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #0033ff; font-weight: bold;">as</span> Person<span style="color: #000066; font-weight: bold;">;</span>
            <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>element == item<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0033ff; font-weight: bold;">return</span> i<span style="color: #000066; font-weight: bold;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
        <span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> itemUpdated<span style="color: #000000;">&#40;</span>item<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Object</span><span style="color: #000066; font-weight: bold;">,</span> property<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Object</span>=<span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000066; font-weight: bold;">,</span> oldValue<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Object</span>=<span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000066; font-weight: bold;">,</span> newValue<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Object</span>=<span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #009900; font-style: italic;">//TODO: implement function</span>
        <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;itemUpdated NOT IMPLEMENTED&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> removeAll<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
    <span style="color: #000000;">&#123;</span>
        _people<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">splice</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span> _people<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> removeItemAt<span style="color: #000000;">&#40;</span><span style="color: #004993;">index</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Object</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #6699cc; font-weight: bold;">var</span> removedStrings<span style="color: #000066; font-weight: bold;">:</span>Vector<span style="color: #000066; font-weight: bold;">.&lt;</span>Person<span style="color: #000066; font-weight: bold;">&gt;</span> = _people<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">splice</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">index</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
        <span style="color: #0033ff; font-weight: bold;">return</span> removedStrings<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> setItemAt<span style="color: #000000;">&#40;</span>item<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Object</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">index</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Object</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">index</span> <span style="color: #000066; font-weight: bold;">&lt;</span> <span style="color: #000000; font-weight:bold;">0</span> <span style="color: #000066; font-weight: bold;">||</span> <span style="color: #004993;">index</span> <span style="color: #000066; font-weight: bold;">&gt;</span>= _people<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0033ff; font-weight: bold;">throw</span> <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">RangeError</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;setItemAt index: &quot;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #004993;">index</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #990000;">&quot; is out of range&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
        <span style="color: #000000;">&#125;</span>
        _people<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">splice</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">index</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span> item<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
        <span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #000000;">&#125;</span></pre></div></div>

<p>And here it is in use:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>List id=<span style="color: #990000;">&quot;list&quot;</span><span style="color: #000066; font-weight: bold;">/&gt;</span>
<span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>ComboBox id=<span style="color: #990000;">&quot;combo&quot;</span><span style="color: #000066; font-weight: bold;">/&gt;</span>
<span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>DropDownList id=<span style="color: #990000;">&quot;drop&quot;</span><span style="color: #000066; font-weight: bold;">/&gt;</span>
&nbsp;
<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> createPersonVectorDP<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #6699cc; font-weight: bold;">var</span> people<span style="color: #000066; font-weight: bold;">:</span>PersonVectorDP = <span style="color: #0033ff; font-weight: bold;">new</span> PersonVectorDP<span style="color: #000066; font-weight: bold;">;</span>
    list<span style="color: #000066; font-weight: bold;">.</span>dataProvider = people<span style="color: #000066; font-weight: bold;">;</span>
    combo<span style="color: #000066; font-weight: bold;">.</span>dataProvider = people<span style="color: #000066; font-weight: bold;">;</span>
    drop<span style="color: #000066; font-weight: bold;">.</span>dataProvider = people<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
    <span style="color: #6699cc; font-weight: bold;">var</span> person1<span style="color: #000066; font-weight: bold;">:</span>Person = <span style="color: #0033ff; font-weight: bold;">new</span> Person<span style="color: #000066; font-weight: bold;">;</span>
    person1<span style="color: #000066; font-weight: bold;">.</span>firstName = <span style="color: #990000;">&quot;Don&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
    person1<span style="color: #000066; font-weight: bold;">.</span>lastName = <span style="color: #990000;">&quot;Draper&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
    people<span style="color: #000066; font-weight: bold;">.</span>addItem<span style="color: #000000;">&#40;</span>person1<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
    <span style="color: #6699cc; font-weight: bold;">var</span> person2<span style="color: #000066; font-weight: bold;">:</span>Person = <span style="color: #0033ff; font-weight: bold;">new</span> Person<span style="color: #000066; font-weight: bold;">;</span>
    person2<span style="color: #000066; font-weight: bold;">.</span>firstName = <span style="color: #990000;">&quot;Betty&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
    person2<span style="color: #000066; font-weight: bold;">.</span>lastName = <span style="color: #990000;">&quot;Draper&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
    people<span style="color: #000066; font-weight: bold;">.</span>addItem<span style="color: #000000;">&#40;</span>person2<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
    <span style="color: #6699cc; font-weight: bold;">var</span> person3<span style="color: #000066; font-weight: bold;">:</span>Person = <span style="color: #0033ff; font-weight: bold;">new</span> Person<span style="color: #000066; font-weight: bold;">;</span>
    person3<span style="color: #000066; font-weight: bold;">.</span>firstName = <span style="color: #990000;">&quot;Roger&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
    person3<span style="color: #000066; font-weight: bold;">.</span>lastName = <span style="color: #990000;">&quot;Sterling&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
    people<span style="color: #000066; font-weight: bold;">.</span>addItem<span style="color: #000000;">&#40;</span>person3<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
    <span style="color: #6699cc; font-weight: bold;">var</span> person4<span style="color: #000066; font-weight: bold;">:</span>Person = <span style="color: #0033ff; font-weight: bold;">new</span> Person<span style="color: #000066; font-weight: bold;">;</span>
    person4<span style="color: #000066; font-weight: bold;">.</span>firstName = <span style="color: #990000;">&quot;Peggy&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
    person4<span style="color: #000066; font-weight: bold;">.</span>lastName = <span style="color: #990000;">&quot;Olson&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
    people<span style="color: #000066; font-weight: bold;">.</span>addItem<span style="color: #000000;">&#40;</span>person4<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p><a href="#sample">Download the sample code at the end of this post.</a></p>
<h2>Making the DataProvider Type-Safe</h2>
<p>Although this does work there’s a problem with using the  IList interface to add elements to the Vector DataProvider. The problem is that  those IList methods accept Objects. So, although we were only inserting Persons  into our PersonVectorDP container the compiler wouldn’t stop us from inserting  Dogs or Cats:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">d</span><span style="color: #000066; font-weight: bold;">:</span>Dog = <span style="color: #0033ff; font-weight: bold;">new</span> Dog<span style="color: #000066; font-weight: bold;">;</span>
people<span style="color: #000066; font-weight: bold;">.</span>addItem<span style="color: #000000;">&#40;</span><span style="color: #004993;">d</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// compiles, but throws error at run-time</span></pre></div></div>

<p> We would get an error at run-time but it sure would be nice to get the error at compile-time instead. Fortunately, there’s a solution:  never use the IList interface in your own  code. Instead, create your own type-safe interface and use it as a proxy to access  the Vector object. We can do this with the following simple interface:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> interface IPersonVectorDP
<span style="color: #000000;">&#123;</span>
    <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">pop</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span>Person<span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span><span style="color: #000066; font-weight: bold;">:</span>Person<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">uint</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>We’ll implement this interface, in addition to IList, in  PersonVectorDP:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">pop</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span>Person
<span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">return</span> _people<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">pop</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span><span style="color: #000066; font-weight: bold;">:</span>Person<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">uint</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">return</span> _people<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Now, we can add and remove elements to _people in a  completely type-safe manner. Any attempt to push a Dog in where a Person  belongs will be rejected by the compiler.</p>
<p> If you want a more robust set of accessor methods you’re  free to add them. As long as you use only these strongly-typed methods you get  the best of both worlds: List-compatible collections <strong>and</strong> type-safety. Just make sure never to use the IList accessor  methods. To avoid this mistake you should always access the only through the type-safe interface, never through the Vector wrapper:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> people<span style="color: #000066; font-weight: bold;">:</span>PersonVectorDP = <span style="color: #0033ff; font-weight: bold;">new</span> PersonVectorDP<span style="color: #000066; font-weight: bold;">;</span>
list<span style="color: #000066; font-weight: bold;">.</span>dataProvider = people<span style="color: #000066; font-weight: bold;">;</span>
combo<span style="color: #000066; font-weight: bold;">.</span>dataProvider = people<span style="color: #000066; font-weight: bold;">;</span>
drop<span style="color: #000066; font-weight: bold;">.</span>dataProvider = people<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #009900; font-style: italic;">// Always use this interface to access the personVectorDP instance:</span>
<span style="color: #6699cc; font-weight: bold;">var</span> peopleInterface<span style="color: #000066; font-weight: bold;">:</span>IPersonVectorDP = people <span style="color: #0033ff; font-weight: bold;">as</span> IPersonVectorDP<span style="color: #000066; font-weight: bold;">;</span> 
people = <span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// To be safe, null this out ASAP. That way, you can't inadvertently use the wrong method.</span>
<span style="color: #6699cc; font-weight: bold;">var</span> person1<span style="color: #000066; font-weight: bold;">:</span>Person = <span style="color: #0033ff; font-weight: bold;">new</span> Person<span style="color: #000066; font-weight: bold;">;</span>
person1<span style="color: #000066; font-weight: bold;">.</span>firstName = <span style="color: #990000;">&quot;Don&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
person1<span style="color: #000066; font-weight: bold;">.</span>lastName = <span style="color: #990000;">&quot;Draper&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
peopleInterface<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>person1<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #6699cc; font-weight: bold;">var</span> person2<span style="color: #000066; font-weight: bold;">:</span>Person = <span style="color: #0033ff; font-weight: bold;">new</span> Person<span style="color: #000066; font-weight: bold;">;</span>
person2<span style="color: #000066; font-weight: bold;">.</span>firstName = <span style="color: #990000;">&quot;Betty&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
person2<span style="color: #000066; font-weight: bold;">.</span>lastName = <span style="color: #990000;">&quot;Draper&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
peopleInterface<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>person2<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #6699cc; font-weight: bold;">var</span> person3<span style="color: #000066; font-weight: bold;">:</span>Person = <span style="color: #0033ff; font-weight: bold;">new</span> Person<span style="color: #000066; font-weight: bold;">;</span>
person3<span style="color: #000066; font-weight: bold;">.</span>firstName = <span style="color: #990000;">&quot;Roger&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
person3<span style="color: #000066; font-weight: bold;">.</span>lastName = <span style="color: #990000;">&quot;Sterling&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
peopleInterface<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>person3<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #6699cc; font-weight: bold;">var</span> person4<span style="color: #000066; font-weight: bold;">:</span>Person = <span style="color: #0033ff; font-weight: bold;">new</span> Person<span style="color: #000066; font-weight: bold;">;</span>
person4<span style="color: #000066; font-weight: bold;">.</span>firstName = <span style="color: #990000;">&quot;Peggy&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
person4<span style="color: #000066; font-weight: bold;">.</span>lastName = <span style="color: #990000;">&quot;Olson&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
peopleInterface<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>person4<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #009900; font-style: italic;">// Never access people directly. Always use the interface as shown above.</span>
<span style="color: #009900; font-style: italic;">// people.addItem(new Dog()); // This will fail at run-time since people is now null.</span></pre></div></div>

<p><a name="sample" id="sample"></a> <a href="http://flexperiential.com/code/VectorDataProviderExample/VectorDataProviderExample.fxp">Download the sample code (FXP)</a></p>
<p>In an upcoming post, I&#8217;ll show an implementation of PersonVectorDP which dispatches events so that the Flex List classes will reflect changes to the data source. <span style="font-weight:bold">I&#8217;ve written an updated post, <a href="http://flexperiential.com/2011/02/26/an-enhanced-type-safe-data-provider-technique-for-flex-ilist-based-controls/">An Enhanced Type-Safe Data Provider Technique for Flex IList-Based Controls,</a> which includes a new, enhanced version of the code. Please get that version of the code instead.</span></p>
<h2>Brain Teaser</h2>
<p>Substitute a StringVectorDP class for the PersonVectorDP  class above. This class is the same as the PersonVectorDP class except that its field variable is a Vector of Strings. And, of course, all the IList methods operate on this _strings var. </p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _strings<span style="color: #000066; font-weight: bold;">:</span>Vector<span style="color: #000066; font-weight: bold;">.&lt;</span>String<span style="color: #000066; font-weight: bold;">&gt;</span> = <span style="color: #0033ff; font-weight: bold;">new</span> Vector<span style="color: #000066; font-weight: bold;">.&lt;</span>String<span style="color: #000066; font-weight: bold;">&gt;;</span></pre></div></div>

<p>Consider the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> vectorDP<span style="color: #000066; font-weight: bold;">:</span>StringVectorDP = <span style="color: #0033ff; font-weight: bold;">new</span> StringVectorDP<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">d</span><span style="color: #000066; font-weight: bold;">:</span>Dog = <span style="color: #0033ff; font-weight: bold;">new</span> Dog<span style="color: #000066; font-weight: bold;">;</span>
vectorDP<span style="color: #000066; font-weight: bold;">.</span>addItem<span style="color: #000000;">&#40;</span><span style="color: #004993;">d</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// No error either at compile-time or run-time</span></pre></div></div>

<p>No error  occurs when adding the Dog object either at compile-time or run-time. Why not? <span onclick="showAnswer()" style="text-decoration:underline">Click to see answer</span></p>
<p><script type="text/javascript">
function showAnswer()
{
	$("#answer").css("visibility","visible");
}
 </script></p>
<p id="answer" style="visibility:hidden">It works because the data type of the objects in the _strings Vector is String. All ActionScript objects have an implicit toString() function. So, when addItem calls Vector.push() the Dog object is converted into a String before getting stored in the Vector. One more reason to use the type-safe interface technique described above!</p>
]]></content:encoded>
			<wfw:commentRss>http://flexperiential.com/2011/02/21/type-safe-dataproviders-for-flex-lists-with-the-actionscript-vector/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Menu Tutorial Using the Flex Parsley Application Framework</title>
		<link>http://flexperiential.com/2011/02/07/menu-tutorial-using-the-flex-parsley-application-framework/</link>
		<comments>http://flexperiential.com/2011/02/07/menu-tutorial-using-the-flex-parsley-application-framework/#comments</comments>
		<pubDate>Mon, 07 Feb 2011 09:53:07 +0000</pubDate>
		<dc:creator>David Salahi</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[Parsley]]></category>

		<guid isPermaLink="false">http://flexperiential.com/?p=697</guid>
		<description><![CDATA[Update 2/12/2011: Based on feedback from Jens, the creator of the Parsley framework, I&#8217;ve updated this post and the downloadable sample code. Last year I posted an example of a Flex sliding menu component along with a simple project that shows its use. Here, I present a variation of the menu using the Parsley application [...]]]></description>
			<content:encoded><![CDATA[<p></p><p style="font-weight:bold">Update 2/12/2011: Based on feedback from Jens, the creator of the Parsley framework, I&#8217;ve updated this post and the downloadable sample code.</p>
<p>Last year I posted an example of a Flex <a href="http://flexperiential.com/2010/07/28/flex-4-vertical-sliding-menu/">sliding menu  component</a> along with a simple project that shows its use. Here, I present a variation of the menu using the <a href="http://www.spicefactory.org/parsley/">Parsley application framework</a>.  Parsley is a popular Flex framework which is used for dependency injection,  decoupled messaging and much more. Dependency injection allows you to build  applications which are more loosely coupled. This makes your applications  easier to modify and test.
</p>
<p>Similarly, decoupled messaging gives you more flexibility  than the standard Flex event mechanism. With Flex events, in order to add an  event listener you have to have a reference to the object you want to listen  to. This causes closer coupling which, again, makes applications less resilient  to changes. With Parsley messaging any object can listen for any event whether  or not the receiver has a reference to the sender. Parsley messaging also provides  additional flexibility in the ability to send any object as a message payload.  With Flex events you can only send an Event or an Event subclass. There are additional  benefits to Parsley’s messaging model, such as message selectors, which you can  read about on the Parsley website.</p>
<h2>Parsley Menu Example</h2>
<p>This example is very similar to my original example. The  only differences are in the sending and receiving of the MenuItemSelectedEvent  and, of course, the configuration of Parsley. For this simple example the  amount of code needed to set up Parsley is minimal. To get started we just need  to add a pair of SWC library files to the Flex Build Path: parsley-flex4-2.3.0.swc  and spicelib-flex-2.3.0.swc. Both are included in the Parsley distribution.</p>
<p> In the menu component, VerticalSlidingMenu.mxml, a  MenuItemSelectedEvent is constructed as before. This event contains the id of  the menu item which is clicked. The difference is that, instead of dispatching  an event, we use the Parsley messaging mechanism to send a message. In this  case, we call our message sender sendMessage although we could call it anything  we want. What’s interesting here is that we don’t actually define a sendMessage  function, per se. Instead, we just declare it using the Parsley  MessageDispatcher meta tag:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>MessageDispatcher<span style="color: #000000;">&#93;</span>
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> sendMessage<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Function</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> menuClickHandler<span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> menuEvent<span style="color: #000066; font-weight: bold;">:</span>MenuItemSelectedEvent = <span style="color: #0033ff; font-weight: bold;">new</span> MenuItemSelectedEvent<span style="color: #000000;">&#40;</span>MenuItemSelectedEvent<span style="color: #000066; font-weight: bold;">.</span>MENU_ITEM_SELECTED<span style="color: #000066; font-weight: bold;">,</span> event<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">target</span><span style="color: #000066; font-weight: bold;">.</span>id<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	sendMessage<span style="color: #000000;">&#40;</span>menuEvent<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Parsley provides the function definition.</p>
<p> To receive the event we declare a MessageHandler in MenuParsley.mxml, the main application file:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>MessageHandler<span style="color: #000000;">&#93;</span>
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> messageHandler <span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">:</span>MenuItemSelectedEvent<span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> 
<span style="color: #000000;">&#123;</span>
	_menuItemClicked = event<span style="color: #000066; font-weight: bold;">.</span>menuItem<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This is equivalent to a Flex event listener <strong>except </strong>that we don’t use  addEventListener to link it to the menu component. Parsley takes care of connecting  the sender and receiver.</p>
<p>Parsley configuration for this simple example consists of only two statements. First, in MenuParsley.mxml we just need a ContextBuilder tag. The  following tag is in the Declarations section:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">&lt;</span>parsley<span style="color: #000066; font-weight: bold;">:</span>ContextBuilder  <span style="color: #000066; font-weight: bold;">/&gt;</span></pre></div></div>

<p>Second, we just have to add a &lt;parsley:Configure/&gt;  tag to the declarations section of VerticalSlidingMenu.mxml. That’s it!</p>
<p><a href="http://flexperiential.com/code/MenuParsley/MenuParsley.fxp">Download the sample application (FXP)</a>. For more info on using Parsley see <a href="http://artinflex.blogspot.com/2010/09/quick-dive-into-parsley-intro-why.html">Art&#8217;s Flex Notes</a> series of posts. </p>
]]></content:encoded>
			<wfw:commentRss>http://flexperiential.com/2011/02/07/menu-tutorial-using-the-flex-parsley-application-framework/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Flex 4 Version of Art&#8217;s Flex Notes Project</title>
		<link>http://flexperiential.com/2011/02/05/flex-4-version-of-arts-flex-notes-project/</link>
		<comments>http://flexperiential.com/2011/02/05/flex-4-version-of-arts-flex-notes-project/#comments</comments>
		<pubDate>Sat, 05 Feb 2011 23:08:10 +0000</pubDate>
		<dc:creator>David Salahi</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[Parsley]]></category>

		<guid isPermaLink="false">http://flexperiential.com/?p=707</guid>
		<description><![CDATA[Arturo Alvarado has a very nice series of tutorials on the Parsley application framework but they&#8217;re in Flex 3. A couple of them have been translated into Flex 4 by other blog readers but not the code for his Quick Dive into Parsley (Nested Contexts &#038; Object lifecycle methods &#8211; Part 6). I&#8217;ve done that [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>Arturo Alvarado has <a href="http://artinflex.blogspot.com/2010/09/quick-dive-into-parsley-intro-why.html">a very nice series of tutorials</a> on the Parsley application framework but they&#8217;re in Flex 3. A couple of them have been translated into Flex 4 by other blog readers but not the code for his <a href="http://artinflex.blogspot.com/2010/09/quick-dive-into-parsley-nested-contexts.html">Quick Dive into Parsley (Nested Contexts &#038; Object lifecycle methods &#8211; Part 6)</a>. I&#8217;ve done that translation and you can <a href="http://flexperiential.com/code/Parsley Nested Context Example Flex4.fxp">download it here</a>. Note that I&#8217;ve only modified the code needed to make it compile and run.</p>
]]></content:encoded>
			<wfw:commentRss>http://flexperiential.com/2011/02/05/flex-4-version-of-arts-flex-notes-project/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Server-Side Validation with Flex</title>
		<link>http://flexperiential.com/2011/01/22/server-side-validation-with-flex/</link>
		<comments>http://flexperiential.com/2011/01/22/server-side-validation-with-flex/#comments</comments>
		<pubDate>Sat, 22 Jan 2011 20:14:12 +0000</pubDate>
		<dc:creator>David Salahi</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://flexperiential.com/?p=626</guid>
		<description><![CDATA[This application uses a custom Validator subclass which performs both client-side and server-side input validation. The client-side validation is done first whenever the First Name field loses focus. If it passes the client-side validation the input is sent to a server for further validation. Standard Flex Validator functionality is used. Right-click the app to view [...]]]></description>
			<content:encoded><![CDATA[<p></p><div style="float:left; padding-right:15px; width:170px; ">
    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_5" width="150" height="110">
      <param name="movie" value="http://flexperiential.com/code/ServerSideValidation/ServerSideValidation.swf" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://flexperiential.com/code/ServerSideValidation/ServerSideValidation.swf" width="150" height="110">
      <!--<![endif]-->
        
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>

<p style="font-size:small; font-style:italic">This application uses a custom Validator subclass which performs both client-side and server-side input validation. The client-side validation is done first whenever the First Name field loses focus. If it passes the client-side validation the input is sent to a server for further validation. Standard Flex Validator functionality is used. Right-click the app to view source or <a href="http://flexperiential.com/code/ServerSideValidation/ServerSideValidation.fxp">download an FXP file here</a>.</p>
</div>
<p>Flex provides nice form field validation functionality  through its various Validator classes, including:</p>
<ul style="list-style-position:inside;">
<li>CreditCardValidator</li>
<li>CurrencyValidator</li>
<li>DateValidator</li>
<li>EmailValidator</li>
<li>NumberValidator</li>
<li>PhoneNumberValidator</li>
<li>SocialSecurityValidator</li>
<li>StringValidator</li>
<li>StyleValidator</li>
<li>ZipCodeValidator</li>
</ul>
<p>If one of these doesn&rsquo;t serve your needs you might make use  of the RegExpValidator to customize your validation. And, if that still doesn&rsquo;t  do it you can subclass Validator to write your own validation algorithm. However,  sometimes even that&rsquo;s not enough. Sometimes, you really have to have  server-side validation. Maybe you have complex server-side business logic that  you don&rsquo;t want to replicate in the client. Maybe you need to look up something  in a database. You could fetch information from the DB and process the  validation in Flex but it&rsquo;s much more efficient and secure to do that kind of  thing on the server. In addition, by moving code out of the client and onto the  server you may be able to reduce the SWF file size.</p>
<p> But Flex doesn&rsquo;t provide any built-in support for  server-side validation. So, how can you accomplish it? This post shows one  technique.</p>
<h2>Client-Side and Server-Side Validation</h2>
<p>The original inspiration for this technique came from the  article <a href="http://iamjosh.wordpress.com/2008/01/31/data-services-with-server-side-validation/" target="_blank">Data  Services with Server-Side Validation</a> on the <a href="http://iamjosh.wordpress.com/" target="_blank">i am josh blog</a>. This example differs first,  in that it is complete, containing all code needed on both the client side and  the server side. Second, this example focuses on validating a single field at a  time. Josh&rsquo;s example includes a ValidateableErrors class which contains error  information for all the fields in a form. This implies that the server-side  validation is to be done for all fields at once after submitting the form. By  contrast, this solution provides maximum responsiveness by first doing whatever  simple validation is possible on the client. If an input field is validated in  the client-side logic the input is then sent on to the server for additional  validation. Each field is validated as soon as it loses focus.</p>
<h2>Form Which Uses the Custom Validator</h2>
<p>We&rsquo;ll start with a very simple form which contains just two  fields:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>VGroup paddingTop=<span style="color: #990000;">&quot;10&quot;</span><span style="color: #000066; font-weight: bold;">&gt;</span>
    <span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>Label <span style="color: #004993;">text</span>=<span style="color: #990000;">&quot;First Name:&quot;</span><span style="color: #000066; font-weight: bold;">/&gt;</span>
    <span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>TextInput id=<span style="color: #990000;">&quot;firstName&quot;</span> <span style="color: #004993;">focusOut</span>=<span style="color: #990000;">&quot;firstName_focusOutHandler(event)&quot;</span> <span style="color: #000066; font-weight: bold;">/&gt;</span>
    <span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>Label <span style="color: #004993;">text</span>=<span style="color: #990000;">&quot;Last Name:&quot;</span> <span style="color: #000066; font-weight: bold;">/&gt;</span>
    <span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>TextInput <span style="color: #000066; font-weight: bold;">/&gt;</span>
<span style="color: #000066; font-weight: bold;">&lt;/</span>s<span style="color: #000066; font-weight: bold;">:</span>VGroup<span style="color: #000066; font-weight: bold;">&gt;</span></pre></div></div>

<p> The first field is the only one which is active in this  example. The second field exists only to allow the focus to leave the first one  so that validation can be done.</p>
<p>We also have a custom validator object of class  ServerValidator:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">&lt;</span>local<span style="color: #000066; font-weight: bold;">:</span>ServerValidator id=<span style="color: #990000;">&quot;firstNameValidator&quot;</span> property=<span style="color: #990000;">&quot;text&quot;</span> <span style="color: #004993;">source</span>=<span style="color: #990000;">&quot;{firstName}&quot;</span> <span style="color: #000066; font-weight: bold;">/&gt;</span></pre></div></div>

<p>Linking the validator to the TextInput field as above means  that the validator will be called whenever the field loses focus.</p>
<p>We also have an HTTPService to facilitate sending the  contents of the firstName text field to the server for validation:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>HTTPService id=<span style="color: #990000;">&quot;serverValidator&quot;</span> <span style="color: #004993;">method</span>=<span style="color: #990000;">&quot;POST&quot;</span>
			   <span style="color: #004993;">url</span>=<span style="color: #990000;">&quot;http://flexperiential.com/code/ServerSideValidation/validator.php&quot;</span> 
			   result=<span style="color: #990000;">&quot;serverValidator_resultHandler(event)&quot;</span> 
			   fault=<span style="color: #990000;">&quot;serverValidator_faultHandler(event)&quot;</span> <span style="color: #000066; font-weight: bold;">/&gt;</span></pre></div></div>

<p>Whenever the focus leaves the firstName TextInput field the  handler is called to check whether the client-side validation was successful.  (If you run the code, the trace statements will show that the validator is  called before the focus-out listener.) If the client-side validation was  successful, the contents of the firstName field are sent to the server for  further checking. The result handler relays the results of the server-side  validation to our custom validator class. </p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> serverValidator_resultHandler<span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">:</span>ResultEvent<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;Validation result: &quot;</span> <span style="color: #000066; font-weight: bold;">+</span> event<span style="color: #000066; font-weight: bold;">.</span>result<span style="color: #000066; font-weight: bold;">.</span>html<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
    firstNameValidator<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">value</span> = event<span style="color: #000066; font-weight: bold;">.</span>result<span style="color: #000066; font-weight: bold;">.</span>html<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> firstName_focusOutHandler<span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">FocusEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;firstName_focusOutHandler&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>firstNameValidator<span style="color: #000066; font-weight: bold;">.</span>localValidationWasSuccessful<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;Sending data for server-side validation&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
        <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">data</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Object</span> =<span style="color: #000000;">&#123;</span><span style="color: #990000;">&quot;firstName&quot;</span> <span style="color: #000066; font-weight: bold;">:</span> firstName<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span><span style="color: #000000;">&#125;</span><span style="color: #000066; font-weight: bold;">;</span>
        serverValidator<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">send</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">data</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<h2>Custom Validator Class</h2>
<p>The custom class, ServerValidator, extends Flex&rsquo;s Validator class:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> ServerValidator <span style="color: #0033ff; font-weight: bold;">extends</span> Validator
<span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #6699cc; font-weight: bold;">var</span> _value<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _localValidationWasSuccessful<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span> = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span> 
    <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _doNeedClientSideValidation<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span> = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #000066; font-weight: bold;">.</span>
    <span style="color: #000066; font-weight: bold;">.</span></pre></div></div>

<p>The three member variables allow the validator class to keep  track of the status of the user&rsquo;s input and its validation. The variable _value  contains the user&rsquo;s current input—but only after it has passed muster with the  client-side validation. We saw this being set above in serverValidator_resultHandler.</p>
<p>The variable _localValidationWasSuccessful keeps track of  whether the input passed the client-side validation. If not, the form can avoid  sending the data to the server. I&#8217;ll cover this in more detail below.</p>
<p>Finally, the variable _doNeedClientSideValidation keeps  track of which phase the validator is in: client-side validation or server-side  validation.</p>
<p>The heart of the validator is the doValidation method which  you must override whenever you create a custom Validator:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">protected</span> override <span style="color: #339966; font-weight: bold;">function</span> doValidation<span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Object</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Array</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>_doNeedClientSideValidation<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #009900; font-style: italic;">// Client-side validation </span>
        <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;Client-side validation&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
        <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">toString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">toUpperCase</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">indexOf</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;D&quot;</span><span style="color: #000000;">&#41;</span> == <span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span> <span style="color: #009900; font-style: italic;">// All great names have a &quot;d&quot; (mine does!)</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;Client-side validation failed&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
            _doNeedClientSideValidation = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span>
            _localValidationWasSuccessful = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span>
            <span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #000000;">&#91;</span><span style="color: #0033ff; font-weight: bold;">new</span> ValidationResult<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #990000;">&quot;FAIL_CODE&quot;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #990000;">'First name must contain a &quot;D&quot;'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span>  
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;Client-side validation succeeded&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
        <span style="color: #009900; font-style: italic;">// Local validation was successful</span>
        _doNeedClientSideValidation = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span>
        _localValidationWasSuccessful = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span>
        <span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;Server-side validation&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
    _doNeedClientSideValidation = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>_value == <span style="color: #990000;">&quot;SUCCESS&quot;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;Server side validation succeeded&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
        <span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0033ff; font-weight: bold;">else</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;Server side validation FAILED&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
        <span style="color: #6699cc; font-weight: bold;">var</span> validationError<span style="color: #000066; font-weight: bold;">:</span>ValidationResult = <span style="color: #0033ff; font-weight: bold;">new</span> ValidationResult<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #990000;">&quot;FAIL_CODE&quot;</span><span style="color: #000066; font-weight: bold;">,</span> _value<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
        <span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #000000;">&#91;</span>validationError<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>If we&rsquo;re in the  client-side validation phase, the first if statement is entered and we check  the user&rsquo;s input which is passed in through the value argument. In this  example, I&#8217;m looking for the letter &ldquo;D.&rdquo; If the input contains the letter &ldquo;D&rdquo; the  input is valid; otherwise, it&rsquo;s not. Inside this if statement, we update the  two status variables based on whether the input is valid or not. If it was  valid we return an empty array which tells Flex that everything is copacetic.  And if it was not valid, we return a ValidationResult object containing  information about the error.</p>
<p>The rest of the function is concerned with processing the  results of the server-side validation. For this example I&rsquo;ve created a simple  PHP file which checks to see if the name contains at least three characters:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$first_name</span> <span style="color: #339933;">=</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'firstName'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$first_name</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">3</span> <span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Please enter at least three letters'</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span> 
   <span style="color: #b1b100;">else</span> 
   <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'SUCCESS'</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>If it does, the page sends the text &ldquo;SUCCESS&rdquo; and, if not,  it sends an error message for display to the user. In the MXML form the  ServerValidator&rsquo;s value property is set with the results returned by the server  to the HTTPService&rsquo;s resultHandler. The value setter also calls the Validator  class&rsquo; validate method which, in turn, causes the doValidation method to be  called.</p>
<p>At this point, _doNeedClientSideValidation is false so the  first if statement will be skipped. In this example, the value &ldquo;SUCCESS&rdquo; is  checked for and, if present, the method returns an empty array, again indicating  valid input. If anything else is received from the server it&rsquo;s assumed to be an  error message. In this case, a ValidationResult containing the error message is  returned.</p>
<h2>One Validator per Input Field</h2>
<p>Because the validator&rsquo;s state is closely coupled to the  input field&rsquo;s state it is not possible to use a validator instance with  multiple input fields as is sometimes done with standard Flex validators like  ZIP code or phone. If this were done, the validator could get out of sync with the  state of the input fields and fail to operate correctly.</p>
<p>One last note: in a real form with a submit button  (actually, you should never use the word submit; <a href="http://uxmovement.com/forms/why-your-form-buttons-should-never-say-submit" target="_blank">this  article on the UX Movement website</a> explains why) you would also want to call  all your validators in its click handler.</p>
]]></content:encoded>
			<wfw:commentRss>http://flexperiential.com/2011/01/22/server-side-validation-with-flex/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Creating a Custom Animation Effect in Flex 4</title>
		<link>http://flexperiential.com/2010/12/19/creating-a-custom-animation-effect-in-flex-4/</link>
		<comments>http://flexperiential.com/2010/12/19/creating-a-custom-animation-effect-in-flex-4/#comments</comments>
		<pubDate>Sun, 19 Dec 2010 23:27:38 +0000</pubDate>
		<dc:creator>David Salahi</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Animation]]></category>
		<category><![CDATA[effects]]></category>

		<guid isPermaLink="false">http://flexperiential.com/?p=617</guid>
		<description><![CDATA[As I discussed in my previous post &#8220;O&#8217;Reilly&#8217;s Flex 4 Cookbook Continues to Disappoint&#8221; I recently was frustrated when looking there for sample code to create a Flex 4 custom effect. The Adobe docs have some information on their page &#8220;About creating a custom effect&#8221; but some of the details are missing, particularly if you [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>As I discussed in my previous post &ldquo;<a href="http://flexperiential.com/2010/12/19/o’reilly’s-flex-4-cookbook-continues-to-disappoint/">O&#8217;Reilly&#8217;s Flex 4  Cookbook Continues to Disappoint</a>&rdquo; I recently was frustrated when looking there  for sample code to create a Flex 4 custom effect. The Adobe docs have some  information on their page &ldquo;<a href="http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf68e80-7fee.html">About  creating a custom effect</a>&rdquo; but some of the details are missing, particularly  if you want to create an effect based on the Spark Animate class. When I found  example 11.8 &ldquo;Create Custom Animation Effects&rdquo; in the Flex 4 Cookbook I thought  I&rsquo;d found the info I needed since the sample code on p. 308 shows a class extending  Animate. However, I soon found that the code presented on pp. 308-310 is a  confusing mish-mash of Flex 3 and Flex 4 that doesn&rsquo;t even compile. I&#8217;ll spare  you the gory details here. But if you&rsquo;re really interested, see my previous  post.</p>
<p> Here, I&#8217;ll share the code I eventually created to create a  custom effect by extending the Flex 4 Animate and AnimateInstance classes. As  discussed on the Adobe page mentioned above, you need to write two classes to  create a custom animation class. One class is the factory class which the Flex  framework uses to create an instance of your animation effect. The other is the  class which implements your custom animation. It&rsquo;s pretty easy once you figure  out the basic outline. Here&rsquo;s my factory class:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> CustomAnimateEffect <span style="color: #0033ff; font-weight: bold;">extends</span> Animate <span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> finalAlpha<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> CustomAnimateEffect <span style="color: #000000;">&#40;</span><span style="color: #004993;">target</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Object</span>=<span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">super</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">target</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span>instanceClass = CustomAnimateInstance<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #009900; font-style: italic;">// create our new instance</span>
	override <span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> initInstance<span style="color: #000000;">&#40;</span>instance<span style="color: #000066; font-weight: bold;">:</span>IEffectInstance<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">super</span><span style="color: #000066; font-weight: bold;">.</span>initInstance<span style="color: #000000;">&#40;</span>instance<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		CustomAnimateInstance<span style="color: #000000;">&#40;</span>instance<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">.</span>finalAlpha = <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span>finalAlpha<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	override <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> getAffectedProperties<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Array</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #000000;">&#91;</span><span style="color: #990000;">&quot;alpha&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The first thing you have to do is provide the effect factory with the name of your effect class. This is done here in the constructor where instanceClass is set. Then you have to set the properties to be animated in the override of initInstance. In this case, I&#8217;m only setting the end value finalAlpha. As you’ll see below, this demo effect starts at whatever alpha the target is currently set to. </p>
<p> You could also set multiple properties and animate them all  which is where a custom effect could really become useful. Of course, you could  also do that with a Parallel or Sequence object. But with a custom effect you  have more flexibility to animate whatever you want in whatever way you want. So,  for example, you might choose to animate a target object&rsquo;s alpha value uniformly  throughout the effect&rsquo;s duration but then wait to start animating its color  until 50% of the duration has elapsed. Then, you could animate the color change  twice as fast during the remaining half the duration. (OK, you could probably  do that with a Sequence and a startDelay but you get the idea.)</p>
<p>To complete the two required classes here&rsquo;s the code for the  custom animation:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> CustomAnimateInstance <span style="color: #0033ff; font-weight: bold;">extends</span> AnimateInstance<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> finalAlpha<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> CustomAnimateInstance<span style="color: #000000;">&#40;</span><span style="color: #004993;">target</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Object</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">super</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">target</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	override <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">play</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #6699cc; font-weight: bold;">var</span> motionPath<span style="color: #000066; font-weight: bold;">:</span>SimpleMotionPath = <span style="color: #0033ff; font-weight: bold;">new</span> SimpleMotionPath<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;alpha&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		motionPath<span style="color: #000066; font-weight: bold;">.</span>valueFrom = <span style="color: #000000;">&#40;</span><span style="color: #004993;">target</span> <span style="color: #0033ff; font-weight: bold;">as</span> <span style="color: #004993;">DisplayObject</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">alpha</span><span style="color: #000066; font-weight: bold;">;</span>
		motionPath<span style="color: #000066; font-weight: bold;">.</span>valueTo = finalAlpha == <span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span> <span style="color: #000066; font-weight: bold;">?</span> <span style="color: #000000; font-weight:bold;">1</span> <span style="color: #000066; font-weight: bold;">:</span> finalAlpha<span style="color: #000066; font-weight: bold;">;</span>
		motionPaths = <span style="color: #0033ff; font-weight: bold;">new</span> Vector<span style="color: #000066; font-weight: bold;">.&lt;</span>MotionPath<span style="color: #000066; font-weight: bold;">&gt;;</span>
		motionPaths<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>motionPath<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">super</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">play</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	override <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> animationUpdate<span style="color: #000000;">&#40;</span>animation<span style="color: #000066; font-weight: bold;">:</span>Animation<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #000000;">&#40;</span><span style="color: #004993;">target</span> <span style="color: #0033ff; font-weight: bold;">as</span> <span style="color: #004993;">DisplayObject</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">alpha</span> = animation<span style="color: #000066; font-weight: bold;">.</span>currentValue<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">alpha</span> <span style="color: #0033ff; font-weight: bold;">as</span> <span style="color: #004993;">Number</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Here, we override a couple of the methods from  IAnimationTarget. Most of the code simply creates a SimpleMotionPath which is  required by the Animate class. The Animate class calculates the tweened value  for you at each frame. Note that the motion path must be created in the play  method and not the constructor. This is because the finalAlpha value doesn&rsquo;t  get set until the effect begins to play. That&rsquo;s when CustomAnimateEffect&rsquo;s initInstance  method is called and that&rsquo;s where the desired  value gets passed in from the client code. And, here&rsquo;s that client code:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">&lt;</span>fx<span style="color: #000066; font-weight: bold;">:</span>Declarations<span style="color: #000066; font-weight: bold;">&gt;</span>
	<span style="color: #000066; font-weight: bold;">&lt;</span>local<span style="color: #000066; font-weight: bold;">:</span>CustomAnimateEffect id=<span style="color: #990000;">&quot;customEffect&quot;</span> finalAlpha=<span style="color: #990000;">&quot;1&quot;</span> <span style="color: #004993;">target</span>=<span style="color: #990000;">&quot;{btn2}&quot;</span> <span style="color: #000066; font-weight: bold;">/&gt;</span> 
<span style="color: #000066; font-weight: bold;">&lt;/</span>fx<span style="color: #000066; font-weight: bold;">:</span>Declarations<span style="color: #000066; font-weight: bold;">&gt;</span>
<span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>VGroup gap=<span style="color: #990000;">&quot;10&quot;</span> paddingTop=<span style="color: #990000;">&quot;10&quot;</span> paddingLeft=<span style="color: #990000;">&quot;10&quot;</span><span style="color: #000066; font-weight: bold;">&gt;</span>
	<span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>Button label=<span style="color: #990000;">&quot;Click to fade in the button below&quot;</span> <span style="color: #004993;">click</span>=<span style="color: #990000;">&quot;customEffect.play()&quot;</span><span style="color: #000066; font-weight: bold;">/&gt;</span>
	<span style="color: #000066; font-weight: bold;">&lt;</span>s<span style="color: #000066; font-weight: bold;">:</span>Button id=<span style="color: #990000;">&quot;btn2&quot;</span> label=<span style="color: #990000;">&quot;Fade Me In&quot;</span> <span style="color: #004993;">fontSize</span>=<span style="color: #990000;">&quot;50&quot;</span> <span style="color: #004993;">alpha</span>=<span style="color: #990000;">&quot;0.2&quot;</span><span style="color: #000066; font-weight: bold;">/&gt;</span>
<span style="color: #000066; font-weight: bold;">&lt;/</span>s<span style="color: #000066; font-weight: bold;">:</span>VGroup<span style="color: #000066; font-weight: bold;">&gt;</span></pre></div></div>

<p><a href="http://flexperiential.com/code/CustomEffect/CustomEffects.fxp">Download the source code for the custom Flex 4 animation effect (fxp).</a></p>
]]></content:encoded>
			<wfw:commentRss>http://flexperiential.com/2010/12/19/creating-a-custom-animation-effect-in-flex-4/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>O’Reilly’s Flex 4 Cookbook Continues to Disappoint</title>
		<link>http://flexperiential.com/2010/12/19/o%e2%80%99reilly%e2%80%99s-flex-4-cookbook-continues-to-disappoint/</link>
		<comments>http://flexperiential.com/2010/12/19/o%e2%80%99reilly%e2%80%99s-flex-4-cookbook-continues-to-disappoint/#comments</comments>
		<pubDate>Sun, 19 Dec 2010 23:01:22 +0000</pubDate>
		<dc:creator>David Salahi</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Animation]]></category>
		<category><![CDATA[Books]]></category>
		<category><![CDATA[effects]]></category>

		<guid isPermaLink="false">http://flexperiential.com/?p=612</guid>
		<description><![CDATA[I&#8217;ve been a fan of the O&#8217;Reilly Cookbooks in the past and have found valuable info in both their ActionScript 3.0 Cookbook and Flex 3 Cookbook. So, when O&#8217;Reilly released their Flex 4 Cookbook earlier this year I purchased a PDF copy as soon as it became available. However, I&#8217;ve been disappointed by this volume [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>I&rsquo;ve been a fan of the O&rsquo;Reilly Cookbooks in the past and  have found valuable info in both their ActionScript 3.0 Cookbook and Flex 3  Cookbook. So, when O&rsquo;Reilly released their Flex 4 Cookbook earlier this year I purchased  a PDF copy as soon as it became available. However, I&rsquo;ve been disappointed by  this volume as I&rsquo;ve blogged previously (<a href="http://flexperiential.com/2010/09/15/flex-4-cookbook-sample-code-difficult-to-use/">Flex 4 Cookbook Sample Code Difficult to Use</a> ). Unfortunately, as I continue to use the book I continue to be frustrated.</p>
<p> My most recent frustration was occasioned by an attempt to  use the code in section 11.8, &ldquo;Create Custom Animation Effects.&rdquo; I started by  creating a new Flex 4 project and then copying the book&rsquo;s two downloaded sample  files, CustomTweenEffect.as and CustomTweenInstance.as, into the new project. I  then added some code to my new MXML application to use CustomTweenEffect but I  was confronted with four compile errors in the Cookbook code. The first error was  caused by what appeared to be an extra constructor in the CustomTweenEffect  class. This extra constructor was for a class called CustomDisplacementEffect.  This was the only reference to CustomDisplacementEffect in the two downloaded  source code files so I commented out this function.</p>
<p> I moved on to an error in the CustomTweenEffect class  declaration—a missing import for the Flex Animate class. I added the import  statement and, while I was at it, I removed the unneeded import for mx.effects.TweenEffect. </p>
<p>At this point, CustomTweenEffect compiled successfully so next  I continued with the one remaining error which was in the CustomTweenInstance  class. In this case, the problem appeared to be an incorrectly named  constructor function: NewTweenInstance. The class in question is CustomTweenInstance  so I changed the constructor function name to match. One step forward, one step  back. Now, I had a newly reported error on line 11: </p>
<p> 1180: Call to a possibly undefined method Tween.           CustomTweenInstance.as                /CustomEffects/src/oreilly/cookbook    line 11   Flex  Problem</p>
<p>Another missing import, easy to fix but annoying. Meanwhile,  back in CustomTweenEffect  another new  error had popped up:<br />
1046: Type was not found or was not a compile-time constant:  IEffectInstance. CustomTweenEffect.as                /CustomEffects/src/oreilly/cookbook    line 16   Flex  Problem</p>
<p>Yet another missing import, this time for IEffectInstance. After  fixing this, the two cookbook classes finally compiled. Dare I hope that it would  run? No. Here&rsquo;s the run-time error (at CustomTweenEffect.as, line 21):</p>
<p>TypeError: Error #1034: Type Coercion failed: cannot convert  spark.effects.supportClasses::AnimateInstance@1d45ae51 to  oreilly.cookbook.CustomTweenInstance.</p>
<p>Here&rsquo;s the code at line 21:</p>
<p>CustomTweenInstance(instance).finalAlpha = this.finalAlpha;</p>
<p>This looked to me like CustomTweenInstance was extending the  wrong class (TweenEffectInstance). So, I tried changing it to extend AnimateInstance.  That led to a compile error on the onTweenUpdate function which is not a member  of AnimateInstance. So, I changed that method name to animationUpdate, which is  the corresponding method in IAnimationTarget (which is implemented by  AnimateInstance), but then the passed-in parameter was wrong. I fixed that. One  more step forward, one more step back.   Now, I had a compile error in the CustomTweenInstance play function.</p>
<p>Long story short, I ended up completely rewriting the Cookbook&rsquo;s  two sample classes so that they look almost nothing like the originals. To see  an example of creating a custom tween/animate effect see my post &ldquo;<a href="http://flexperiential.com/2010/12/19/creating-a-custom-animation-effect-in-flex-4/">Creating a Custom  Animation effect in Flex 4.</a>&rdquo;</p>
<p>Before I started all this debugging I checked the book&rsquo;s download  &amp; errata pages on the O&rsquo;Reilly website. However, the latest download available  was dated June, 2010—the same version I already had. And there was nothing in  the book&rsquo;s errata page about these problems. It appears O&rsquo;Reilly has taken the  approach of having its customers become alpha testers. But even with a public  alpha/beta test one can hope that eventually the software will be debugged. Unfortunately,  the Flex 4 Cookbook, with a release date of May, 2010, seems to be stuck somewhere  around V0.8 as of the end of 2010.</p>
<h3>Update 7/20/11</h3>
<p>  A reader emailed me to tell of the problems he was having  while trying to use the DataGrid examples in chapter 9. Indeed, these examples  are a mess. Here are the changes I had to make to example 9_1_b to get them  working:</p>
<ol>
<li>Create a project in Flash Builder and copy the  sample files into it.</li>
<li>Rename the MXML file to start with a non-numeric  character.</li>
<li>Remove the &ldquo;oreilly.cookbook.flex4&rdquo; package on  the DataGrid&rsquo;s itemrenderer property.</li>
<li>Copy the file example.mxml from the parent  directory of the samples into the Flash Builder project directory.</li>
<li>Change the URL on the HTTPService to point to  example.mxml instead of assets/homesforsale.xml.</li>
<li>Change the mx: namespace declaration to xmlns:mx=&quot;library://ns.adobe.com/flex/mx&quot;</li>
</ol>
<p>It&rsquo;s really a shame that these examples were so  thoughtlessly packaged. If you can get over these hurdles the sample code is  actually quite good. But someone trying to learn Flex might easily be  overwhelmed and give up.</p>
]]></content:encoded>
			<wfw:commentRss>http://flexperiential.com/2010/12/19/o%e2%80%99reilly%e2%80%99s-flex-4-cookbook-continues-to-disappoint/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

