<?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/"
	xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule"
>

<channel>
	<title>Building Blocks &#187; flex</title>
	<atom:link href="http://joelhooks.com/category/flex/feed/" rel="self" type="application/rss+xml" />
	<link>http://joelhooks.com</link>
	<description>[without unit tests] you&#039;re not refactoring; you&#039;re just changing shit. -Hamlet D&#039;Arcy</description>
	<lastBuildDate>Fri, 30 Dec 2011 04:11:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
		<item>
		<title>An Introduction to Robotlegs AS3 Part 3: Services</title>
		<link>http://joelhooks.com/2011/03/12/an-introduction-to-robotlegs-as3-part-3-services/</link>
		<comments>http://joelhooks.com/2011/03/12/an-introduction-to-robotlegs-as3-part-3-services/#comments</comments>
		<pubDate>Sat, 12 Mar 2011 18:41:34 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[robotlegs]]></category>

		<guid isPermaLink="false">http://joelhooks.com/?p=600</guid>
		<description><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top: 60%;
left: 5px;
}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;

padding-bottom:2px;
}


#bottomcontainerBox {
height: 30px;
width:50%;
padding-top:1px;
}

#bottomcontainerBox .buttons {
float:left;
height: 30px;
margin:4px 4px 4px 4px;
}

</style>
This is the third part in the introductory series covering Robotlegs AS3 here on InsideRIA. In the first part in the series we learned what Robotlegs AS3 is and had a brief "HelloWorld" introduction to the Robotlegs Context and Mediator classes. In the second part we explored Models. This article is going to expand on [...]]]></description>
			<content:encoded><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top: 60%;
left: 5px;
}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;

padding-bottom:2px;
}


#bottomcontainerBox {
height: 30px;
width:50%;
padding-top:1px;
}

#bottomcontainerBox .buttons {
float:left;
height: 30px;
margin:4px 4px 4px 4px;
}

</style>
<p>This is the third part in the introductory series covering Robotlegs AS3 here on InsideRIA. In the first part in the series we learned what <a href="http://www.robotlegs.org">Robotlegs AS3</a> is and had a brief <a href="http://joelhooks.com/2011/03/12/an-introduction-to-robotlegs-as3-part-1-context-and-mediators/">"HelloWorld" introduction to the Robotlegs Context and Mediator</a> classes. In the <a href="http://joelhooks.com/2011/03/12/an-introduction-to-robotlegs-as3-part-2-models/">second part</a> we explored Models. This article is going to expand on those concepts and introduce Services.</p>
<p><span class="mt-enclosure mt-enclosure-image" style="display: inline;"><img alt="robotlegs.jpg" src="http://www.developria.com/2010/06/03/robotlegs.jpg" width="157" height="164" class="mt-image-right" style="float: right; margin: 0 0 20px 20px;" /></span> </p>
<p>If you missed the first two parts I recommend looking at them first:</p>
<ul>
<li>
      <a href="http://joelhooks.com/2011/03/12/an-introduction-to-robotlegs-as3-part-1-context-and-mediators/">Part One: Context and Mediators</a>
    </li>
<li>
      <a href="http://joelhooks.com/2011/03/12/an-introduction-to-robotlegs-as3-part-2-models/">Part Two: Models</a>
    </li>
</ul>
<h2>What is a Service?</h2>
<p>The name of this website is InsideRIA. A Service is your application's link to the I in RIA. Services provide your application an API to access web services, data sources, and other applications. This could be accessing the Twitter API, your company/client's web server, the file system, or a host of other sources of data outside of your application.</p>
<p>A Service makes remote calls and parses the results. After the results are parsed, the service will notify the rest of the application that data is ready and/or update a Model with the results. We want external data to live as short of a life as possible within our applications. As soon as we get the results, they are converted to a format that is specific to the application domain. This might be XML, JSON, or typed Objects from an AMF source. When we are working in a "controlled" environment, meaning the server-side mid-tier is within our control, sometimes we can avoid parsing altogether because the returned objects are already part of the application domain. In many situations we are accessing third-party servers and receiving "foreign" data. This is what we want to parse.</p>
<h2>Let's make something!</h2>
<p>So with the definition behind us, lets look at how a Service functions within a Robotlegs AS3 application. This example is going to make use of the basic Twitter Search API.</p>
<p><span class="mt-enclosure mt-enclosure-image" style="display: inline;"><img alt="serve-talks-to-twitter.png" src="http://www.developria.com/2010/06/12/serve-talks-to-twitter.png" width="515" height="320" class="mt-image-center" style="text-align: center; display: block; margin: 0 auto 20px;" /></span> </p>
<p>The diagram above is the general overview of how our service will function. It receives a request, asks the server for some data, parses the results, and notifies the rest of the application. The requests are generally made from Mediators and Commands. In this example, we are going to keep it simple and make requests to the service from a Mediator. In the next article we will talk about how Commands are a great place to access services. Let's take a look at the finished widget and then build it from the ground up.</p>
<p> <span id="more-600"></span></p>
<p>
<div align=center><iframe src="http://joelhooks.com/examples/serviceExample" width="300" height="300" scrolling="no" frameborder="0"></iframe></div>
</p>
<div align="center"><span class="mt-enclosure mt-enclosure-file" style="display: inline;"><a href="http://www.developria.com/2010/06/18/ServiceExample.zip">Download ServiceExample.zip</a></span></div>
<p>So it's not the next TweetDeck, but it is about as simple as it gets when accessing the Twitter API. The widget access the Twitter Search API and returns the current results for "robotlegs." We'll start simple, and add complexity later if we need to. Let's take a look at the pieces that go into to putting this together:</p>
<h3>ServiceExample.mxml</h3>

<div class="wp_codebox"><table><tr id="p60016"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p600code16"><pre class="actionscript" style="font-family:monospace;"><span style="color: #66cc66;">&lt;</span>?<span style="color: #0066CC;">xml</span> <span style="color: #0066CC;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span>?<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;</span>s:Application xmlns:fx=<span style="color: #ff0000;">&quot;http://ns.adobe.com/mxml/2009&quot;</span> 
               xmlns:s=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/spark&quot;</span>
               xmlns:service=<span style="color: #ff0000;">&quot;robotlegs.examples.service.*&quot;</span> 
               xmlns:mx=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/mx&quot;</span>
               xmlns:view=<span style="color: #ff0000;">&quot;robotlegs.examples.service.view.*&quot;</span>
               <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;300&quot;</span>
               <span style="color: #0066CC;">height</span>=<span style="color: #ff0000;">&quot;300&quot;</span><span style="color: #66cc66;">&gt;</span>
    <span style="color: #66cc66;">&lt;</span>fx:Declarations<span style="color: #66cc66;">&gt;</span>
        <span style="color: #66cc66;">&lt;</span>service:ServiceExampleContext contextView=<span style="color: #ff0000;">&quot;{this}&quot;</span><span style="color: #66cc66;">/&gt;</span>
    <span style="color: #66cc66;">&lt;/</span>fx:Declarations<span style="color: #66cc66;">&gt;</span>
    <span style="color: #66cc66;">&lt;</span>view:SearchResultsView<span style="color: #66cc66;">/&gt;</span>
<span style="color: #66cc66;">&lt;/</span>s:Application<span style="color: #66cc66;">&gt;</span></pre></td></tr></table></div>

<p>This is the main application view. You will notice that I have already made an instance of the ServiceExampleContext available in the Declarations tag. If you haven't had a chance to read parts <a href="http://www.developria.com/2010/06/an-introduction-to-robotlegs-a.html">1</a> and <a href="http://www.developria.com/2010/06/an-introduction-to-robotlegs-a-1.html">2</a> in the series, I recommend doing so now. They cover basics of creating and initializing the Context. In addition to the ServiceExampleContext, you see that the SearchResultView has also been added to the application. This example only has the one view. Take a look at the context:</p>
<h3>ServiceExampleContext.as</h3>

<div class="wp_codebox"><table><tr id="p60017"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p600code17"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ServiceExampleContext <span style="color: #0066CC;">extends</span> Context
<span style="color: #66cc66;">&#123;</span>
    override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> startup<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
    <span style="color: #66cc66;">&#123;</span>
        mediatorMap.<span style="color: #006600;">mapView</span><span style="color: #66cc66;">&#40;</span>SearchResultsView, SearchResultsViewMediator<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>In the SearchExampleContext we are mapping a mediator for the SearchResultsView. The SearchResultsView is a sub-class of the Spark List component:</p>
<h3>SearchResultsView.mxml</h3>

<div class="wp_codebox"><table><tr id="p60018"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p600code18"><pre class="actionscript" style="font-family:monospace;"><span style="color: #66cc66;">&lt;</span>?<span style="color: #0066CC;">xml</span> <span style="color: #0066CC;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span>?<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;</span>s:<span style="color: #0066CC;">List</span>
        xmlns:fx=<span style="color: #ff0000;">&quot;http://ns.adobe.com/mxml/2009&quot;</span>
        xmlns:s=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/spark&quot;</span>
        xmlns:mx=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/mx&quot;</span>
        itemRenderer=<span style="color: #ff0000;">&quot;robotlegs.examples.service.view.renderers.SearchResultItemRenderer&quot;</span>
        <span style="color: #0066CC;">height</span>=<span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;300&quot;</span><span style="color: #66cc66;">&gt;</span>
    <span style="color: #66cc66;">&lt;</span>s:layout<span style="color: #66cc66;">&gt;&lt;</span>s:VerticalLayout gap=<span style="color: #ff0000;">&quot;0&quot;</span> paddingBottom=<span style="color: #ff0000;">&quot;10&quot;</span> paddingRight=<span style="color: #ff0000;">&quot;10&quot;</span> paddingLeft=<span style="color: #ff0000;">&quot;10&quot;</span> paddingTop=<span style="color: #ff0000;">&quot;10&quot;</span><span style="color: #66cc66;">/&gt;&lt;/</span>s:layout<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;/</span>s:List<span style="color: #66cc66;">&gt;</span></pre></td></tr></table></div>

<p>The SearchResultsView needs a mediator to allow it to communicate with the rest of the application:</p>
<h3>SearchResultsViewMediator.as</h3>

<div class="wp_codebox"><table><tr id="p60019"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p600code19"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SearchResultsViewMediator <span style="color: #0066CC;">extends</span> Mediator
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #66cc66;">&#91;</span>Inject<span style="color: #66cc66;">&#93;</span>
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> view:SearchResultsView;
&nbsp;
    override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> onRegister<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #808080; font-style: italic;">//Make the magic!</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Now that the SearchResultsView is properly mediated, we are ready to start looking at the service that will connect to the remote Twitter server and return some data for us to list. If you were looking closely at the SearchResultsView you will see that we have also given it an ItemRenderer. We will be looking at that in just a bit. First, let's take a look at the basic service class that will connect to Twitter and fetch our data:</p>
<h3>TwitterSearchService.as</h3>

<div class="wp_codebox"><table><tr id="p60020"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
</pre></td><td class="code" id="p600code20"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TwitterSearchService <span style="color: #0066CC;">extends</span> Actor <span style="color: #0066CC;">implements</span> ISearchService
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> loader:URLLoader;
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> TwitterSearchService<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        loader = <span style="color: #000000; font-weight: bold;">new</span> URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0066CC;">private</span> const TWITTER_SEARCH_ENDPOINT:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;http://search.twitter.com/search.json&quot;</span>;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getResults<span style="color: #66cc66;">&#40;</span>forQuery:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;robotlegs&quot;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">var</span> urlRequest:URLRequest = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span>TWITTER_SEARCH_ENDPOINT<span style="color: #66cc66;">&#41;</span>;
        <span style="color: #000000; font-weight: bold;">var</span> params:URLVariables = <span style="color: #000000; font-weight: bold;">new</span> URLVariables<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        params.<span style="color: #006600;">q</span> = HTMLStringTools.<span style="color: #006600;">htmlEscape</span><span style="color: #66cc66;">&#40;</span>forQuery<span style="color: #66cc66;">&#41;</span>;
        params.<span style="color: #006600;">rpp</span> = <span style="color: #cc66cc;">100</span>;
        urlRequest.<span style="color: #0066CC;">data</span> = params;
&nbsp;
        addLoaderListeners<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        loader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>urlRequest<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> handleError<span style="color: #66cc66;">&#40;</span>event:SecurityErrorEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
    <span style="color: #66cc66;">&#123;</span>
        removeLoaderListeners<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> handleLoadComplete<span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
    <span style="color: #66cc66;">&#123;</span>
        removeLoaderListeners<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> addLoaderListeners<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
    <span style="color: #66cc66;">&#123;</span>
        loader.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>IOErrorEvent.<span style="color: #006600;">IO_ERROR</span>, handleError<span style="color: #66cc66;">&#41;</span>;
        loader.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>SecurityErrorEvent.<span style="color: #006600;">SECURITY_ERROR</span>, handleError<span style="color: #66cc66;">&#41;</span>;
        loader.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>, handleLoadComplete<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> removeLoaderListeners<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
    <span style="color: #66cc66;">&#123;</span>
        loader.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>IOErrorEvent.<span style="color: #006600;">IO_ERROR</span>, handleError<span style="color: #66cc66;">&#41;</span>;
        loader.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>SecurityErrorEvent.<span style="color: #006600;">SECURITY_ERROR</span>, handleError<span style="color: #66cc66;">&#41;</span>;
        loader.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>, handleLoadComplete<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>TwitterSearchService has one public method, getResults(). It fetches results for a search string from the Twitter Search API. It is using the URLLoader to accomplish this and accessing the API directly. There are a number of excellent Twitter API libraries for Actionscript available. I'd highly recommend investigating them if you are trying to write the next TweetDeck. With the URLLoader the TwitterSearchService makes a simple request to the appropriate endpoint. Listeners are added to the URLLoader, and the fault/result handlers catch whatever is thrown back. Right now handleLoadComplete isn't actually doing anything except removing the listeners, which isn't very useful, but we will remedy that in just a moment.</p>
<p>Note that TwitterSearchService extends Actor and implements ISearchService. Actor simply provides convenient access to the EventDispatcher for this context. With services, it is generally considered best practice to implement an interface. Take a look at ISearchService:</p>
<h3>ISearchService.as</h3>

<div class="wp_codebox"><table><tr id="p60021"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p600code21"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> ISearchService
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">function</span> getResults<span style="color: #66cc66;">&#40;</span>forQuery:<span style="color: #0066CC;">String</span>=<span style="color: #ff0000;">&quot;robotlegs&quot;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>When you implement an interface for a service class, it makes it <b>really easy</b> to swap the service out. Why would you want to do that? When connecting to remote services it is sometimes more effectiveduring development to create mock data services with 0 latency. You could make a MockTwitterJsonService that returned the same static, hardcoded results every time. This approach is critical for unit testing, which will be covered in another article, as well as just general development. Many times you are will find yourself developing in parallel to the team developing the service. The ability to create and replace mock services with live ones provides a lot of flexibility.</p>
<p>You will recall in the definition of a Robotlegs service above that we talked about parsing data as soon as it is returned from the remote service. In the case of the Twitter Search API endpoint we are using, we receive a JSON string as the result. JSON is a great format, but we don't want to start passing around raw JSON strings in our application. It loses the benefit of strong typing, and besides, we want to pick and choose what information we are going to use in our application. In this case, we only need the user name and actual tweet text. </p>
<h3>Tweet.as</h3>

<div class="wp_codebox"><table><tr id="p60022"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p600code22"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Tweet
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> user:<span style="color: #0066CC;">String</span>;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">message</span>:<span style="color: #0066CC;">String</span>;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>This class needs a sandwich! Not a lot going on here, but we now have something to convert the results to.</p>
<h3>TwitterSearchResultParser.as</h3>

<div class="wp_codebox"><table><tr id="p60023"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code" id="p600code23"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TwitterSearchResultParser <span style="color: #0066CC;">implements</span> ISearchResultParser
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> parseSearchResults<span style="color: #66cc66;">&#40;</span>results:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Array</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">var</span> decoder:JSONDecoder = <span style="color: #000000; font-weight: bold;">new</span> JSONDecoder<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#40;</span>results<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #000000; font-weight: bold;">var</span> resultObjects:<span style="color: #0066CC;">Array</span> = decoder.<span style="color: #006600;">getValue</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">results</span>;
        <span style="color: #000000; font-weight: bold;">var</span> tweets:<span style="color: #0066CC;">Array</span> = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;
        <span style="color: #b1b100;">for</span> <span style="color: #b1b100;">each</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> result:<span style="color: #0066CC;">Object</span> <span style="color: #b1b100;">in</span> resultObjects<span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">var</span> tweet:Tweet = <span style="color: #000000; font-weight: bold;">new</span> Tweet<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
            tweet.<span style="color: #006600;">user</span> = result.<span style="color: #006600;">from_user</span>;
            tweet.<span style="color: #0066CC;">message</span> = HTMLStringTools.<span style="color: #006600;">htmlUnescape</span><span style="color: #66cc66;">&#40;</span>result.<span style="color: #0066CC;">text</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
            tweets.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>tweet<span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #b1b100;">return</span> tweets;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The results parser is a straight forward utility class. For the same reasons the service implements an interface, the results parser does as well. It has one method, parseSearchResults() which takes a results argument. The results from Twitter will be a string, but other results might be XML if we were to use local mock data or change which Twitter service we were accessing. The JSON string is encoded into objects, which are then converted to our Tweet value object from above. The resulting Array is returned as the result. Now that the parser is ready to go, we want to supply it to the service so that it can parse the results.</p>
<p>You have several options for giving the service access to the parser class. You could make its methods static, you could create a <b>new</b> instance of the parser class within the service, or you can leverage dependency injection to supply the parser to the service class. The problem with the first two options is that it makes it a lot harder to swap out the parser class later if we needed to.</p>
<p>We are in the land of dependency injection now, so why not map the parser for injection? If we do this, it will be one single line in our context to change if we want to switch it out. There is no digging around in our classes commenting out lines and changing types. because both the parser and service conform to interfaces. If you'd like to see how easy it is to swap out a service, take a look at the <a href="http://github.com/robotlegs/robotlegs-demos-Bundle/blob/master/FlickrImageGallery/src/org/robotlegs/demos/imagegallery/ImageGalleryContext.as#L44">Flickr Image Gallery</a> in the <a href="http://github.com/robotlegs/robotlegs-demos-Bundle">Robotlegs Demo Bundle</a>.</p>
<p>Let's look at mapping the service and parser interfaces in the context:</p>
<h3>ServiceExampleContext.as</h3>

<div class="wp_codebox"><table><tr id="p60024"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p600code24"><pre class="actionscript" style="font-family:monospace;">override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> startup<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
    mediatorMap.<span style="color: #006600;">mapView</span><span style="color: #66cc66;">&#40;</span>SearchResultsView, SearchResultsViewMediator<span style="color: #66cc66;">&#41;</span>;
&nbsp;
    injector.<span style="color: #006600;">mapSingletonOf</span><span style="color: #66cc66;">&#40;</span>ISearchService, TwitterSearchService<span style="color: #66cc66;">&#41;</span>;
    injector.<span style="color: #006600;">mapSingletonOf</span><span style="color: #66cc66;">&#40;</span>ISearchResultParser, TwitterSearchResultParser<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>This is the first time we have used the mapSingletonOf() method of the injector. It behaves much the same way as mapSingleton, with one critical difference. With mapSingletonOf() you are mapping the class you wish to inject via its interface or base class. Let's take a look at the TwitterSearchService and see how this is injected into our classes:</p>
<h3>TwitterSearchService.as</h3>

<div class="wp_codebox"><table><tr id="p60025"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code" id="p600code25"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _parser:ISearchResultParser;
&nbsp;
<span style="color: #66cc66;">&#91;</span>Inject<span style="color: #66cc66;">&#93;</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> parser<span style="color: #66cc66;">&#40;</span>value:ISearchResultParser<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
    _parser = value;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> loader:URLLoader;
&nbsp;
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> TwitterSearchService<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    loader = <span style="color: #000000; font-weight: bold;">new</span> URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>With the above code added to the TwitterSearchService you are now injecting the parser that was mapped into it. There are a couple of interesting things happening here. You will notice right away that the parser is of type ISearchResultParser. Since we used mapSingletonOf() to map our parser, it looks for a property of the base class or interface type that we mapped. It will actually inject the TwitterSearchResultParser, but we will access it via its interface. This approach is referred to as <a href="http://www.developria.com/2009/09/as3-object-oriented-concepts-p.html">polymorphism</a> and is a power object-oriented concept.</p>
<p>We are also placing the [Inject] tag above the setter function and not the property itself. In addition to property injection, which we have used up until now, Robotlegs supports setter and constructor injection. In this case we are using setter injection to supply the value to the parser property's setter function. Constructor injection doesn't even require the [Inject] metadata to function. We will talk about construction injection later, for now we need to remember to add the setter function to the ISearchService interface:</p>
<h3>ISearchService.as</h3>

<div class="wp_codebox"><table><tr id="p60026"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p600code26"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> ISearchService
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">function</span> getResults<span style="color: #66cc66;">&#40;</span>forQuery:<span style="color: #0066CC;">String</span>=<span style="color: #ff0000;">&quot;robotlegs&quot;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
&nbsp;
    <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> parser<span style="color: #66cc66;">&#40;</span>value:ISearchResultParser<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Now that the parser is all sorted out, we are ready to actually use it in the service. The result handler is the likely spot to make use of the parser, so we will add some code there:</p>
<h3>TwitterSearchService.as</h3>

<div class="wp_codebox"><table><tr id="p60027"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p600code27"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> handleLoadComplete<span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">data</span>:<span style="color: #0066CC;">Object</span> = loader.<span style="color: #0066CC;">data</span>;
    <span style="color: #000000; font-weight: bold;">var</span> results:<span style="color: #0066CC;">Array</span> = _parser.<span style="color: #006600;">parseSearchResults</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#41;</span>;
    dispatch<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> SearchResultEvent<span style="color: #66cc66;">&#40;</span>SearchResultEvent.<span style="color: #006600;">RECEIVED</span>, results<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    removeLoaderListeners<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>There we go, the data is extracted from the URLLoader, sent to the parser to be converted into an Array of Tweet objects, an event is dispatched notifying the rest of the app that we now have results, and the event listeners are removed. The SearchResultEvent is a simple custom event that has an array as its payload:</p>
<h3>SearchResultEvent.as</h3>

<div class="wp_codebox"><table><tr id="p60028"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code" id="p600code28"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SearchResultEvent <span style="color: #0066CC;">extends</span> Event
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const RECEIVED:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;searchResultsReceived&quot;</span>;
&nbsp;
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _results:<span style="color: #0066CC;">Array</span>;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> results<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Array</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">return</span> _results;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> SearchResultEvent<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">type</span>:<span style="color: #0066CC;">String</span>, results:<span style="color: #0066CC;">Array</span> = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">type</span><span style="color: #66cc66;">&#41;</span>;
        _results = results;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> clone<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Event
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> SearchResultEvent<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">type</span>, results<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>At this point in development we have a service that should work, a mediated view component, and an event being dispatched to deaf ears. It would be more accurate to say an event that is never dispatched at all. We haven't wired the service anywhere to call getResult(). We will take care of that in the SearchResultViewMediator:</p>
<h3>SearchResultsViewMediator.as</h3>

<div class="wp_codebox"><table><tr id="p60029"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code" id="p600code29"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SearchResultsViewMediator <span style="color: #0066CC;">extends</span> Mediator
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #66cc66;">&#91;</span>Inject<span style="color: #66cc66;">&#93;</span>
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> view:SearchResultsView;
&nbsp;
    <span style="color: #66cc66;">&#91;</span>Inject<span style="color: #66cc66;">&#93;</span>
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> service:ISearchService;
&nbsp;
    override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> onRegister<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
    <span style="color: #66cc66;">&#123;</span>
        service.<span style="color: #006600;">getResults</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        addContextListener<span style="color: #66cc66;">&#40;</span>SearchResultEvent.<span style="color: #006600;">RECEIVED</span>, handleResultsReceived<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> handleResultsReceived<span style="color: #66cc66;">&#40;</span>event:SearchResultEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
    <span style="color: #66cc66;">&#123;</span>
        view.<span style="color: #006600;">dataProvider</span> = <span style="color: #000000; font-weight: bold;">new</span> ArrayCollection<span style="color: #66cc66;">&#40;</span>event.<span style="color: #006600;">results</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Now we are talking! Again the service is injected via its interface, and in the onRegister() we make a call to getResults(). The magical hamsters at Twitter HQ generate some content that our service parses and notifies the rest of the application with an event carrying the data. The mediator is listening for that event, and in handleResultsReceived it supplies the results to the view's dataProvider as an ArrayCollection. All we need to add is the ItemRenderer and we will have a fully semi-functional Twitter client:</p>
<h3>SearchResultItemRenderer.mxml</h3>

<div class="wp_codebox"><table><tr id="p60030"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code" id="p600code30"><pre class="actionscript" style="font-family:monospace;"><span style="color: #66cc66;">&lt;</span>?<span style="color: #0066CC;">xml</span> <span style="color: #0066CC;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span>?<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;</span>s:ItemRenderer
        xmlns:fx=<span style="color: #ff0000;">&quot;http://ns.adobe.com/mxml/2009&quot;</span>
        xmlns:s=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/spark&quot;</span>
        xmlns:mx=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/mx&quot;</span>
        <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;260&quot;</span> <span style="color: #0066CC;">height</span>=<span style="color: #ff0000;">&quot;85&quot;</span><span style="color: #66cc66;">&gt;</span>
    <span style="color: #66cc66;">&lt;</span>fx:Script<span style="color: #66cc66;">&gt;&lt;!</span><span style="color: #66cc66;">&#91;</span>CDATA<span style="color: #66cc66;">&#91;</span>
        <span style="color: #0066CC;">import</span> robotlegs.<span style="color: #006600;">examples</span>.<span style="color: #006600;">service</span>.<span style="color: #006600;">model</span>.<span style="color: #006600;">Tweet</span>;
&nbsp;
        override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> <span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#40;</span>value:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
        <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0066CC;">super</span>.<span style="color: #0066CC;">data</span> = value;
            <span style="color: #000000; font-weight: bold;">var</span> searchResult:Tweet = value as Tweet;
            <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>searchResult<span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#123;</span>
                <span style="color: #0066CC;">message</span>.<span style="color: #0066CC;">text</span> = searchResult.<span style="color: #0066CC;">message</span>;
                username.<span style="color: #0066CC;">text</span> = searchResult.<span style="color: #006600;">user</span>;
            <span style="color: #66cc66;">&#125;</span>
            <span style="color: #b1b100;">else</span>
            <span style="color: #66cc66;">&#123;</span>
                <span style="color: #0066CC;">message</span>.<span style="color: #0066CC;">text</span> = <span style="color: #ff0000;">&quot;&quot;</span>;
                username.<span style="color: #0066CC;">text</span> = <span style="color: #ff0000;">&quot;&quot;</span>;
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
 <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&gt;&lt;/</span>fx:Script<span style="color: #66cc66;">&gt;</span>
    <span style="color: #66cc66;">&lt;</span>s:layout<span style="color: #66cc66;">&gt;</span>
        <span style="color: #66cc66;">&lt;</span>s:VerticalLayout paddingBottom=<span style="color: #ff0000;">&quot;5&quot;</span> paddingRight=<span style="color: #ff0000;">&quot;5&quot;</span> paddingLeft=<span style="color: #ff0000;">&quot;5&quot;</span> paddingTop=<span style="color: #ff0000;">&quot;5&quot;</span><span style="color: #66cc66;">/&gt;</span>
    <span style="color: #66cc66;">&lt;/</span>s:layout<span style="color: #66cc66;">&gt;</span>
    <span style="color: #66cc66;">&lt;</span>s:Label id=<span style="color: #ff0000;">&quot;username&quot;</span> <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;100%&quot;</span> maxDisplayedLines=<span style="color: #ff0000;">&quot;1&quot;</span> fontWeight=<span style="color: #ff0000;">&quot;bold&quot;</span><span style="color: #66cc66;">/&gt;</span>
    <span style="color: #66cc66;">&lt;</span>s:Label id=<span style="color: #ff0000;">&quot;message&quot;</span> <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;100%&quot;</span> maxDisplayedLines=<span style="color: #ff0000;">&quot;4&quot;</span> <span style="color: #66cc66;">/&gt;</span>
<span style="color: #66cc66;">&lt;/</span>s:ItemRenderer<span style="color: #66cc66;">&gt;</span></pre></td></tr></table></div>

<p>And there you have it. A list of tweets that mention Robotlegs. Glorious indeed! I would encourage you to play with this application a bit. Add a control bar to change the search terms. Add the ability to login, retrieve your timeline, and maybe post a Tweet. If you don't want to piss off all your nerd friends, start a new Twitter account to play with while you develop. I hear they have plenty of extras available.</p>
<p>Now that we have services, we've almost covered the full scope of tools for utilizing the Robotlegs MVC+S implementation. All that we have left to cover is Commands. Commands will let us decouple the code and provide re-usable "actions" that are triggered by events.</p>
<p>If you can't wait, there are articles <a href="http://joelhooks.com">on my blog</a> (and lots of others across the internets) dealing with various Robotlegs topics including <a href="http://joelhooks.com/2009/11/11/a-walkthrough-of-an-air-twitter-client-using-robotlegs-mvcs/">this 25 minute screencast</a>. John Lindquist has a <a href="http://johnlindquist.com/2009/11/18/robotlegs-hello-world-video-tutorial/">Hello World screencast</a> on his blog. Additionally there is a <a href="http://wiki.github.com/robotlegs/robotlegs-framework/best-practices">Best Practices document</a> that has proven helpful for many. You can always hit the <a href="http://knowledge.robotlegs.org/">Robotlegs Knowledge Base</a> for help and support. It has an active group of community volunteers, including myself, that diligently answer questions regarding all things Robotlegs. Additionally I participated in writing Flex 4 in Action, which has 22 pages of Robotlegs material.</p>
<ul>
<li>
      <a href="http://joelhooks.com/2011/03/12/an-introduction-to-robotlegs-as3-part-1-context-and-mediators/">Part One: Context and Mediators</a>
    </li>
<li>
      <a href="http://joelhooks.com/2011/03/12/an-introduction-to-robotlegs-as3-part-2-models/">Part Two: Models</a>
    </li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://joelhooks.com/2011/03/12/an-introduction-to-robotlegs-as3-part-3-services/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
	</item>
		<item>
		<title>An Introduction to Robotlegs AS3 Part 1: Context and Mediators</title>
		<link>http://joelhooks.com/2011/03/12/an-introduction-to-robotlegs-as3-part-1-context-and-mediators/</link>
		<comments>http://joelhooks.com/2011/03/12/an-introduction-to-robotlegs-as3-part-1-context-and-mediators/#comments</comments>
		<pubDate>Sat, 12 Mar 2011 15:51:57 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[robotlegs]]></category>

		<guid isPermaLink="false">http://joelhooks.com/?p=589</guid>
		<description><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top: 60%;
left: 5px;
}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;

padding-bottom:2px;
}


#bottomcontainerBox {
height: 30px;
width:50%;
padding-top:1px;
}

#bottomcontainerBox .buttons {
float:left;
height: 30px;
margin:4px 4px 4px 4px;
}

</style>
We are presented with lots of choices these days when it comes to frameworks for Actionscript 3 development. This is a good sign. The open source community is alive and vibrant, and tools that make development easier are a good thing. Over the course of the last year Robotlegs AS3 has seen rapid growth in [...]]]></description>
			<content:encoded><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top: 60%;
left: 5px;
}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;

padding-bottom:2px;
}


#bottomcontainerBox {
height: 30px;
width:50%;
padding-top:1px;
}

#bottomcontainerBox .buttons {
float:left;
height: 30px;
margin:4px 4px 4px 4px;
}

</style>
<p>We are presented with lots of choices these days when it comes to frameworks for Actionscript 3 development. This is a good sign. The open source community is alive and vibrant, and tools that make development easier are a good thing. Over the course of the last year <a href="http://www.robotlegs.org">Robotlegs AS3</a> has seen rapid growth in adoption. It is being leveraged by major media corporations, independent game developers, startups, and enterprises of all sizes.</p>
<p><span class="mt-enclosure mt-enclosure-image" style="display: inline;"><img alt="robotlegs.jpg" src="http://www.developria.com/2010/06/03/robotlegs.jpg" width="157" height="164" class="mt-image-right" style="float: right; margin: 0 0 20px 20px;" /></span> </p>
<p>This is the first in a series of Robotlegs articles that will appear here on InsideRIA. In the coming weeks, more articles in the series will detail core Robotlegs concepts and work through some more advanced concepts involving third party utilities and libraries built to interact with them.</p>
<p>This article uses Flex. Don't worry straight AS3 developers, the second part uses an example that is pure AS3.</p>
<h2>What is Robotlegs?</h2>
<p>Simply put, Robotlegs is a mechanism for wiring your objects together. ObjectA needs to talk to ObjectB. ObjectA doesn't want, or need, to know that ObjectB even exists. How can they communicate? The simple answer is through Events. With Flash, we have a native event system that is used to facilitate this sort of communication. Likely you use Events on a daily basis. Objects on the display list communicate via events, and event bubbling allows distant objects to receive messages from other display objects. What about objects that aren't on the display list? This is where a framework such as Robotlegs can really make life easier for you.</p>
<p>At its core, Robotlegs is a set of modular utilities and interfaces that provide tools to ease these communication tasks, reduce repetitive coding tasks, and manage <a href="http://www.developria.com/2009/09/as3-dependency-injection-demys.html">dependency injection</a> within your application. In addition to this core set of tools, Robotlegs provides a lightly prescriptive MVC+S (<a href="http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller">Model, View, Controller</a>, and Service) implementation to get you started. If you have any experience with PureMVC you will quickly recognize the use of Mediators and Commands within the Robotlegs MVC+S implementation. If not, don't worry, we will be looking at these classes in more depth soon.</p>
<p>This article is going to give an overview of Robotlegs through a simple "Hello World" example. You will probably look at the example and say, "Eh? I could have done this in a single MXML file without all the hassle!" While probably true with a trivial example, keep in mind that on a large project this structure quickly becomes invaluable. That is the benefit of development with a framework in general. It allows us to effectively communicate concepts and understand a code base much quicker than a slapped together application with no patterns and practices.</p>
<p>This is not going an exhaustive dissection of Robotlegs, but hopefully it is enough to spark your interest. I've posted resources at the end of the article for further investigation. That said, let's look at some code!</p>
<p><span id="more-589"></span></p>
<h2>Basic Structure of a Robotlegs MVC+S Application</h2>
<p><b>A typical Robotlegs MVC+S application is composed of several parts:</b> </p>
<ul>
<li>Context - the Context is the bootstrapping mechanism that initializes dependency injection and the various core utilities that Robotlegs uses.</li>
<li>Mediators - Mediators manage communication between your application's view components and other objects within your application.</li>
<li>Commands - Commands represent individual actions that your application can perform. These are typically in response to user activity, but are not limited to that use case.</li>
<li>Models -  Models store data and represent the current state of your application.</li>
<li>Services - Services are your application's gateway to the outside world.</li>
</ul>
<p>Let's take a look at the Context and Mediators in more depth, starting with the Context.</p>
<p>The Context is at the heart of your application. It provides the central event bus that your other application objects will utilize to communicate with one another. Beyond the initial loading and bootstrapping of your application, you won't deal with the Context during regular development. It comes to life, does its job, and then quietly gets out of your way while you develop your heart out. The Context is not a Singleton. Your application can have any number of Contexts, which makes Robotlegs well suited for modular applications. We aren't going to explore modular applications here, but it will be the subject of a future article as it is an extremely powerful tool. To start off, let's look at the most basic Context.<br />
<h3>HelloWorldContext.as</h3>

<div class="wp_codebox"><table><tr id="p58944"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p589code44"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">import</span> org.<span style="color: #006600;">robotlegs</span>.<span style="color: #006600;">mvcs</span>.<span style="color: #006600;">Context</span>
&nbsp;
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HelloWorldContext <span style="color: #0066CC;">extends</span> Context
<span style="color: #66cc66;">&#123;</span>
    override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> startup<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #808080; font-style: italic;">//bootstrap here</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Inside of your Context you override the startup method. The startup() method is called when the Context has been fully initialized. Behind the scenes, prior to calling startup(), the Context is creating instances of all the core Robotlegs utilities, preparing to receive dependency injection mappings, and creating the event dispatcher that will be used to communicate amongst your application's objects.</p>
<p>Once your Context class has been created, your application needs a reference to it. In a Flex 4 Spark Application, this is generally done in the main MXML file that extends Application as seen below.</p>
<h3>HelloWorld.mxml</h3>

<div class="wp_codebox"><table><tr id="p58945"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p589code45"><pre class="actionscript" style="font-family:monospace;"><span style="color: #66cc66;">&lt;</span>?<span style="color: #0066CC;">xml</span> <span style="color: #0066CC;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span>?<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;</span>s:Application
        xmlns:fx=<span style="color: #ff0000;">&quot;http://ns.adobe.com/mxml/2009&quot;</span>
        xmlns:s=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/spark&quot;</span>
        xmlns:mx=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/mx&quot;</span> 
        xmlns:local=<span style="color: #ff0000;">&quot;*&quot;</span><span style="color: #66cc66;">&gt;</span>
    <span style="color: #66cc66;">&lt;</span>fx:Declarations<span style="color: #66cc66;">&gt;</span>
        <span style="color: #66cc66;">&lt;</span>local:HelloWorldContext contextView=<span style="color: #ff0000;">&quot;{this}&quot;</span><span style="color: #66cc66;">/&gt;</span>
    <span style="color: #66cc66;">&lt;/</span>fx:Declarations<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;/</span>s:Application<span style="color: #66cc66;">&gt;</span></pre></td></tr></table></div>

<p>Because the Context is a non-visual class, it must be placed in the Declarations tag. You should also note the contextView property bound to the application itself. The context view is the root view of the Context, and is used to provide automated assistance with view component mediation. We will look at that shortly when we discuss the relationships between views and mediators.</p>
<p>That is it for the Context. As mentioned, it has a very brief, but critical, role in the lifecycle of your application. With your Context ready, we can now add a few view components and get them speaking to one another via Robotlegs. Let's take a look at Mediators and their relationship to your application's view components now.</p>
<p>Mediators sit between your view components and the rest of the application. Put simply, Mediators listen for events. Your view components dispatch events when the user interacts with them or they are updated in some other way. These events need to be captured and delivered to the rest of the application. Perhaps the user has clicked a save button and some information needs to get sent to a server. The mediator listens for this event to occur, and when it does, the mediator gathers the appropriate information and sends an event that the rest of the application can utilize to perform some unit of work on the data. Likewise, a Mediator also listens to events from the rest of the application. If some data has been received from the server, parsed, and an event dispatched from a service class, the Mediator is the place where you will listen for that event and update its view component with the new data. Here is a view that will receive a Mediator.</p>
<h3>MessageView.mxml</h3>

<div class="wp_codebox"><table><tr id="p58946"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p589code46"><pre class="actionscript" style="font-family:monospace;"><span style="color: #66cc66;">&lt;</span>?<span style="color: #0066CC;">xml</span> <span style="color: #0066CC;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span>?<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;</span>s:TextArea
        xmlns:fx=<span style="color: #ff0000;">&quot;http://ns.adobe.com/mxml/2009&quot;</span>
        xmlns:s=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/spark&quot;</span>
        xmlns:mx=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/mx&quot;</span><span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;/</span>s:TextArea<span style="color: #66cc66;">&gt;</span></pre></td></tr></table></div>

<p>This class is short. It is nothing more than a simple extension of a TextArea. Why not just use a TextArea? Dependency injection works better with unambiguous classes. What this means is that by extending TextArea into our new MessageView class, we are creating a specific view component for the dependency injection to act upon. This is important if our application were to have several TextAreas that served different purposes. By dividing up our classes in this way, we are clearly defining the intent of the class and allowing for the dependency injection tools to do their jobs effectively. With the MessageView, we might also add some additional functionality in the future. For this example, it will remain a simple TextArea, but you get the idea. Now we'll look at the mediator for the MessageView component.</p>
<h3>MessageViewMediator.as</h3>

<div class="wp_codebox"><table><tr id="p58947"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p589code47"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">import</span> org.<span style="color: #006600;">robotlegs</span>.<span style="color: #006600;">mvcs</span>.<span style="color: #006600;">Mediator</span>;
&nbsp;
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MessageViewMediator <span style="color: #0066CC;">extends</span> Mediator
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #66cc66;">&#91;</span>Inject<span style="color: #66cc66;">&#93;</span>
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> view:MessageView;
&nbsp;
    override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> onRegister<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
    <span style="color: #66cc66;">&#123;</span>
         <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;I am registered!&quot;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The MessageViewMediator has two interesting features at this point. Right away, you will notice the first use of the [Inject] metadata tag. This tag is used by Robotlegs to identify properties and methods that it needs to perform injection on. With a mediator, the mediated view is always available for injection when the mediator is created. You don't need to make any special consideration for mapping the view for injection. That is taken care of for you when you map the view to its mediator. We will look at that in a moment, but first let's take a look at the second interesting feature of the basic mediator which is the onRegister() method.</p>
<p>The onRegister() method is the hook provided to you when the mediator has been fully initialized. The injections have occurred, the view is ready, and it is where you typically add your event listeners for both the view component and the application. You will typically override this method in every Robotlegs mediator you create.</p>
<p>Now that you have a view component and a mediator, they need to be registered, or mapped, with the Context. This is achieved through the MediatorMap. As the name suggests, the MediatorMap is a utility for mapping mediators to view components within the Context. In addition the MediatorMap by default listens to the contextView for ADDED_TO_STAGE and REMOVED_FROM_STAGE events to automatically create and destroy mediators as their corresponding view components are added and removed from the display list. It is worth noting that in graphics intensive applications with many display objects (1000s), this automatic mediation can be a performance issue. In a typical application it is very convenient and rarely causes performance issues. Here is how we map the MessageView to its MessageViewMediator within the HelloWorldContext.</p>

<div class="wp_codebox"><table><tr id="p58948"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p589code48"><pre class="actionscript" style="font-family:monospace;">override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> startup<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
    mediatorMap.<span style="color: #006600;">mapView</span><span style="color: #66cc66;">&#40;</span>MessageView, MessageViewMediator<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>With that done, all that is left is to add the MessageView to the HelloWorld.mxml.</p>
<h3>HelloWorld.mxml</h3>

<div class="wp_codebox"><table><tr id="p58949"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p589code49"><pre class="actionscript" style="font-family:monospace;"><span style="color: #66cc66;">&lt;</span>?<span style="color: #0066CC;">xml</span> <span style="color: #0066CC;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span>?<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;</span>s:Application
        xmlns:fx=<span style="color: #ff0000;">&quot;http://ns.adobe.com/mxml/2009&quot;</span>
        xmlns:s=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/spark&quot;</span>
        xmlns:mx=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/mx&quot;</span>
        xmlns:local=<span style="color: #ff0000;">&quot;*&quot;</span><span style="color: #66cc66;">&gt;</span>
    <span style="color: #66cc66;">&lt;</span>fx:Declarations<span style="color: #66cc66;">&gt;</span>
        <span style="color: #66cc66;">&lt;</span>local:HelloWorldContext contextView=<span style="color: #ff0000;">&quot;{this}&quot;</span><span style="color: #66cc66;">/&gt;</span>
    <span style="color: #66cc66;">&lt;/</span>fx:Declarations<span style="color: #66cc66;">&gt;</span>
    <span style="color: #66cc66;">&lt;</span>local:MessageView top=<span style="color: #ff0000;">&quot;40&quot;</span> <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #0066CC;">height</span>=<span style="color: #ff0000;">&quot;100%&quot;</span><span style="color: #66cc66;">/&gt;</span>
<span style="color: #66cc66;">&lt;/</span>s:Application<span style="color: #66cc66;">&gt;</span></pre></td></tr></table></div>

<p>Now, when you run your application in the debugger, you will see "I am registered!" printed to the console. Congratulations, you have a fully functional Robotlegs application. Granted, that functionality is limited to almost nothing right now, but we can change that. To give our application something to do besides just bootstrap, we will add a button called HelloButton that simply extends the Spark Button class, as well as a mediator for HelloButton called... HelloButtonMediator. </p>
<h3>HelloButton.mxml</h3>

<div class="wp_codebox"><table><tr id="p58950"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p589code50"><pre class="actionscript" style="font-family:monospace;"><span style="color: #66cc66;">&lt;</span>?<span style="color: #0066CC;">xml</span> <span style="color: #0066CC;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span>?<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;</span>s:<span style="color: #0066CC;">Button</span>
        xmlns:fx=<span style="color: #ff0000;">&quot;http://ns.adobe.com/mxml/2009&quot;</span>
        xmlns:s=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/spark&quot;</span>
        xmlns:mx=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/mx&quot;</span><span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;/</span>s:Button<span style="color: #66cc66;">&gt;</span></pre></td></tr></table></div>

<h3>HelloButtonMediator.as</h3>

<div class="wp_codebox"><table><tr id="p58951"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code" id="p589code51"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
&nbsp;
<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">robotlegs</span>.<span style="color: #006600;">mvcs</span>.<span style="color: #006600;">Mediator</span>;
&nbsp;
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HelloButtonMediator <span style="color: #0066CC;">extends</span> Mediator
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #66cc66;">&#91;</span>Inject<span style="color: #66cc66;">&#93;</span>
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> view:HelloButton;
&nbsp;
    override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> onRegister<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
    <span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Right now the HelloButtonMediator looks exactly like the MessageViewMediator above, except the classes are different. Your Context startup method will look like this once you have added the mapping for the HelloButton and its mediator.</p>

<div class="wp_codebox"><table><tr id="p58952"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p589code52"><pre class="actionscript" style="font-family:monospace;">override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> startup<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
    mediatorMap.<span style="color: #006600;">mapView</span><span style="color: #66cc66;">&#40;</span>MessageView, MessageViewMediator<span style="color: #66cc66;">&#41;</span>;
    mediatorMap.<span style="color: #006600;">mapView</span><span style="color: #66cc66;">&#40;</span>HelloButton, HelloButtonMediator<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>You will also add the button to HelloWorld.mxml so that it will be added to the display list. You will probably want to add something wittier to the HelloButton's label property, but I will leave that up to you. </p>
<h3>HelloWorld.mxml</h3>

<div class="wp_codebox"><table><tr id="p58953"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p589code53"><pre class="actionscript" style="font-family:monospace;"><span style="color: #66cc66;">&lt;</span>?<span style="color: #0066CC;">xml</span> <span style="color: #0066CC;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span>?<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;</span>s:Application
        xmlns:fx=<span style="color: #ff0000;">&quot;http://ns.adobe.com/mxml/2009&quot;</span>
        xmlns:s=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/spark&quot;</span>
        xmlns:mx=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/mx&quot;</span>
        xmlns:local=<span style="color: #ff0000;">&quot;*&quot;</span><span style="color: #66cc66;">&gt;</span>
    <span style="color: #66cc66;">&lt;</span>fx:Declarations<span style="color: #66cc66;">&gt;</span>
        <span style="color: #66cc66;">&lt;</span>local:HelloWorldContext contextView=<span style="color: #ff0000;">&quot;{this}&quot;</span><span style="color: #66cc66;">/&gt;</span>
    <span style="color: #66cc66;">&lt;/</span>fx:Declarations<span style="color: #66cc66;">&gt;</span>
    <span style="color: #66cc66;">&lt;</span>local:HelloButton label=<span style="color: #ff0000;">&quot;Say Hello&quot;</span><span style="color: #66cc66;">/&gt;</span>
    <span style="color: #66cc66;">&lt;</span>local:MessageView top=<span style="color: #ff0000;">&quot;40&quot;</span> <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #0066CC;">height</span>=<span style="color: #ff0000;">&quot;100%&quot;</span><span style="color: #66cc66;">/&gt;</span>
<span style="color: #66cc66;">&lt;/</span>s:Application<span style="color: #66cc66;">&gt;</span></pre></td></tr></table></div>

<p>At this point we have two fully mediated view components that are just dying to talk. I'm not one to deny my objects their desires, so lets do just that; starting with the custom event they will use to communicate.</p>
<h3>HelloWorldMessageEvent.as</h3>

<div class="wp_codebox"><table><tr id="p58954"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code" id="p589code54"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HelloWorldMessageEvent <span style="color: #0066CC;">extends</span> Event
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">static</span> const MESSAGE_DISPATCHED:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;messageDispatched&quot;</span>;
&nbsp;
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _message:<span style="color: #0066CC;">String</span>;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> <span style="color: #0066CC;">message</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">return</span> _message;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> HelloWorldMessageEvent<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">type</span>:<span style="color: #0066CC;">String</span>, <span style="color: #0066CC;">message</span>:<span style="color: #0066CC;">String</span>, bubbles:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">false</span>, cancelable:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">type</span>, bubbles, cancelable<span style="color: #66cc66;">&#41;</span>;
        _message = <span style="color: #0066CC;">message</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> clone<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Event
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> HelloWorldMessageEvent<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">type</span>, <span style="color: #0066CC;">message</span>, bubbles, cancelable<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>This is a simple custom event. Be sure to override the clone() method in your custom events. Events cannot be redispatched, relayed, or bubbled without this method. I make it a habit to always override clone() in all my custom events. After being burned with a couple hours of head scratching debugging, you will too.</p>
<p>What we want to do is update the MessageView when the user clicks the HelloButton. The HelloButtonMediator needs to listen for the MouseEvent.CLICK on the HelloButton and then dispatch the HelloWorldMessageEvent to the application. It doesn't know who will respond to this event. It doesn't care who responds to the event. The HelloButtonMediator has done its job.</p>
<h3>HelloButtonMediator.as</h3>

<div class="wp_codebox"><table><tr id="p58955"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code" id="p589code55"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HelloButtonMediator <span style="color: #0066CC;">extends</span> Mediator
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #66cc66;">&#91;</span>Inject<span style="color: #66cc66;">&#93;</span>
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> view:HelloButton;
&nbsp;
    override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> onRegister<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
    <span style="color: #66cc66;">&#123;</span>
        addViewListener<span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, handleMouseClick<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> handleMouseClick<span style="color: #66cc66;">&#40;</span>event:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
    <span style="color: #66cc66;">&#123;</span>
        dispatch<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> HelloWorldMessageEvent<span style="color: #66cc66;">&#40;</span>HelloWorldMessageEvent.<span style="color: #006600;">MESSAGE_DISPATCHED</span>, <span style="color: #ff0000;">&quot;Hello World&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>With the view listener added to the HelloButtonMediator, we are now dispatching an event to the application. The next step is to do something with that event. The MessageViewMediator seems like the logical choice.</p>
<h3>MessageViewMediator.as</h3>

<div class="wp_codebox"><table><tr id="p58956"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code" id="p589code56"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MessageViewMediator <span style="color: #0066CC;">extends</span> Mediator
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #66cc66;">&#91;</span>Inject<span style="color: #66cc66;">&#93;</span>
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> view:MessageView;
&nbsp;
    override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> onRegister<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
    <span style="color: #66cc66;">&#123;</span>
        addContextListener<span style="color: #66cc66;">&#40;</span>HelloWorldMessageEvent.<span style="color: #006600;">MESSAGE_DISPATCHED</span>, handleMessage<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> handleMessage<span style="color: #66cc66;">&#40;</span>event:HelloWorldMessageEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
    <span style="color: #66cc66;">&#123;</span>
        view.<span style="color: #0066CC;">text</span> = event.<span style="color: #0066CC;">message</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>...and with that, we have a Hello World! Seems like the long way to get there, but it will pay off in your non-trivial applications. In the next part in this series we will explore models and commands followed by an article on services. In addition to the core of Robotlegs, we will look in detail at some of the utilities that are currently available to make use of tools such as <a href="http://github.com/robertpenner/as3-signals">AS3-Signals</a> and modular application development. It is an exciting time to be developing with Robotlegs and I look forward to sharing some of my enthusiasm here on InsideRIA over the coming weeks.</p>
<p>If you can't wait, there are articles <a href="http://joelhooks.com">on my blog</a> (and lots of others across the internets) dealing with various Robotlegs topics. John Lindquist has a <a href="http://johnlindquist.com/2009/11/18/robotlegs-hello-world-video-tutorial/">Hello World screencast</a> on his blog (watching him use FDT is interesting in and of itself). Additionally there is a <a href="http://wiki.github.com/robotlegs/robotlegs-framework/best-practices">Best Practices document</a> that has proven helpful for many. You can always hit the <a href="http://knowledge.robotlegs.org/">Robotlegs Knowledge Base</a> for help and support. It has an active group of community volunteers, including myself, that diligently answer questions regarding all things Robotlegs. Additionally I participated in writing the forthcoming Flex 4 in Action book, which has 22 pages of Robotlegs material.</p>
<p><a href="http://joelhooks.com/2011/03/12/an-introduction-to-robotlegs-as3-part-2-models/">Part 2: Models can be found here.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://joelhooks.com/2011/03/12/an-introduction-to-robotlegs-as3-part-1-context-and-mediators/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
	</item>
		<item>
		<title>360&#124;Flex Denver 2011</title>
		<link>http://joelhooks.com/2011/01/30/360flex-denver-2011/</link>
		<comments>http://joelhooks.com/2011/01/30/360flex-denver-2011/#comments</comments>
		<pubDate>Mon, 31 Jan 2011 00:02:57 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[conference]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[speaking]]></category>

		<guid isPermaLink="false">http://joelhooks.com/?p=582</guid>
		<description><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top: 60%;
left: 5px;
}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;

padding-bottom:2px;
}


#bottomcontainerBox {
height: 30px;
width:50%;
padding-top:1px;
}

#bottomcontainerBox .buttons {
float:left;
height: 30px;
margin:4px 4px 4px 4px;
}

</style>
I'm incredibly humbled and excited to be speaking at 360Flex this year in Denver, CO. Last year in San Jose was my first trip to the event and it was really a great experience. 3 days jam packed full of learning and connecting with fellow developers. This year I am going to be delivering an [...]]]></description>
			<content:encoded><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top: 60%;
left: 5px;
}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;

padding-bottom:2px;
}


#bottomcontainerBox {
height: 30px;
width:50%;
padding-top:1px;
}

#bottomcontainerBox .buttons {
float:left;
height: 30px;
margin:4px 4px 4px 4px;
}

</style>
<p><a href="http://www.360flex.com/register/"><img src="http://joelhooks.com/wp-content/uploads/2011/01/360FlexBadge2011.png" alt="" title="360FlexBadge2011" width="180" height="176" class="alignright size-full wp-image-583" /></a>I'm incredibly humbled and excited to be speaking at <a href="http://www.360flex.com/register/">360Flex this year</a> in Denver, CO. Last year in San Jose was my first trip to the event and it was really a great experience. 3 days jam packed full of learning and connecting with fellow developers.</p>
<p>This year I am going to be delivering an introduction to the Parsley framework. I've been using Parsley extensively for the last 7 months and have really enjoyed the experience. So if you have a hand in building RIAs large enough to need a robust modular framework, this talk will likely give you an idea of how Parsley might fit that bill.</p>
<p>More than the knowledge transfer at these events, I really love the connections that it allows. In my day to day life I feel a bit isolated, and while I truly love working from my home office, gatherings like 360|Flex fill a much needed nerd-to-nerd void. So <a href="http://www.360flex.com/register/">get your tickets</a> and lets connect at 360|Flex!</p>
]]></content:encoded>
			<wfw:commentRss>http://joelhooks.com/2011/01/30/360flex-denver-2011/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
	</item>
		<item>
		<title>IntelliJ Idea Flex and Actionscript 3 Workflow: [Part 1] Creating a Flex Project</title>
		<link>http://joelhooks.com/2010/03/21/intellij-idea-flex-and-actionscript-3-workflow-part-1-creating-a-flex-project/</link>
		<comments>http://joelhooks.com/2010/03/21/intellij-idea-flex-and-actionscript-3-workflow-part-1-creating-a-flex-project/#comments</comments>
		<pubDate>Mon, 22 Mar 2010 03:08:50 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[dev tools]]></category>
		<category><![CDATA[flex]]></category>

		<guid isPermaLink="false">http://joelhooks.com/?p=463</guid>
		<description><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top: 60%;
left: 5px;
}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;

padding-bottom:2px;
}


#bottomcontainerBox {
height: 30px;
width:50%;
padding-top:1px;
}

#bottomcontainerBox .buttons {
float:left;
height: 30px;
margin:4px 4px 4px 4px;
}

</style>
I've been using IntelliJ Idea 9 quite a bit in both production and personal work lately. It is a great IDE, with lots of useful features. This is going to be a series of short screencasts that highlight the workflow in Idea for Flex/Actionscript development. The first is a quick introduction and how to set [...]]]></description>
			<content:encoded><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top: 60%;
left: 5px;
}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;

padding-bottom:2px;
}


#bottomcontainerBox {
height: 30px;
width:50%;
padding-top:1px;
}

#bottomcontainerBox .buttons {
float:left;
height: 30px;
margin:4px 4px 4px 4px;
}

</style>
<p>I've been using IntelliJ Idea 9 quite a bit in both production and personal work lately. It is a great IDE, with lots of useful features. This is going to be a series of short screencasts that highlight the workflow in Idea for Flex/Actionscript development. The first is a quick introduction and how to set up a Flex project. If you have any questions or requests for future videos in the series please feel free to leave them in the comments.</p>
<p><a href="http://joelhooks.com/video/intro.mov" rel="shadowbox;height=664;width=994"><img src="http://69.164.207.135/wp-content/uploads/2010/03/idea_flex_part1.png" alt="" title="idea_flex_part1" width="450" height="301" class="aligncenter size-full wp-image-464" /></a></p>
<p><a href="http://joelhooks.com/video/intro.mov">direct link to the video (42mb QT)</a></p>
<h3>Resources</h3>
<p><a href="http://confluence.jetbrains.net/display/IDEADEV/Maia+EAP">IntelliJ IDEA EAP Download Page</a><br />
<a href="http://www.insideria.com/2010/02/intellij-idea9-actionscript-3f.html">Jesse Freeman's IDEA Workflow series on InsideRIA</a></p>
]]></content:encoded>
			<wfw:commentRss>http://joelhooks.com/2010/03/21/intellij-idea-flex-and-actionscript-3-workflow-part-1-creating-a-flex-project/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
<enclosure url="http://joelhooks.com/video/intro.mov" length="44160652" type="video/quicktime" />
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
	</item>
		<item>
		<title>Robotlegs, AS3-Signals and the SignalCommandMap Example</title>
		<link>http://joelhooks.com/2010/02/14/robotlegs-as3-signals-and-the-signalcommandmap-example/</link>
		<comments>http://joelhooks.com/2010/02/14/robotlegs-as3-signals-and-the-signalcommandmap-example/#comments</comments>
		<pubDate>Sun, 14 Feb 2010 21:39:53 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[object oriented concepts]]></category>
		<category><![CDATA[robotlegs]]></category>
		<category><![CDATA[software architecture]]></category>

		<guid isPermaLink="false">http://joelhooks.com/?p=432</guid>
		<description><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top: 60%;
left: 5px;
}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;

padding-bottom:2px;
}


#bottomcontainerBox {
height: 30px;
width:50%;
padding-top:1px;
}

#bottomcontainerBox .buttons {
float:left;
height: 30px;
margin:4px 4px 4px 4px;
}

</style>
Robotlegs and AS3-Signals play really well together. Both apply solid object-oriented principles to accomplish their respective goals. Signals is extremely well suited for automated Dependency Injection. By combining Signals and Robotlegs you are able to eliminate the use of Flash Events in the framework layer of your application. Eliminating Events means eliminating the ambiguity that [...]]]></description>
			<content:encoded><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top: 60%;
left: 5px;
}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;

padding-bottom:2px;
}


#bottomcontainerBox {
height: 30px;
width:50%;
padding-top:1px;
}

#bottomcontainerBox .buttons {
float:left;
height: 30px;
margin:4px 4px 4px 4px;
}

</style>
<p><a href="http://robotlegs.org">Robotlegs</a> and <a href="http://github.com/robertpenner/as3-signals">AS3-Signals</a> play really well together. Both apply solid object-oriented principles to accomplish their respective goals. Signals is extremely well suited for automated Dependency Injection. By combining Signals and Robotlegs you are able to eliminate the use of Flash Events in the framework layer of your application. Eliminating Events means eliminating the ambiguity that can accompany Events and their String registry based approach to the <a href="http://en.wikipedia.org/wiki/Observer_pattern">Observer pattern</a>. Signals provides a strongly-typed object-oriented approach to this same pattern.</p>
<p>With the standard <a href="http://wiki.github.com/robotlegs/robotlegs-framework/best-practices">Robotlegs MVCS implementation</a> you leverage the events provided by Actionscript 3 to communicate amongst the various actors of an application. From models and services dispatching notifications of their actions to triggering commands, events are a core piece of the implementation. To facilitate the use of Signals within MVCS it was necessary to create an extension to allow for Signals to be registered as Command triggers. This need spawned the <a href="http://github.com/joelhooks/signals-extensions-CommandSignal">SignalCommandMap utility</a>.</p>
<p>The SignalCommandMap extends the normal MVCS context and creates a SignalContext. The SignalContext instantiates and provides access to the SignalCommandMap alongside the other maps that are standard to Robotlegs. The SignalCommandMap allows you to map Signal classes and instances to commands that will be executed when the Signal dispatch() method is called. The value objects that are passed in the dispatch are then injected into the command alongside any other mapped injections you have created.</p>
<p>Let's take a look at a simple example that makes use of the SignalCommandMap and discuss some of the underlying code to see how it works:</p>
<div align="center">
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_index_1870259537"
			class="flashmovie"
			width="320"
			height="550">
	<param name="movie" value="http://joelhooks.com/examples/robotlegsSignalsCafe/index.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://joelhooks.com/examples/robotlegsSignalsCafe/index.swf"
			name="fm_index_1870259537"
			width="320"
			height="550">
	<!--<![endif]-->
		
<p><a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object></div>
<p><a href="http://github.com/joelhooks/robotlegs-examples-RobotlegsSignalsCafe">Source available on Github</a> (<a href="http://github.com/joelhooks/robotlegs-examples-RobotlegsSignalsCafe/zipball/v0.2.4212342">zip</a>)<br />
<span id="more-432"></span><br />
The example is a menu that allows you to add food which is displayed in a list. The total cost of your selected items is displayed and you can remove items from your order. First let's get started by taking a look at the context of the Application, which extends SignalContext:</p>
<h6>SignalCafeContext.as Bootstraps the Application</h6>

<div class="wp_codebox"><table><tr id="p43261"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code" id="p432code61"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SignalCafeContext <span style="color: #0066CC;">extends</span> SignalContext
<span style="color: #66cc66;">&#123;</span>
    override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> startup<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
    <span style="color: #66cc66;">&#123;</span>
      injector.<span style="color: #006600;">mapSingleton</span><span style="color: #66cc66;">&#40;</span>FoodOrderModel<span style="color: #66cc66;">&#41;</span>;
      injector.<span style="color: #006600;">mapSingleton</span><span style="color: #66cc66;">&#40;</span>FoodOrderUpdated<span style="color: #66cc66;">&#41;</span>;
      injector.<span style="color: #006600;">mapSingleton</span><span style="color: #66cc66;">&#40;</span>FoodItemAddedToOrder<span style="color: #66cc66;">&#41;</span>;
&nbsp;
      signalCommandMap.<span style="color: #006600;">mapSignalClass</span><span style="color: #66cc66;">&#40;</span>AddFoodItemToOrder, AddFoodItemToOrderCommand<span style="color: #66cc66;">&#41;</span>;
      signalCommandMap.<span style="color: #006600;">mapSignalClass</span><span style="color: #66cc66;">&#40;</span>FoodItemSelected, FoodItemSelectedCommand<span style="color: #66cc66;">&#41;</span>;
      signalCommandMap.<span style="color: #006600;">mapSignalClass</span><span style="color: #66cc66;">&#40;</span>RemoveAllOfSelectedItem, RemoveAllSelectedItemCommand<span style="color: #66cc66;">&#41;</span>;
      signalCommandMap.<span style="color: #006600;">mapSignalClass</span><span style="color: #66cc66;">&#40;</span>NoFoodItemSelected, NoFoodItemSelectedCommand<span style="color: #66cc66;">&#41;</span>;
&nbsp;
      mediatorMap.<span style="color: #006600;">mapView</span><span style="color: #66cc66;">&#40;</span>FoodSelectionView, FoodSelectionViewMediator<span style="color: #66cc66;">&#41;</span>;
      mediatorMap.<span style="color: #006600;">mapView</span><span style="color: #66cc66;">&#40;</span>CurrentOrderView, CurrentOrderViewMediator<span style="color: #66cc66;">&#41;</span>;
      mediatorMap.<span style="color: #006600;">mapView</span><span style="color: #66cc66;">&#40;</span>FoodOrderSummaryView, FoodOrderSummaryViewMediator<span style="color: #66cc66;">&#41;</span>;
      mediatorMap.<span style="color: #006600;">mapView</span><span style="color: #66cc66;">&#40;</span>FoodItemRemovalView, FoodItemRemovalViewMediator<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>A SignalContext is structured exactly the same as a standard Robotlegs MVCS Context class. You override the startup() method and bootstrap your application. The major difference here is that you are mapping signals to commands instead of mapping events to commands. With the SignalCommandMap you are not restricted to just using Signals for triggering commands. You could mix events and Signals liberally as your needs or requirements dictated. As a warning, this could quickly became confusing and choosing one or the other might be a saner choice.</p>
<p>SignalCafeContext is mapping 4 Signals to 4 commands. The Signals are typed extensions of the base Signal class. This is necessary to provide Robotlegs (and more specifically in this case SwiftSuspenders) object types to differentiate for the purposes of injection. You could use named injections, but this would rapidly riddle your application with strings that would need to be laboriously checked for accuracy. Avoiding strings in favor of compiler-checked object types is always a good choice.</p>
<p>The strongly typed Signals are relatively simple. They contain a constructor and no additional methods or properties are added to the Signal. You can of course add properties and methods to the extended Signals,, but it wasn't necessary for this example. The SignalCommandMap will accept any class that implements the ISignal interface. Let's take a look at one of the commands triggered by a Signal mapped in the SignalCafeContext:</p>
<h6>AddFoodItemToOrder.as triggers the AddFoodItemToOrderCommand</h6>

<div class="wp_codebox"><table><tr id="p43262"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p432code62"><pre class="actionscirpt" style="font-family:monospace;">public class AddFoodItemToOrder extends Signal
{
	public function AddFoodItemToOrder()
	{
		super(FoodType);
	}
}</pre></td></tr></table></div>

<p>That is the whole of the extended Signal. As mentioned, it is not very complex at all. You will notice that super(FoodType) is being called. Signals take constructor arguments consisting of classes or interfaces that will be used as the value objects transmitted via the dispatch() method. In this case we are using FoodType as this value to ensure that our Signal will carry the payload that the mapped command is expecting.</p>
<p>Command signals are named a bit differently than standard reaction Signals. A Signal that is triggering a command is "requesting action" or specifying an action that needs to occur. Standard signals are typically past tense, or informative, describing an action that has occurred. By using this standard it is much easier to differentiate quickly what each Signal's purpose is. This can help with overall clarity within your application.</p>
<p>The AddFoodItemToOrder signal is dispatched by the FoodSelectionViewMediator which is mediating the DropDownList of FoodTypes and the "Add Some Food" button:</p>
<h6>FoodSelectionViewMediator.as mediates a view and dispatches a Signal when the user has clicked the "Add Some Food" button</h6>

<div class="wp_codebox"><table><tr id="p43263"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code" id="p432code63"><pre class="actionscript" style="font-family:monospace;">	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FoodSelectionViewMediator <span style="color: #0066CC;">extends</span> Mediator
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #66cc66;">&#91;</span>Inject<span style="color: #66cc66;">&#93;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> view:FoodSelectionView;
&nbsp;
		<span style="color: #66cc66;">&#91;</span>Inject<span style="color: #66cc66;">&#93;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> addItem:AddFoodItemToOrder;
&nbsp;
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> onRegister<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			view.<span style="color: #006600;">itemTypeAdded</span>.<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span>handleItemTypeAdded<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> handleItemTypeAdded<span style="color: #66cc66;">&#40;</span>itemType:FoodType<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			addItem.<span style="color: #006600;">dispatch</span><span style="color: #66cc66;">&#40;</span>itemType<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>When the AddFoodItemToOrder Signal's dispatch(aFoodTypeInstance) is called the AddFoodItemToOrderCommand is triggered:</p>
<h6>AddFoodItemToOrderCommand.as is executed in response to AddFoodItemToOrder Signal dispatch</h6>

<div class="wp_codebox"><table><tr id="p43264"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p432code64"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> AddFoodItemToOrderCommand <span style="color: #0066CC;">extends</span> SignalCommand
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #66cc66;">&#91;</span>Inject<span style="color: #66cc66;">&#93;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> itemType:FoodType;
&nbsp;
	<span style="color: #66cc66;">&#91;</span>Inject<span style="color: #66cc66;">&#93;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> model:FoodOrderModel;
&nbsp;
	override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> execute<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
	<span style="color: #66cc66;">&#123;</span>
		model.<span style="color: #006600;">addItemToOrder</span><span style="color: #66cc66;">&#40;</span>itemType<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>AddFoodItemToOrderCommand has two public properties that are marked for injection. The itemType property is a FoodType object. As you will recall, FoodType is the parameter that was passed to the AddFoodItemToOrder Signal super constructor. The AddFoodItemToOrder Signal dispatch() included an instance of a FoodType object which will be injected into the AddFoodItemToOrderCommand alongside any other injections that have been mapped and specified. In this case we are also injecting the FoodOrderModel instance that was mapped as a singleton in the SignalCafeContext.</p>
<div class="note-block">It is important to note that the parameters passed via the Signals dispatch() method will be instantly mapped and unmapped when the command is executed. If you've previously mapped a class that is being delivered via a Signal command mapping, that mapping will be overwritten and removed. To avoid this, favor using typed value objects as Signal parameters.</div>
<p>The AddFoodItemToOrderCommand has a simple job. It accesses the FoodOrderModel instance and calls its addItemToOrder method adding the FoodType parameter that was dispatched with the Signal to trigger the command.</p>
<p>That covers the basics of using the SignalCommandMap within a Robotlegs application. There are options that this example doesn't cover. I would recommend looking through the SignalCommandMap unit test suite for a thorough review of its capabilities. There are several more commands and Signals in the example for you to look over as well. Additionally the example uses Signals within views and the model to dispatch notifications. Outside of binding and UI events, no events are being passed within the application.</p>
<p>Hopefully this will give you the basic understanding of using the SignalCommandMap in a Robotlegs application. Let me know if you have any criticisms, comments, suggestions, or ideas regarding the utility's implementation at <a href="http://knowledge.robotlegs.org">http://knowledge.robotlegs.org</a></p>
]]></content:encoded>
			<wfw:commentRss>http://joelhooks.com/2010/02/14/robotlegs-as3-signals-and-the-signalcommandmap-example/feed/</wfw:commentRss>
		<slash:comments>54</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
	</item>
		<item>
		<title>Robotlegs Image Gallery Example using AS3-Signals and the Presentation Model</title>
		<link>http://joelhooks.com/2010/01/16/robotlegs-image-gallery-example-using-as3-signals-and-the-presentation-model/</link>
		<comments>http://joelhooks.com/2010/01/16/robotlegs-image-gallery-example-using-as3-signals-and-the-presentation-model/#comments</comments>
		<pubDate>Sat, 16 Jan 2010 22:56:36 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[object oriented concepts]]></category>
		<category><![CDATA[robotlegs]]></category>

		<guid isPermaLink="false">http://joelhooks.com/?p=386</guid>
		<description><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top: 60%;
left: 5px;
}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;

padding-bottom:2px;
}


#bottomcontainerBox {
height: 30px;
width:50%;
padding-top:1px;
}

#bottomcontainerBox .buttons {
float:left;
height: 30px;
margin:4px 4px 4px 4px;
}

</style>
Jason Crist posted a thought provoking request for his upcoming presentation comparing Robotlegs and Swiz. He's got a clever knack for stirring the framework ant pile and getting developers eyes off their IDEs long enough to discuss their passions. In this case the developers include Ben Clinkinbeard, Shaun Smith, Jesse Warden and myself. These conversations [...]]]></description>
			<content:encoded><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top: 60%;
left: 5px;
}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;

padding-bottom:2px;
}


#bottomcontainerBox {
height: 30px;
width:50%;
padding-top:1px;
}

#bottomcontainerBox .buttons {
float:left;
height: 30px;
margin:4px 4px 4px 4px;
}

</style>
<p>Jason Crist <a href="http://pbking.com/blog/?p=209">posted a thought provoking request</a> for his upcoming presentation comparing <a href="http://www.robotlegs.org">Robotlegs</a> and <a href="http://swizframework.org/">Swiz</a>. He's got a clever knack for stirring the framework ant pile and getting developers eyes off their IDEs long enough to discuss their passions. In this case the developers include <a href="http://www.benclinkinbeard.com/">Ben Clinkinbeard</a>, <a href="http://shaun.boyblack.co.za/blog/">Shaun Smith</a>, <a href="http://jessewarden.com/">Jesse Warden</a> and myself.</p>
<p>These conversations are always good natured. While we work on (or use) "competing" frameworks there is always a sense of mutual respect. We like our tools but have obvious inclinations towards the projects that we've invested our hearts and souls into.</p>
<p>Ben Clinkinbeard has pointed out (several times!) that Robotlegs is all about extending the <a href="http://github.com/robotlegs/robotlegs-framework/tree/master/src/org/robotlegs/mvcs/">MVCS classes</a>. My stock answer is that there is a clear separation between the framework and the concrete MVCS implementation. To paraphrase Ben, "Well show it to me then!"</p>
<p>All of the <a href="http://www.robotlegs.org/examples/">"official" Robotlegs examples</a> are making use of the MVCS implementation. Why? Because it is solid, recognizable, and fairly easy to get one's head around. It provides a common ground for developers and a set architectural structure which is a huge advantage in any team environment. It is important to make the distinction  between the framework and the MVCS implementation. What does that even mean? At the core, Robotlegs is a modular set of tools to provide a convenient mechanism for wiring applications. Robotlegs is not doing class reflection. Robotlegs is not an automated dependency injection solution. Robotlegs is  an adapter to a dependency injection solution,by default the lightweight <a href="http://github.com/tschneidereit/SwiftSuspenders">SwiftSuspenders library</a>. Through a set of tools, namely the MediatorMap, CommandMap, ViewMap, and the injection adapter Robotlegs provides a robust starting place to begin coding your application.</p>
<p>The MVCS implementation is a set of base classes loosely modeled on <a href="http://puremvc.org/">PureMVC</a>. At the heart of the implementation is the Context. The Context creates instances of the various mapping classes, the injection adapter, and gives the developer a centralized IEventDispatcher that can be used for messaging between application tiers.. The other three classes, Actor, Mediator, and Command, reduce boiler plate and provide convenient access to injected dependencies typically used in the the MVCS tiers.</p>
<p>What if you hate PureMVC, don't want to extend any framework classes, or generally just want to work in a different way outside of a prescribed MVCS architecture?</p>
<p>No problem.</p>
<p>Here's the deal. Robotlegs, the framework, is a <a href="http://github.com/robotlegs/robotlegs-framework/tree/master/src/org/robotlegs/core/">set of interfaces</a>. You can effectively do whatever you want with these interfaces. You can make use of the base concrete implementations of the core interfaces, use a class from the MVCS implementation, implement your own concrete classes based on the framework core, bring in other libraries or any combination you can think of. In terms of a framework, Robotlegs can be whatever you want/need it to be.</p>
<h2>Man Joel, that was a long intro. Where's the freaking code??!?</h2>
<p><span id="more-386"></span><br />
So, to that end, I sat down to rework <a href="http://joelhooks.com/2009/07/17/robotlegs-as3-a-dependency-injection-driven-mvcs-framework-for-flashflex-–-inspired-by-puremvc/">my first (and still favorite) Robotlegs example application</a> and demonstrate this concept a bit.</p>
<p>I've stripped the example down slightly, using just the XMLImageService and not connecting to Flickr (in an effort to keep it simple).</p>
<p><a href="http://github.com/joelhooks/robotlegs-examples-ImageGalleryPM">Get the source on GitHub</a><br />
<a href="http://github.com/joelhooks/robotlegs-examples-ImageGalleryPM/zipball/v1.0.0"><strong>Download the full source as a Zip</strong></a></p>
<p><strong>This example requires:</strong><br />
<a href="http://www.robotlegs.org/">Robotlegs</a><br />
<a href="http://github.com/robertpenner/as3-signals">AS3-Signals</a><br />
<a href="http://github.com/joelhooks/signals-extensions-CommandSignal">SignalsCommandMap library</a><br />
<a href="http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4">Flex 4 SDK (a fairly recent version)</a></p>
<div class="note-block" align="center"><strong>You neeed <a href="http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4">SDK version 4.0.0.12635 or later</a> to compile this...</strong></div>
<div align="center">
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_index_1931208524"
			class="flashmovie"
			width="400"
			height="500">
	<param name="movie" value="http://joelhooks.com/examples/robotlegsGalleryPM/index.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://joelhooks.com/examples/robotlegsGalleryPM/index.swf"
			name="fm_index_1931208524"
			width="400"
			height="500">
	<!--<![endif]-->
		
<p><a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object></div>
<p><br/><br />
The example should be familiar if you've looked at the Image Gallery previously. It loads images from a service and displays them. This version is much different on the code level however. Instead of mediators and data models, the gallery is using presentation models and AS3-Signals. It still uses the Context from the MVCS implementation. it's so f'n handy and I don't want to manually instantiate the maps.</p>
<h3>ImageGalleryContext</h3>

<div class="wp_codebox"><table><tr id="p38673"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code" id="p386code73"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ImageGalleryContext <span style="color: #0066CC;">extends</span> Context
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">private</span> const VIEW_PACKAGE:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;org.robotlegs.examples.imagegallery.view.components&quot;</span>;
	<span style="color: #0066CC;">private</span> const LOAD_GALLERY:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;loadGallery&quot;</span>;
&nbsp;
	override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> startup<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//map the views</span>
		viewMap.<span style="color: #006600;">mapPackage</span><span style="color: #66cc66;">&#40;</span>VIEW_PACKAGE<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">//map the presentation models</span>
		injector.<span style="color: #006600;">mapSingletonOf</span><span style="color: #66cc66;">&#40;</span> IGalleryViewPresentationModel, GalleryViewPresentationModel <span style="color: #66cc66;">&#41;</span>;
		injector.<span style="color: #006600;">mapSingletonOf</span><span style="color: #66cc66;">&#40;</span> IGalleryThumbnailsPresentationModel, GalleryThumbnailsPresentationModel <span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">//map the services and their factories</span>
		injector.<span style="color: #006600;">mapSingletonOf</span><span style="color: #66cc66;">&#40;</span> IGalleryImageService, XMLImageService <span style="color: #66cc66;">&#41;</span>;
		injector.<span style="color: #006600;">mapSingletonOf</span><span style="color: #66cc66;">&#40;</span> IGalleryFactory, XMLGalleryFactory <span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">//map the signals</span>
		injector.<span style="color: #006600;">mapSingleton</span><span style="color: #66cc66;">&#40;</span>GalleryUpdatedSignal<span style="color: #66cc66;">&#41;</span>;
		injector.<span style="color: #006600;">mapSingleton</span><span style="color: #66cc66;">&#40;</span>GalleryImageSelectedSignal<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">//map the command</span>
		commandMap.<span style="color: #006600;">mapEvent</span><span style="color: #66cc66;">&#40;</span> LOAD_GALLERY, LoadGalleryCommand <span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">//go!!</span>
		eventDispatcher.<span style="color: #006600;">dispatchEvent</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Event<span style="color: #66cc66;">&#40;</span>LOAD_GALLERY<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>As you can see here the context is performing the duty of mapping the injections the application will use.</p>
<h3>Automated Dependency Injected View</h3>

<div class="wp_codebox"><table><tr id="p38674"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
</pre></td><td class="code" id="p386code74"><pre class="actionscript" style="font-family:monospace;"><span style="color: #66cc66;">&lt;</span>s:Group xmlns:fx=<span style="color: #ff0000;">&quot;http://ns.adobe.com/mxml/2009&quot;</span>
		 xmlns:s=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/spark&quot;</span>
		 xmlns:mx=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/halo&quot;</span> <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #0066CC;">height</span>=<span style="color: #ff0000;">&quot;100&quot;</span><span style="color: #66cc66;">&gt;</span>
	<span style="color: #66cc66;">&lt;</span>fx:Script<span style="color: #66cc66;">&gt;</span>
		<span style="color: #66cc66;">&lt;!</span><span style="color: #66cc66;">&#91;</span>CDATA<span style="color: #66cc66;">&#91;</span>
			<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">robotlegs</span>.<span style="color: #006600;">examples</span>.<span style="color: #006600;">imagegallery</span>.<span style="color: #006600;">view</span>.<span style="color: #006600;">models</span>.<span style="color: #006600;">IGalleryThumbnailsPresentationModel</span>;
&nbsp;
			<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _model:IGalleryThumbnailsPresentationModel;
&nbsp;
			<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> model<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:IGalleryThumbnailsPresentationModel
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">return</span> _model;
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
			<span style="color: #66cc66;">&#91;</span>Inject<span style="color: #66cc66;">&#93;</span>
			<span style="color: #66cc66;">&#91;</span>Bindable<span style="color: #66cc66;">&#93;</span>
			<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> model<span style="color: #66cc66;">&#40;</span>value:IGalleryThumbnailsPresentationModel<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
			<span style="color: #66cc66;">&#123;</span>
				_model = value;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&gt;</span>
	<span style="color: #66cc66;">&lt;/</span>fx:Script<span style="color: #66cc66;">&gt;</span>
	<span style="color: #66cc66;">&lt;</span>s:Rect <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;100%&quot;</span>
			<span style="color: #0066CC;">height</span>=<span style="color: #ff0000;">&quot;100%&quot;</span><span style="color: #66cc66;">&gt;</span>
		<span style="color: #66cc66;">&lt;</span>s:fill<span style="color: #66cc66;">&gt;</span>
			<span style="color: #66cc66;">&lt;</span>s:SolidColor <span style="color: #0066CC;">color</span>=<span style="color: #ff0000;">&quot;#111111&quot;</span><span style="color: #66cc66;">/&gt;</span>
		<span style="color: #66cc66;">&lt;/</span>s:fill<span style="color: #66cc66;">&gt;</span>
	<span style="color: #66cc66;">&lt;/</span>s:Rect<span style="color: #66cc66;">&gt;</span>
	<span style="color: #66cc66;">&lt;</span>s:Group <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #0066CC;">height</span>=<span style="color: #ff0000;">&quot;100%&quot;</span><span style="color: #66cc66;">&gt;</span>
		<span style="color: #66cc66;">&lt;</span>s:layout<span style="color: #66cc66;">&gt;</span>
			<span style="color: #66cc66;">&lt;</span>s:VerticalLayout gap=<span style="color: #ff0000;">&quot;0&quot;</span><span style="color: #66cc66;">/&gt;</span>
		<span style="color: #66cc66;">&lt;/</span>s:layout<span style="color: #66cc66;">&gt;</span>
		<span style="color: #66cc66;">&lt;</span>s:DataGroup id=<span style="color: #ff0000;">&quot;dgThumbnails&quot;</span>
					 clipAndEnableScrolling=<span style="color: #ff0000;">&quot;true&quot;</span>
					 dataProvider=<span style="color: #ff0000;">&quot;{model.dataProvider}&quot;</span>
					 <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;100%&quot;</span>
					 <span style="color: #0066CC;">height</span>=<span style="color: #ff0000;">&quot;85&quot;</span>
					 itemRenderer=<span style="color: #ff0000;">&quot;org.robotlegs.examples.imagegallery.view.components.renderers.GalleryImageThumbnailItemRenderer&quot;</span><span style="color: #66cc66;">&gt;</span>
			<span style="color: #66cc66;">&lt;</span>s:layout<span style="color: #66cc66;">&gt;</span>
				<span style="color: #66cc66;">&lt;</span>s:HorizontalLayout gap=<span style="color: #ff0000;">&quot;0&quot;</span><span style="color: #66cc66;">/&gt;</span>
			<span style="color: #66cc66;">&lt;/</span>s:layout<span style="color: #66cc66;">&gt;</span>
		<span style="color: #66cc66;">&lt;/</span>s:DataGroup<span style="color: #66cc66;">&gt;</span>
		<span style="color: #66cc66;">&lt;</span>s:HScrollBar id=<span style="color: #ff0000;">&quot;thumbScrollBar&quot;</span>
					  viewport=<span style="color: #ff0000;">&quot;{dgThumbnails}&quot;</span>
					  <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;100%&quot;</span>
					  smoothScrolling=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #66cc66;">/&gt;</span>
	<span style="color: #66cc66;">&lt;/</span>s:Group<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;/</span>s:Group<span style="color: #66cc66;">&gt;</span></pre></td></tr></table></div>

<p>The viewMap is set to map the entire components folder. This is a convenient way to provide automated dependency injection to a large number of views. It also maps views in any sub-package of the mapped package.</p>
<h3>GalleryViewThumbnailsPresentationModel</h3>

<div class="wp_codebox"><table><tr id="p38675"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
</pre></td><td class="code" id="p386code75"><pre class="actionscript" style="font-family:monospace;"><span style="color: #66cc66;">&#91;</span>Bindable<span style="color: #66cc66;">&#93;</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> GalleryThumbnailsPresentationModel <span style="color: #0066CC;">implements</span> IGalleryThumbnailsPresentationModel
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #66cc66;">&#91;</span>Inject<span style="color: #66cc66;">&#93;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> updated:GalleryUpdatedSignal;
&nbsp;
	<span style="color: #66cc66;">&#91;</span>Inject<span style="color: #66cc66;">&#93;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> imageSelected:GalleryImageSelectedSignal;
&nbsp;
	<span style="color: #66cc66;">&#91;</span>PostConstruct<span style="color: #66cc66;">&#93;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> mapSignalListeners<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
	<span style="color: #66cc66;">&#123;</span>
		updated.<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span>galleryUpdated<span style="color: #66cc66;">&#41;</span>;
		imageSelected.<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span>updateImageSelectionState<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _dataprovider:ArrayCollection;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> dataProvider<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:ArrayCollection
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">return</span> _dataprovider;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> dataProvider<span style="color: #66cc66;">&#40;</span>v:ArrayCollection<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
	<span style="color: #66cc66;">&#123;</span>
		_dataprovider = v;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> galleryUpdated<span style="color: #66cc66;">&#40;</span>gallery:Gallery<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
	<span style="color: #66cc66;">&#123;</span>
		dataProvider = gallery.<span style="color: #006600;">photos</span>;
		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>gallery.<span style="color: #006600;">photos</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			imageSelected.<span style="color: #006600;">dispatch</span><span style="color: #66cc66;">&#40;</span>gallery.<span style="color: #006600;">photos</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> updateImageSelectionState<span style="color: #66cc66;">&#40;</span>image:GalleryImage<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">for</span> <span style="color: #b1b100;">each</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> galleryImage:GalleryImage <span style="color: #b1b100;">in</span> dataProvider<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			galleryImage.<span style="color: #006600;">selected</span> = galleryImage == image;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<h3>IGalleryViewThumbnailsPresentationModel</h3>

<div class="wp_codebox"><table><tr id="p38676"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p386code76"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/**
 * This interface is simple because the application is simple. Obviously in
 * a large application you'd get more complex interfaces.
 */</span>
<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> IGalleryThumbnailsPresentationModel
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> dataProvider<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:ArrayCollection;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The presentation models are provided as singletons. The application will only display one of each view that requires the model. They are also mapped as interfaces with the interfaces being injected into the views. With the presentation model, the view does not update the model directly. By using interfaces we can supply read-only contracts between the presentation models and the views they control:</p>
<h3>XMLImageService</h3>

<div class="wp_codebox"><table><tr id="p38677"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
</pre></td><td class="code" id="p386code77"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> XMLImageService <span style="color: #0066CC;">implements</span> IGalleryImageService
<span style="color: #66cc66;">&#123;</span>
	protected <span style="color: #0066CC;">static</span> const BASE_URL:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;assets/gallery/&quot;</span>;
&nbsp;
	<span style="color: #66cc66;">&#91;</span>Inject<span style="color: #66cc66;">&#93;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> galleryFactory:IGalleryFactory;
&nbsp;
	<span style="color: #66cc66;">&#91;</span>Inject<span style="color: #66cc66;">&#93;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> galleryUpdated:GalleryUpdatedSignal;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> XMLImageService<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> loadGallery<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">var</span> service:HTTPService = <span style="color: #000000; font-weight: bold;">new</span> HTTPService<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #000000; font-weight: bold;">var</span> responder:Responder = <span style="color: #000000; font-weight: bold;">new</span> Responder<span style="color: #66cc66;">&#40;</span>handleServiceResult, handleServiceFault<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #000000; font-weight: bold;">var</span> token:AsyncToken;
		service.<span style="color: #006600;">resultFormat</span> = <span style="color: #ff0000;">&quot;e4x&quot;</span>;
		service.<span style="color: #0066CC;">url</span> = BASE_URL+<span style="color: #ff0000;">&quot;gallery.xml&quot;</span>;
		token = service.<span style="color: #0066CC;">send</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		token.<span style="color: #006600;">addResponder</span><span style="color: #66cc66;">&#40;</span>responder<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	protected <span style="color: #000000; font-weight: bold;">function</span> handleServiceResult<span style="color: #66cc66;">&#40;</span>event:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">var</span> gallery:Gallery = galleryFactory.<span style="color: #006600;">createGallery</span><span style="color: #66cc66;">&#40;</span>event.<span style="color: #006600;">result</span>.<span style="color: #006600;">image</span>, BASE_URL<span style="color: #66cc66;">&#41;</span>;
		galleryUpdated.<span style="color: #006600;">dispatch</span><span style="color: #66cc66;">&#40;</span> gallery <span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	protected <span style="color: #000000; font-weight: bold;">function</span> handleServiceFault<span style="color: #66cc66;">&#40;</span>event:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>event<span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<h3>IGalleryImageService</h3>

<div class="wp_codebox"><table><tr id="p38678"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p386code78"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> IGalleryImageService
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">function</span> loadGallery<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The service and its factory class are also mapped as singleton interfaces. This makes it really easy to swap out services (to add a Flickr service for example).</p>
<h3>GalleryUpdatedSignal</h3>

<div class="wp_codebox"><table><tr id="p38679"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p386code79"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> GalleryUpdatedSignal <span style="color: #0066CC;">extends</span> Signal
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> GalleryUpdatedSignal<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span>Gallery<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The signals are simple classes that merely extend the Signal class and declare their payload type in the super() of the constructor. Signals are a really marvelous concept and provide a lot of very nice functionality. They make a great companion to Robotlegs and I think you will see some really cool stuff being done with Signals and Robotlegs in the very near future.</p>
<h3>LoadGalleryCommand</h3>

<div class="wp_codebox"><table><tr id="p38680"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p386code80"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> LoadGalleryCommand
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #66cc66;">&#91;</span>Inject<span style="color: #66cc66;">&#93;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> service:IGalleryImageService;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> execute<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
	<span style="color: #66cc66;">&#123;</span>
		service.<span style="color: #006600;">loadGallery</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The application has a single command, LoadGalleryCommand, that is used to actually load the initial images from service and is only called once. In fact, the event that is fired to launch this command in the context startup method is the only event that this application uses (outside of the Flex events on the UI items). That is awesome!</p>
<p>Something you will notice right away looking at the source is that the ImageGalleryContext is the only class that extends a Robotlegs class. You could do without that too, frankly, and create your own context that created the maps and injector instances. I'm not a masochist however, and I will use the provided context for this example. There is a huge amount of potential there for implementing your own custom contexts. I think an interesting implementation might pitch Flash Events altogether and use only Signals for communication between application actors. This example is essentially doing that, but I'd like to get rid of that single event in startup too!</p>
<p><a href="http://github.com/joelhooks/robotlegs-examples-ImageGalleryPM/zipball/v1.0.0"><strong>Download the full source as a Zip</strong></a></p>
<p>Let me know if you make something cool with Robotlegs and post details about it, I've got a stack of these to give away:</p>
<div align="center"><a href="http://69.164.207.135/wp-content/uploads/2010/01/photo.jpg"><img src="http://joelhooks.com/wp-content/uploads/2010/01/photo-225x300.jpg" alt="photo" title="photo" width="225" height="300" /></a></div>
]]></content:encoded>
			<wfw:commentRss>http://joelhooks.com/2010/01/16/robotlegs-image-gallery-example-using-as3-signals-and-the-presentation-model/feed/</wfw:commentRss>
		<slash:comments>48</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
	</item>
		<item>
		<title>Continuous Scrolling Thumbnail Component for Flex</title>
		<link>http://joelhooks.com/2009/12/24/continuous-scrolling-thumbnail-component-for-flex/</link>
		<comments>http://joelhooks.com/2009/12/24/continuous-scrolling-thumbnail-component-for-flex/#comments</comments>
		<pubDate>Fri, 25 Dec 2009 04:37:27 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[components]]></category>
		<category><![CDATA[flex]]></category>

		<guid isPermaLink="false">http://joelhooks.com/?p=378</guid>
		<description><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top: 60%;
left: 5px;
}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;

padding-bottom:2px;
}


#bottomcontainerBox {
height: 30px;
width:50%;
padding-top:1px;
}

#bottomcontainerBox .buttons {
float:left;
height: 30px;
margin:4px 4px 4px 4px;
}

</style>
This post continues to be the most popular on this space after a year and a half. I've never been particularly fond of the implmenetation. Tightly coupled to very specific data sets and not really anything like a Flex component. It is setup more like an application (because it was pulled out of an application [...]]]></description>
			<content:encoded><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top: 60%;
left: 5px;
}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;

padding-bottom:2px;
}


#bottomcontainerBox {
height: 30px;
width:50%;
padding-top:1px;
}

#bottomcontainerBox .buttons {
float:left;
height: 30px;
margin:4px 4px 4px 4px;
}

</style>
<p><a href="http://joelhooks.com/2008/05/11/continuous-scrolling-image-thumbnail-and-slideshow-component-for-flex/">This post</a> continues to be the most popular on this space after a year and a half. I've never been particularly fond of the implmenetation. Tightly coupled to very specific data sets and not really anything like a Flex component. It is setup more like an application (because it was pulled out of an application and generalized).</p>
<p>I've finally found the opportunity to rewrite the continuous scrolling thumbnail view portion of the slideshow and I am much happier with the results. Now, instead of resembling an application it is structured like a standard Flex 3 List based component. You supply it with a dataProvider and an itemRenderer (IListItemRenderer) and it acts as you would expect it to.</p>
<p>This approach is much nicer, as the data is cleanly decoupled from the component implementation and it opens up the doors for reuse.</p>
<p><a href="http://github.com/joelhooks/components-thumbScroller">The code is available on Github</a>. If you have any suggestions or would like to add to the component, fork it and let me know!</p>
<div align="center">
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_SimpleSlideshow_534031005"
			class="flashmovie"
			width="450"
			height="600">
	<param name="movie" value="http://joelhooks.com/examples/thumbScroller/SimpleSlideshow.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://joelhooks.com/examples/thumbScroller/SimpleSlideshow.swf"
			name="fm_SimpleSlideshow_534031005"
			width="450"
			height="600">
	<!--<![endif]-->
		
<p><a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object></div>
]]></content:encoded>
			<wfw:commentRss>http://joelhooks.com/2009/12/24/continuous-scrolling-thumbnail-component-for-flex/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
	</item>
		<item>
		<title>Robotlegs MVCS: Walkthrough of an AIR Twitter Client</title>
		<link>http://joelhooks.com/2009/11/11/a-walkthrough-of-an-air-twitter-client-using-robotlegs-mvcs/</link>
		<comments>http://joelhooks.com/2009/11/11/a-walkthrough-of-an-air-twitter-client-using-robotlegs-mvcs/#comments</comments>
		<pubDate>Wed, 11 Nov 2009 17:46:56 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[flex]]></category>
		<category><![CDATA[robotlegs]]></category>
		<category><![CDATA[screencast]]></category>
		<category><![CDATA[MVCS]]></category>

		<guid isPermaLink="false">http://joelhooks.com/?p=349</guid>
		<description><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top: 60%;
left: 5px;
}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;

padding-bottom:2px;
}


#bottomcontainerBox {
height: 30px;
width:50%;
padding-top:1px;
}

#bottomcontainerBox .buttons {
float:left;
height: 30px;
margin:4px 4px 4px 4px;
}

</style>
Here is the "completed" project on Github. This is a roughly 25 minute walkthrough of wiring together a Twitter client using the Robotlegs AS3 Micro-Architecture MVCS implementation. It covers setting up the context, using commands, mediating composite view component, granular view component mediation, and retrieving data from a service. Apologies in advance for the ambient [...]]]></description>
			<content:encoded><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top: 60%;
left: 5px;
}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;

padding-bottom:2px;
}


#bottomcontainerBox {
height: 30px;
width:50%;
padding-top:1px;
}

#bottomcontainerBox .buttons {
float:left;
height: 30px;
margin:4px 4px 4px 4px;
}

</style>
<p><a href="http://joelhooks.com/video/rl_twitter.mov" rel="shadowbox;height=568;width=960"><img src="http://69.164.207.135/wp-content/uploads/2009/11/rl_twit_ss.png" alt="rl_twit_ss" title="rl_twit_ss" width="490" height="257" class="aligncenter size-full wp-image-354" /></a></p>
<p><a href="http://github.com/joelhooks/robotlegs-examples-Twitter"><strong>Here is the "completed" project on Github.</strong></a></p>
<p>This is a roughly 25 minute walkthrough of wiring together a Twitter client using the Robotlegs AS3 Micro-Architecture MVCS implementation. It covers setting up the context, using commands, mediating composite view component, granular view component mediation, and retrieving data from a service.</p>
<p>Apologies in advance for the ambient noise. 4 kids, 2 dogs, and a busy wife does not make the perfect sound booth <img src='http://joelhooks.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>I am considering expanding on this example and creating a start to finish video series creating a full Twitter client with Robotlegs. This would cover the actual component building, wiring everything together, working with data models (local storage, etc), and refining the application to use the advanced features available with the Twitter API using TDD the whole time for development. Is this something that you'd be interested in?</p>
]]></content:encoded>
			<wfw:commentRss>http://joelhooks.com/2009/11/11/a-walkthrough-of-an-air-twitter-client-using-robotlegs-mvcs/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
<enclosure url="http://joelhooks.com/video/rl_twitter.mov" length="25935872" type="video/quicktime" />
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
	</item>
		<item>
		<title>Robotlegs IoC/DI Flex Framework Examples Updated</title>
		<link>http://joelhooks.com/2009/08/14/robotlegs-iocdi-flex-framework-examples-updated/</link>
		<comments>http://joelhooks.com/2009/08/14/robotlegs-iocdi-flex-framework-examples-updated/#comments</comments>
		<pubDate>Fri, 14 Aug 2009 19:32:51 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[object oriented concepts]]></category>
		<category><![CDATA[robotlegs]]></category>
		<category><![CDATA[software architecture]]></category>

		<guid isPermaLink="false">http://joelhooks.com/?p=314</guid>
		<description><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top: 60%;
left: 5px;
}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;

padding-bottom:2px;
}


#bottomcontainerBox {
height: 30px;
width:50%;
padding-top:1px;
}

#bottomcontainerBox .buttons {
float:left;
height: 30px;
margin:4px 4px 4px 4px;
}

</style>
Live demos are available on robotlegs.org here Acme Widget Factory: The Acme Widget Factory is modular example and deals with loading multiple modules into a shell and communicating between those modules. The shell and each module has its own context. Lazy Stack: Lazy Stack is an example of deferred instantiation of Flex components, and how [...]]]></description>
			<content:encoded><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top: 60%;
left: 5px;
}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;

padding-bottom:2px;
}


#bottomcontainerBox {
height: 30px;
width:50%;
padding-top:1px;
}

#bottomcontainerBox .buttons {
float:left;
height: 30px;
margin:4px 4px 4px 4px;
}

</style>
<p>Live demos are available on <a href="http://www.robotlegs.org/examples/">robotlegs.org here</a></p>
<p>Acme Widget Factory:<br />
The Acme Widget Factory is modular example and deals with loading multiple modules into a shell and communicating between those modules. The shell and each module has its own context.</p>
<p>Lazy Stack:<br />
Lazy Stack is an example of deferred instantiation of Flex components, and how this is handled by the Robotlegs framework.</p>
<p>Wheres Window:<br />
Where's Window is a simple example to show how to mediate and communicate between a set of instances of the same view component that have individual mediators. In this case, the view components are windows in an AIR application. This uses a proxy to register and manage the currently open unique windows. I am going to write a more detailed breakdown of this particular example soon.</p>
<h2>The Elevator Pitch</h2>
<p><a href="http://github.com/darscan/robotlegs/tree/master">Robotlegs</a> is an IoC/DI container that is modeled on the PureMVC application structure. It is designed to be MVCS, model-view-controller-service, providing metadata dependency injection across these layers. While following the well organized approach of PureMVC, it eliminates the use of framework singletons and takes full advantage of the Flash Platform to keep the dreaded boiler plate code to a minimum. Eliminating these singletons makes unit testing and TDD much easier, which can be a real struggle when trying to isolate classes for testing where these singleton dependencies exist. Robotlegs is fast, clean, and perhaps more importantly fun to work with. There is a smorgasbord of architectural frameworks available for Flex/AS3, but Robotlegs is worth a look even if you are sick of looking at architectural frameworks.<br />
<a href="http://github.com/darscan/robotlegs/tree/master"><img class="aligncenter size-full wp-image-262" title="robotlegssketchsmall" src="http://69.164.207.135/wp-content/uploads/2009/07/robotlegssketchsmall.gif" alt="robotlegssketchsmall" width="221" height="260" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://joelhooks.com/2009/08/14/robotlegs-iocdi-flex-framework-examples-updated/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
	</item>
		<item>
		<title>Unit Testing: IoC/DI, Robotlegs and FlexUnit 4</title>
		<link>http://joelhooks.com/2009/07/21/unit-testing-with-inversion-of-control-ioc-and-dependency-injection-di-with-the-robotlegs-framework-and-flexunit-4/</link>
		<comments>http://joelhooks.com/2009/07/21/unit-testing-with-inversion-of-control-ioc-and-dependency-injection-di-with-the-robotlegs-framework-and-flexunit-4/#comments</comments>
		<pubDate>Tue, 21 Jul 2009 06:08:24 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[FlexUnit]]></category>
		<category><![CDATA[object oriented concepts]]></category>
		<category><![CDATA[robotlegs]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://joelhooks.com/?p=283</guid>
		<description><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top: 60%;
left: 5px;
}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;

padding-bottom:2px;
}


#bottomcontainerBox {
height: 30px;
width:50%;
padding-top:1px;
}

#bottomcontainerBox .buttons {
float:left;
height: 30px;
margin:4px 4px 4px 4px;
}

</style>
I've been reviewing the various IoC containers available for Flex/Actionscript. One of the benefits of IoC and DI is that it greatly facilitates unit testing. By injecting our dependencies into our applications actors, we are effectively isolating them from the other classes that make the application function. "So what?" In computer programming, unit testing is [...]]]></description>
			<content:encoded><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top: 60%;
left: 5px;
}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;

padding-bottom:2px;
}


#bottomcontainerBox {
height: 30px;
width:50%;
padding-top:1px;
}

#bottomcontainerBox .buttons {
float:left;
height: 30px;
margin:4px 4px 4px 4px;
}

</style>
<p>I've been reviewing the various IoC containers available for Flex/Actionscript. One of the benefits of IoC and DI is that it greatly facilitates unit testing. By injecting our dependencies into our applications actors, we are effectively isolating them from the other classes that make the application function. "So what?"</p>
<blockquote><p>In computer programming, unit testing is a software verification and validation method in which a programmer tests that individual units of source code are fit for use. A unit is the smallest testable part of an application. In procedural programming a unit may be an individual program, function, procedure, etc., while in object-oriented programming, the smallest unit is a class, which may belong to a base/super class, abstract class or derived/child class.</p>
<p>Ideally, each test case is independent from the others: substitutes like method stubs, mock objects, fakes and test harnesses can be used to assist testing a module in isolation. Unit tests are typically written and run by software developers to ensure that code meets its design and behaves as intended. Its implementation can vary from being very manual (pencil and paper) to being formalized as part of build automation. -<a href="http://en.wikipedia.org/wiki/Unit_testing">wikipedia unit testing entry</a></p></blockquote>
<p>To effectively unit test classes, we want them to be as isolated as possible. The class needs to stand on its own and have its functionality vetted to ensure that it behaves as expected. When we start to test how our classes behave together, we have entered the land of <a href="http://en.wikipedia.org/wiki/Integration_testing">integration testing</a>. Integration testing is a worthwhile pursuit also, but before we get to that point, we really want to ensure that our classes are solid by themselves.</p>
<p>Using the example in this <a href="http://joelhooks.com/2009/07/17/robotlegs-as3-a-dependency-injection-driven-mvcs-framework-for-flashflex-–-inspired-by-puremvc/">Robotlegs Image Gallery demonstration</a>, we are going to test a couple of the classes that make up the application. The demo isn't complex. It has a very simple model, a single service, and just a few views. In fact, it might even seem like a waste of time to unit test a simple example such as this, but the concepts are applicable to larger applications where the benefits of this type of testing really start to pay off.</p>
<p><a href="http://opensource.adobe.com/wiki/display/flexunit/FlexUnit+4+feature+overview">FlexUnit 4 is the framework</a> that is used here. It provides very handy asynchronous testing, meta data support, and a whole bucket of stellar features that I won't even begin to scratch the surface of. FlexUnit 4 also comes with a graphical testrunner interface:</p>
<p><img class="aligncenter size-full wp-image-284" title="success" src="http://joelhooks.com/wp-content/uploads/2009/07/success.jpg" alt="success" width="450" height="277" /><br />
<span id="more-283"></span></p>
<h2>On to the code...</h2>
<p><a href="http://github.com/joelhooks/robotlegsdemos/tree/master">full source is here...</a></p>
<p>This test runner interface is added to the RobotlegsImageGalleryTestRunner.mxml application:</p>

<div class="wp_codebox"><table><tr id="p28385"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code" id="p283code85"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:Application</span></span>
<span style="color: #009900;">	<span style="color: #000066;">xmlns:fx</span>=<span style="color: #ff0000;">&quot;http://ns.adobe.com/mxml/2009&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">xmlns:s</span>=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/spark&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">xmlns:mx</span>=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/halo&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">xmlns:flexUnitUIRunner</span>=<span style="color: #ff0000;">&quot;http://www.adobe.com/2009/flexUnitUIRunner&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">creationComplete</span>=<span style="color: #ff0000;">&quot;onCreationComplete()&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:layout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:BasicLayout</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/s:layout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fx:Script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #339933;">&lt;![CDATA[</span>
<span style="color: #339933;">			import org.robotlegs.demos.imagegallery.test.suites.RobotlegsImageGalleryTestSuite;</span>
<span style="color: #339933;">			import org.flexunit.listeners.UIListener;</span>
<span style="color: #339933;">			import org.flexunit.runner.FlexUnitCore;</span>
&nbsp;
<span style="color: #339933;">			private var core:FlexUnitCore;</span>
<span style="color: #339933;">			private function onCreationComplete():void</span>
<span style="color: #339933;">			{</span>
<span style="color: #339933;">				this.core = new FlexUnitCore();</span>
<span style="color: #339933;">				core.addListener( new UIListener( uiListener ));</span>
<span style="color: #339933;">				core.run( RobotlegsImageGalleryTestSuite )</span>
<span style="color: #339933;">			}</span>
<span style="color: #339933;">		]]&gt;</span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/fx:Script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;flexUnitUIRunner:TestRunnerBase</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;uiListener&quot;</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;100%&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/s:Application<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>The core runs our test suites, in this case we are only running a single suite: the RobotlegsImageGalleryTestSuite. The test suite carries metadata that identifies it as a FlexUnit suite. There is no need to extend TestSuite as in previous version of FlexUnit:</p>

<div class="wp_codebox"><table><tr id="p28386"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p283code86"><pre class="actionscript" style="font-family:monospace;">package org.<span style="color: #006600;">robotlegs</span>.<span style="color: #006600;">demos</span>.<span style="color: #006600;">imagegallery</span>.<span style="color: #006600;">test</span>.<span style="color: #006600;">suites</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">robotlegs</span>.<span style="color: #006600;">demos</span>.<span style="color: #006600;">imagegallery</span>.<span style="color: #006600;">test</span>.<span style="color: #006600;">cases</span>.<span style="color: #006600;">TestFlickrService</span>;
	<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">robotlegs</span>.<span style="color: #006600;">demos</span>.<span style="color: #006600;">imagegallery</span>.<span style="color: #006600;">test</span>.<span style="color: #006600;">cases</span>.<span style="color: #006600;">TestGalleryProxy</span>;
&nbsp;
	<span style="color: #66cc66;">&#91;</span>Suite<span style="color: #66cc66;">&#93;</span>
	<span style="color: #66cc66;">&#91;</span>RunWith<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;org.flexunit.runners.Suite&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RobotlegsImageGalleryTestSuite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> testFlickrService:TestFlickrService;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> testGalleryProxy:TestGalleryProxy;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The suite further defines the test cases that we are going to run, TestFlickrService and TestGalleryProxy. Any other test cases would also be included in this suite. TestFlickrService extends the Robotlegs Service class. It makes an asyncronous call to the Flickr API and retrieves images via the 'interestingness' category, as well as through a text search. The Service class has a dependency that is injected by the framework on its eventBroadcaster property. EventBroadcaster simply wraps an event dispatcher to provide a simplified interface for the framework to access. Service also provides a dispatch(event) convenience method for sending event notifications to the framework context. Our tests are not run within this context, however, so we have run into a problem. Since the framework injects the EventDispatcher into the EventBroadcaster automatically, we apparently have no way to access it and we can't receive framework notifications.</p>
<p>As it happens, we can simply inject our dependencies manually. The TestFlickrService case has a serviceDispatcher property that extends EventDispatcher. In our setUp() method that is called prior to every test, we create a new EventBroadcaster (which takes our serviceDispatcher as an argument) and add it to our fresh FlickrImageService instance. Now the test case has access to the injected dispatcher, and we can listen for our normal flash events that Robotlegs uses for notifications.</p>

<div class="wp_codebox"><table><tr id="p28387"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
</pre></td><td class="code" id="p283code87"><pre class="actionscript" style="font-family:monospace;">package org.<span style="color: #006600;">robotlegs</span>.<span style="color: #006600;">demos</span>.<span style="color: #006600;">imagegallery</span>.<span style="color: #006600;">test</span>.<span style="color: #006600;">cases</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">EventDispatcher</span>;
&nbsp;
	<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">flexunit</span>.<span style="color: #006600;">Assert</span>;
	<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">flexunit</span>.<span style="color: #006600;">async</span>.<span style="color: #006600;">Async</span>;
	<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">robotlegs</span>.<span style="color: #006600;">demos</span>.<span style="color: #006600;">imagegallery</span>.<span style="color: #006600;">events</span>.<span style="color: #006600;">GalleryEvent</span>;
	<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">robotlegs</span>.<span style="color: #006600;">demos</span>.<span style="color: #006600;">imagegallery</span>.<span style="color: #006600;">remote</span>.<span style="color: #006600;">services</span>.<span style="color: #006600;">FlickrImageService</span>;
	<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">robotlegs</span>.<span style="color: #006600;">mvcs</span>.<span style="color: #006600;">EventBroadcaster</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TestFlickrService
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> service:FlickrImageService;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> serviceDispatcher:EventDispatcher = <span style="color: #000000; font-weight: bold;">new</span> EventDispatcher<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #66cc66;">&#91;</span>Before<span style="color: #66cc66;">&#93;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setUp<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			serviceDispatcher = <span style="color: #000000; font-weight: bold;">new</span> EventDispatcher<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			service = <span style="color: #000000; font-weight: bold;">new</span> FlickrImageService<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			service.<span style="color: #006600;">eventBroadcaster</span> = <span style="color: #000000; font-weight: bold;">new</span> EventBroadcaster<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">serviceDispatcher</span><span style="color: #66cc66;">&#41;</span>;;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #66cc66;">&#91;</span>After<span style="color: #66cc66;">&#93;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> tearDown<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">serviceDispatcher</span> = <span style="color: #000000; font-weight: bold;">null</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">service</span>.<span style="color: #006600;">eventBroadcaster</span> = <span style="color: #000000; font-weight: bold;">null</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">service</span> = <span style="color: #000000; font-weight: bold;">null</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #66cc66;">&#91;</span>Test<span style="color: #66cc66;">&#40;</span>async<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testRetreiveImages<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">serviceDispatcher</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span> GalleryEvent.<span style="color: #006600;">GALLERY_LOADED</span>, Async.<span style="color: #006600;">asyncHandler</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>, handleImagesReceived, <span style="color: #cc66cc;">3000</span>, <span style="color: #000000; font-weight: bold;">null</span>, handleServiceTimeout<span style="color: #66cc66;">&#41;</span>, <span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">service</span>.<span style="color: #006600;">loadGallery</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #66cc66;">&#91;</span>Test<span style="color: #66cc66;">&#40;</span>async<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testSearchImages<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">serviceDispatcher</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span> GalleryEvent.<span style="color: #006600;">GALLERY_LOADED</span>, Async.<span style="color: #006600;">asyncHandler</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>, handleImagesReceived, <span style="color: #cc66cc;">3000</span>, <span style="color: #000000; font-weight: bold;">null</span>, handleServiceTimeout<span style="color: #66cc66;">&#41;</span>, <span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">service</span>.<span style="color: #006600;">search</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;robotlegs&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> handleServiceTimeout<span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">object</span>:<span style="color: #0066CC;">Object</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
	    	        Assert.<span style="color: #006600;">fail</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Pending Event Never Occurred'</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> handleImagesReceived<span style="color: #66cc66;">&#40;</span>event:GalleryEvent, <span style="color: #0066CC;">object</span>:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			Assert.<span style="color: #006600;">assertEquals</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;The gallery should have some photos: &quot;</span>, event.<span style="color: #006600;">gallery</span>.<span style="color: #006600;">photos</span>.<span style="color: #0066CC;">length</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>After each test is run, our tearDown method is called by FlexUnit which sets our service and its eventBroadcaster to null in preparation for the next test in this case to be run. While this example isn't very complex, it is easy to see how this could be extended to a much more complicated service.</p>
<p>The GalleryProxy class is tested in much the same way, though it doesn't have any async methods:</p>

<div class="wp_codebox"><table><tr id="p28388"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
</pre></td><td class="code" id="p283code88"><pre class="actionscript" style="font-family:monospace;">package org.<span style="color: #006600;">robotlegs</span>.<span style="color: #006600;">demos</span>.<span style="color: #006600;">imagegallery</span>.<span style="color: #006600;">test</span>.<span style="color: #006600;">cases</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">flexunit</span>.<span style="color: #006600;">Assert</span>;
	<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">robotlegs</span>.<span style="color: #006600;">demos</span>.<span style="color: #006600;">imagegallery</span>.<span style="color: #006600;">models</span>.<span style="color: #006600;">proxies</span>.<span style="color: #006600;">GalleryProxy</span>;
	<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">robotlegs</span>.<span style="color: #006600;">demos</span>.<span style="color: #006600;">imagegallery</span>.<span style="color: #006600;">models</span>.<span style="color: #006600;">vo</span>.<span style="color: #006600;">Gallery</span>;
	<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">robotlegs</span>.<span style="color: #006600;">demos</span>.<span style="color: #006600;">imagegallery</span>.<span style="color: #006600;">models</span>.<span style="color: #006600;">vo</span>.<span style="color: #006600;">GalleryImage</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TestGalleryProxy
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> galleryProxy:GalleryProxy;
&nbsp;
		<span style="color: #66cc66;">&#91;</span>Before<span style="color: #66cc66;">&#93;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setUp<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">galleryProxy</span> = <span style="color: #000000; font-weight: bold;">new</span> GalleryProxy<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #66cc66;">&#91;</span>After<span style="color: #66cc66;">&#93;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> tearDown<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">galleryProxy</span> = <span style="color: #000000; font-weight: bold;">null</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #66cc66;">&#91;</span>Test<span style="color: #66cc66;">&#93;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testSetGallery<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> gallery:Gallery = <span style="color: #000000; font-weight: bold;">new</span> Gallery<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">galleryProxy</span>.<span style="color: #006600;">gallery</span> = gallery;
			Assert.<span style="color: #006600;">assertEquals</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;GalleryProxy should have a gallery&quot;</span>, <span style="color: #0066CC;">this</span>.<span style="color: #006600;">galleryProxy</span>.<span style="color: #006600;">gallery</span> <span style="color: #66cc66;">!</span>= <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">true</span> <span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #66cc66;">&#91;</span>Test<span style="color: #66cc66;">&#93;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testSetSelectedImage<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> image1:GalleryImage = <span style="color: #000000; font-weight: bold;">new</span> GalleryImage<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #000000; font-weight: bold;">var</span> image2:GalleryImage = <span style="color: #000000; font-weight: bold;">new</span> GalleryImage<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #000000; font-weight: bold;">var</span> gallery:Gallery = <span style="color: #000000; font-weight: bold;">new</span> Gallery<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">galleryProxy</span>.<span style="color: #006600;">gallery</span> = gallery;
			gallery.<span style="color: #006600;">photos</span>.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span>image1<span style="color: #66cc66;">&#41;</span>;
			gallery.<span style="color: #006600;">photos</span>.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span>image2<span style="color: #66cc66;">&#41;</span>;
			image1.<span style="color: #006600;">selected</span> = <span style="color: #000000; font-weight: bold;">false</span>;
			image2.<span style="color: #006600;">selected</span> = <span style="color: #000000; font-weight: bold;">true</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">galleryProxy</span>.<span style="color: #006600;">setSelectedImage</span><span style="color: #66cc66;">&#40;</span>image1<span style="color: #66cc66;">&#41;</span>;
			Assert.<span style="color: #006600;">assertEquals</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Image1 should be selected&quot;</span>, image1.<span style="color: #006600;">selected</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
			Assert.<span style="color: #006600;">assertEquals</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Image 2 should NOT be selected&quot;</span>, image2.<span style="color: #006600;">selected</span>, <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>With these simple tests we are 100% certain that our classes function as expected on their own, completely isolated from the rest of the application.</p>
<h2>You really should call your mom...</h2>
<p>We are all unit testing our code and following strong Agile/TDD processes, right.... right? I don't know how much unit testing is going on in the Flash/Flex world, but my guess that the overall test coverage percentage is very small. RIAs are here. They are becoming full fledged applications that <strong><em>people rely on to do business</em></strong>. There will come a time in your RIAs life that you will need to get in there and do some refactoring. It might be for performance, fixing bugs, or adding some new kickass feature to push your app to the next level. Fact, without unit testing:</p>
<blockquote><p><strong>you're not refactoring; you're just changing shit. -<a href="http://hamletdarcy.blogspot.com/2009/06/forgotten-refactorings.html">Hamlet D'Arcy</a></strong></p></blockquote>
<p>There are many, many arguments for unit testing your code, but for me this is the most compelling. At some point in the future, you are going to want to get in there and make some changes. If your application is important to your users, you want to make sure those changes don't break everything. It is guaranteed that they will break something, the question is will you know immediately what happened, or at least where to look?</p>
<p>We are getting the proper tools for the job. FlexUnit 4 is an incredible project. There is a robust selection of frameworks that all have merits and can facilitate structured, well designed applications. I'm personally out of excuses. I want to develop applications that my users can trust and rely on to do business, and unit testing is a part of that recipe.</p>
]]></content:encoded>
			<wfw:commentRss>http://joelhooks.com/2009/07/21/unit-testing-with-inversion-of-control-ioc-and-dependency-injection-di-with-the-robotlegs-framework-and-flexunit-4/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
	</item>
	</channel>
</rss>

