<?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</title>
	<atom:link href="http://joelhooks.com/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>Robotlegs 2 (beta): Flickr Image Gallery</title>
		<link>http://joelhooks.com/2011/12/29/robotlegs-2-beta-flickr-image-gallery/</link>
		<comments>http://joelhooks.com/2011/12/29/robotlegs-2-beta-flickr-image-gallery/#comments</comments>
		<pubDate>Fri, 30 Dec 2011 04:10:26 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://joelhooks.com/?p=618</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 2 is easy to jump in to. From a basic standpoint, the MVCS concepts of how to build your applications remain the same. There are some significant differences in the semantic changes with Robotlegs 2 (beta), and that is what I am going to cover here. This post will go over the changes required [...]]]></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>Robotlegs 2 is easy to jump in to. From a basic standpoint, the MVCS concepts of <em>how</em> to build your applications remain the same. There are some significant differences in the semantic changes with Robotlegs 2 (beta), and that is what I am going to cover here. This post will go over the changes required to port the <a href="https://github.com/robotlegs/robotlegs-demos-Bundle/tree/master/FlickrImageGallery">Robotlegs 1 Flickr Image Gallery demo</a>. I'm not going to cover the basics of Robotlegs MVCS, so if you are looking for that, I'd recommend checking out <a href="http://shop.oreilly.com/product/0636920021216.do">the book</a>! Also keep in mind that this is covering the beta release and some of the API is likely to change for the full release, but that shouldn't be radical either.</p>
<p>The full source for this example can be found <a href="https://github.com/joelhooks/robotlegs-examples-FlickrGallery">on Github</a>.</p>
<h3>Building Your Context</h3>
<p><script src="https://gist.github.com/1537177.js?file=RobotlegsFlickrGallery.mxml"></script></p>
<p>Robotlegs 2 introduces the ContextBuilder. Instead of extending the concrete Context class, you utilize the builder that will configure your application's bundle(s).</p>
<p><script src="https://gist.github.com/1537177.js?file=GalleryAppBundle.as"></script></p>
<p>Within the bundle (which is an implementation of the IContextBuilderBundle interface) you make use of the install method, which will add any additional bundles and supply any configs that go along with your bundle. Let's take a look at the ClassicRobotlegsBundle.</p>
<p><script src="https://gist.github.com/1537177.js?file=ClassicRobotlegsBundle.as"></script></p>
<p>In essence, we are extending this bundle. It provides the tools we are used to seeing in Robotlegs 1 and allows us to make a composite bundle on top of it with out own configuration as you can see in GalleryAppConfig.</p>
<p><script src="https://gist.github.com/1537177.js?file=GalleryAppConfig.as"></script></p>
<p>The config files are fairly similar to what you might be used to seeing within the startup() method of a Robotlegs 1 Context. There is a significant advantage to this approach that you will quickly recognize if you've ever had to create a <strong>ModularSignalCommandCrazyAssCompositeContext</strong> in your application before. With the Builder->Bundle->Config approach you are able to <em>compose</em> your applications with specific tools and extensions.</p>
<h3>Changes to Mediators</h3>
<p>Mediators, for the most part, function as expected. With the supplied MediatorMapExtension, you will no longer find the onRegister() method. Instead you will overrided the initialize() method of the Mediator class.</p>
<p><script src="https://gist.github.com/1537177.js?file=GalleryLabelMediator.as"></script></p>
<h3>Mourning the Death of Actor</h3>
<p>Actor was always an odd concrete class to include in the Framework by default. It is gone in Robotlegs 2. For this port, I create a BaseActor class of my own to supply the functionality that I needed for my services and models.</p>
<p><script src="https://gist.github.com/1537177.js?file=BaseActor.as"></script></p>
<h3>So there you have it...</h3>
<p>Check out <a href="https://github.com/joelhooks/robotlegs-examples-FlickrGallery">the source</a> if you'd like to dig in a little deeper. It shouldn't be a radical departure from what you've come to like with Robotlegs. I'm really stoked about the framework architecture that allows for complete pluggable composition of features. This is huge, and should really open up some opportunities for creating a robust ala carte framework that suits you and your team's specific needs.</p>
]]></content:encoded>
			<wfw:commentRss>http://joelhooks.com/2011/12/29/robotlegs-2-beta-flickr-image-gallery/feed/</wfw:commentRss>
		<slash:comments>34</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
	</item>
		<item>
		<title>Robotlegs Book: ActionScript Developer’s Guide to Robotlegs</title>
		<link>http://joelhooks.com/2011/07/16/robotlegs-book-actionscript-developer%e2%80%99s-guide-to-robotlegs/</link>
		<comments>http://joelhooks.com/2011/07/16/robotlegs-book-actionscript-developer%e2%80%99s-guide-to-robotlegs/#comments</comments>
		<pubDate>Sat, 16 Jul 2011 22:02:10 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[robotlegs]]></category>

		<guid isPermaLink="false">http://joelhooks.com/?p=613</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>
What a ride! Robotlegs has been an amazing project to work on for the last two years and we are excited to announce that the first book on Robotlegs is complete. This tome of knowledge was a collaboration between myself and Stray. She has written a great post that gives a lot of detail. Print [...]]]></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>What a ride! Robotlegs has been an amazing project to work on for the last two years and we are excited to announce that the first book on Robotlegs is complete. This tome of knowledge was a collaboration between myself and Stray. She has written a <a href="http://www.xxcoder.net/actionscript-developers-guide-to-robotlegs-done">great post</a> that gives a lot of detail. </p>
<p>Print copies can be pre-ordered <a href="http://oreilly.com/catalog/0636920021216">here</a>. The e-version should be available soon and there will also be a bundle</p>
]]></content:encoded>
			<wfw:commentRss>http://joelhooks.com/2011/07/16/robotlegs-book-actionscript-developer%e2%80%99s-guide-to-robotlegs/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
	</item>
		<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>13</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
	</item>
		<item>
		<title>An Introduction to Robotlegs AS3 Part 2: Models</title>
		<link>http://joelhooks.com/2011/03/12/an-introduction-to-robotlegs-as3-part-2-models/</link>
		<comments>http://joelhooks.com/2011/03/12/an-introduction-to-robotlegs-as3-part-2-models/#comments</comments>
		<pubDate>Sat, 12 Mar 2011 18:31:36 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[robotlegs]]></category>

		<guid isPermaLink="false">http://joelhooks.com/?p=597</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 second 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. This article is going to expand on those concepts and introduce Models. If you [...]]]></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 second 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. This article is going to expand on those concepts and introduce Models.</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 it, here is <a href="http://joelhooks.com/2011/03/12/an-introduction-to-robotlegs-as3-part-1-context-and-mediators/">Part One: Context and Mediators</a></p>
<h2>What is a Model?</h2>
<p>Model classes encapsulate your application's data and provide an API to access and manipulate that data. The other classes in your application will make requests of models via this API. When data on the model is updated, the model dispatches events that the other classes within your application can react to. Models are appropriate for capturing the domain logic, such as performing calculations or other manipulations. An example of this might be a shopping cart. When an item is added to the shopping cart model, the new total for all of the items in the cart will be calculated. The new total will then be stored on the model for access by other classes within the application. By placing this logic within your models, you ensure that it isn't scattered across the entire application and know exactly where to look to see how and when your data is being manipulated.</p>
<p>In addition to controlling access to data, models maintain the state of your application. State, in the sense of the data model, is not the same concept as a Flex State, which relates to controlling the appearance of your application. They are certainly related, but with a model, consider a list of objects. You want to keep track of which of these objects is selected, so the data model has a selected property which is updated with the currently selected item. Other areas of your application can now access this property to discover which item is selected and react accordingly.</p>
<p>Models can be written to be portable. Along with Services this is something to keep in mind when you develop your models. There are many common sets of data that can easily transport between one application and the next. Think of, as an example, a UserLoginModel or a ShoppingCartModel. Portability takes a bit more thought and energy, but no more than writing the same code over again for each project does.</p>
<p>The model deserves a lot of attention. It is the core of your application. The visual components get all the ooos and aaahs, but as a developer you know that data is the man behind the curtain. It is our jobs, as developers, to curate the data and deliver it to those beautiful interface items accurately. This is why isolating domain logic in the model is so important. By isolating it you have made it easier to locate, update, and maintain. With the basic definition of the model in hand, let's look at a small example application. </p>
<h2>On to the code!</h2>
<p><span id="more-597"></span></p>
<p>This pure AS3 application is making use of <a href="http://www.minimalcomps.com/">Keith Peters's awesome Minimal Comps library</a>. Don't worry, if you love Flex (and how could you not?) the next example will be a Flex app, but Minimal Comps makes it so easy to make quick examples with AS3, and Robotlegs is just as easy to use with a straight Actionscript application as it is with Flex. So let's take a look at the application and take it apart to examine its pieces.</p>
<p>
<div align=center><iframe src="http://joelhooks.com/examples/simpleList/" width="305" height="101"></iframe></div>
</p>
<p>
<div align=center><a href="http://www.developria.com/upload/2010/06/SimpleListExample.zip">SimpleListExample.zip</a></div>
</p>
<p>Above is the application we will be examining. It is a simple list of authors with a quote displayed for the author that is selected in the list. Here is <a href="http://www.developria.com/upload/2010/06/SimpleListExample.zip">the archived Flash Builder project (zip 168k)</a>, which includes the Robotlegs 1.1 and MinimalComps SWC files as well.</p>
<h3>SimpleListExample.as</h3>

<div class="wp_codebox"><table><tr id="p59748"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p597code48"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SimpleListExample <span style="color: #0066CC;">extends</span> Sprite
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> SimpleListExample<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">align</span> = StageAlign.<span style="color: #006600;">TOP_LEFT</span>;
    <span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">scaleMode</span> = StageScaleMode.<span style="color: #006600;">NO_SCALE</span>;
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>This is the main file of the application, the entry point. It is a simple Sprite. This is a Robotlegs application, so the first thing we need to do is create a Context:</p>
<h3>SimpleListExampleContext.as</h3>

<div class="wp_codebox"><table><tr id="p59749"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code" id="p597code49"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SimpleListExampleContext <span style="color: #0066CC;">extends</span> Context
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> SimpleListExampleContext<span style="color: #66cc66;">&#40;</span>contextView:DisplayObjectContainer<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span>contextView<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</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>
        injector.<span style="color: #006600;">mapSingleton</span><span style="color: #66cc66;">&#40;</span>AuthorModel<span style="color: #66cc66;">&#41;</span>;
&nbsp;
        mediatorMap.<span style="color: #006600;">mapView</span><span style="color: #66cc66;">&#40;</span>ListView, ListViewMediator<span style="color: #66cc66;">&#41;</span>;
        mediatorMap.<span style="color: #006600;">mapView</span><span style="color: #66cc66;">&#40;</span>QuoteTextArea, QuoteTextAreaMediator<span style="color: #66cc66;">&#41;</span>;
&nbsp;
        mediatorMap.<span style="color: #006600;">mapView</span><span style="color: #66cc66;">&#40;</span>SimpleListExample, ApplicationMediator<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>It is common in Flex applications to leave out the constructor completely as the contextView is set inside the MXML declaration. In an Actionscript application you want to pass the contextView into the constructor so that it can be set immediately. Now lets create an instance of the SimpleListExampleContext in the main application view:</p>
<h3>SimpleListExample.as</h3>

<div class="wp_codebox"><table><tr id="p59750"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p597code50"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SimpleListExample <span style="color: #0066CC;">extends</span> Sprite
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> context:SimpleListExampleContext;
&nbsp;
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> SimpleListExample<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">align</span> = StageAlign.<span style="color: #006600;">TOP_LEFT</span>;
    <span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">scaleMode</span> = StageScaleMode.<span style="color: #006600;">NO_SCALE</span>;
    context = <span style="color: #000000; font-weight: bold;">new</span> SimpleListExampleContext<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</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>You should notice the context variable. It is important to have a reference to your context held in memory. If you were to simply create a new instance of SimpleListExampleContext in the constructor without placing it into a variable, it would be garbage collected at the Flash Players whim. This can cause some seriously confusing hours of trouble shooting! With Flex, you simply declare the context in MXML which holds onto the reference without needing the variable, unless you create the context in the Script tag. It will require a reference there just like the above Actionscript example.</p>
<p>This application will have two views. A list of names that can be selected and a text area that will display a quote from the selected item in the list. Those two views are called:</p>
<ul>
<li>ListView</li>
<li>QuoteTextArea</li>
</ul>
<p>No sense being too creative with the naming scheme. Both of these views are simple sub-classes of base Minimal Comps classes. Here they are:</p>
<h3>ListView.as</h3>

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

<h3>QuoteTextArea.as</h3>

<div class="wp_codebox"><table><tr id="p59752"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p597code52"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> QuoteTextArea <span style="color: #0066CC;">extends</span> TextArea
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> QuoteTextArea<span style="color: #66cc66;">&#40;</span>parent:DisplayObjectContainer<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span>parent<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 application only has one of each of these. It would be fully functional if we simply used the base List and TextArea classes. Why bother with the sub-classes at all? This is a habit, and a good one when dealing with dependency injection. If we know that we are going to mediate a view class, we sub-class it and give it a name that represents its purpose. When we do it this way it can be easily isolated for mediation.</p>
<p>Now we need to do is get these two views onto the stage as children of the main application. My preferred approach to this in Actionscript applications is to place a createChildren method on the main view and call it from a mediator. Here is the main view with the createChildren() method.</p>
<h3>SimpleListExample.as</h3>

<div class="wp_codebox"><table><tr id="p59753"><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="p597code53"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SimpleListExample <span style="color: #0066CC;">extends</span> Sprite
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> hbox:HBox;
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">list</span>:ListView;
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> quoteText:QuoteTextArea;
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> context:SimpleListExampleContext;
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> SimpleListExample<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">align</span> = StageAlign.<span style="color: #006600;">TOP_LEFT</span>;
        <span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">scaleMode</span> = StageScaleMode.<span style="color: #006600;">NO_SCALE</span>;
        context = <span style="color: #000000; font-weight: bold;">new</span> SimpleListExampleContext<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/**
     * Called from ApplicationMediator's onRegister()
     */</span>
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> createChildren<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
    <span style="color: #66cc66;">&#123;</span>
        hbox = <span style="color: #000000; font-weight: bold;">new</span> HBox<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
        addChild<span style="color: #66cc66;">&#40;</span>hbox<span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #0066CC;">list</span> = <span style="color: #000000; font-weight: bold;">new</span> ListView<span style="color: #66cc66;">&#40;</span>hbox<span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0066CC;">list</span>.<span style="color: #006600;">alternateRows</span> = <span style="color: #000000; font-weight: bold;">true</span>;
&nbsp;
        quoteText = <span style="color: #000000; font-weight: bold;">new</span> QuoteTextArea<span style="color: #66cc66;">&#40;</span>hbox<span style="color: #66cc66;">&#41;</span>;
        quoteText.<span style="color: #006600;">editable</span> = <span style="color: #000000; font-weight: bold;">false</span>;
        quoteText.<span style="color: #0066CC;">selectable</span> = <span style="color: #000000; font-weight: bold;">false</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>You might be wondering why not just add the views in the constructor? I prefer to let Robotlegs get started before adding the children. By calling createChildren() from the ApplicationMediator, we are 100% certain all of the primary application bootstrapping is done and we are good to go. Here is the ApplicationMediator:</p>
<h3>ApplicationMediator.as</h3>

<div class="wp_codebox"><table><tr id="p59754"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p597code54"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ApplicationMediator <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:SimpleListExample;
&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;">createChildren</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>

<div class="note-block">Note that if you find that injections are not working the likely cause is that you need to add the <b>keep-as3-metadata+=Inject, PostConstruct</b> argument to your compiler options. Check <a href="https://github.com/robotlegs/robotlegs-framework/wiki/common-problems#no-injection">here</a> for more details.</div>
<p>The mediator doesn't have any more than the one responsibility in this simple example, but in your apps, mediating the view that is the contextView is a handy mechanism. After the mediator has been created, it needs to be mapped. This is an easy step to forget, but one to think back on when you hit debug and your mediator just doesn't respond. The odds are it isn't mapped, or its view component has never been ADDED_TO_STAGE. The startup method in your context will now look like this:</p>
<h3>SimpleListExampleContext.as</h3>
<div class="acode" style="overflow: auto; padding: 10px;" >
<div style="overflow-x: visible;">
<pre class='brush: as3'>
override public function startup():void
{
    mediatorMap.mapView(SimpleListExample, ApplicationMediator);
}
</pre>
</div>
</div>
<p>When you run the application now, you should see an empty list and text area. Now we need to get the views some data. We are going to put some static data in a Model class that our view component's mediators will be able to access. Here is the basic model:</p>
<h3>AuthorModel.as</h3>

<div class="wp_codebox"><table><tr id="p59755"><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
</pre></td><td class="code" id="p597code55"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> AuthorModel <span style="color: #0066CC;">extends</span> Actor
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _list:<span style="color: #0066CC;">Array</span>;
&nbsp;
  <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;">list</span><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;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span>_list<span style="color: #66cc66;">&#41;</span>
          initializeList<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
      <span style="color: #b1b100;">return</span> _list;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  protected <span style="color: #000000; font-weight: bold;">function</span> initializeList<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> twain:Author = <span style="color: #000000; font-weight: bold;">new</span> Author<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Twain&quot;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #000000; font-weight: bold;">var</span> poe:Author = <span style="color: #000000; font-weight: bold;">new</span> Author<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Poe&quot;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #000000; font-weight: bold;">var</span> plato:Author = <span style="color: #000000; font-weight: bold;">new</span> Author<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Plato&quot;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #000000; font-weight: bold;">var</span> fowler:Author = <span style="color: #000000; font-weight: bold;">new</span> Author<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Fowler&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    twain.<span style="color: #006600;">quote</span> = <span style="color: #ff0000;">&quot;Why, I have known clergymen, good men, kind-hearted, liberal, sincere&quot;</span> +
            <span style="color: #ff0000;">&quot;, and all that, who did not know the meaning of a 'flush.' It is enough &quot;</span> +
            <span style="color: #ff0000;">&quot;to make one ashamed of one's species.&quot;</span>;
    fowler.<span style="color: #006600;">quote</span> = <span style="color: #ff0000;">&quot;Any fool can write code that a computer can understand. &quot;</span> +
            <span style="color: #ff0000;">&quot;Good programmers write code that humans can understand.&quot;</span>;
    poe.<span style="color: #006600;">quote</span> = <span style="color: #ff0000;">&quot;Deep into that darkness peering, long I stood there, wondering, &quot;</span> +
            <span style="color: #ff0000;">&quot;fearing, doubting, dreaming dreams no mortal ever dared to dream before.&quot;</span>;
    plato.<span style="color: #006600;">quote</span> = <span style="color: #ff0000;">&quot;All things will be produced in superior quantity and quality, and with greater ease, &quot;</span> +
            <span style="color: #ff0000;">&quot;when each man works at a single occupation, in accordance with his natural gifts, &quot;</span> +
            <span style="color: #ff0000;">&quot;and at the right moment, without meddling with anything else. &quot;</span>;
&nbsp;
    _list = <span style="color: #66cc66;">&#91;</span>twain,fowler,poe,plato<span style="color: #66cc66;">&#93;</span>;
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Right away you notice that the AuthorModel extends Actor. Actor is a convenience class provided with MVCS. It provides the appropriate code for injecting the IEventDispatcher used to communicate in the context as well as a dispatch() method to send events through that dispatcher. It is not strictly necessary to extend Actor, but you will need to provide the IEventDispatcher injection point yourself if you do not. The Model in Robotlegs MVC+S is purely conceptual. There is no Model class to extend, and the naming convention is simply to express the intent of the class.</p>
<p>The model currently supplies a list of Authors that I admire, with a quote from each of them. The Author class is a simple value object to hold these properties as you can see below:</p>
<h3>Author.as</h3>

<div class="wp_codebox"><table><tr id="p59756"><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="p597code56"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Author
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">name</span>:<span style="color: #0066CC;">String</span>;
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> quote:<span style="color: #0066CC;">String</span>;
&nbsp;
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Author<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">name</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">name</span> = <span style="color: #0066CC;">name</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * Minimal comps took issue with toString(); 
	 * @return 
	 * 
	 */</span>	
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> label<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> <span style="color: #0066CC;">name</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>With a model stuffed full of data, we need to deliver that data to our list for display. The first step is to map the model for injection. The second step is to mediate the ListView and transfer the data from our model to the list. Your context's startup() method will look like this after mapping the model:</p>
<h3>SimpleListExampleContext.as</h3>

<div class="wp_codebox"><table><tr id="p59757"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p597code57"><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>
  injector.<span style="color: #006600;">mapSingleton</span><span style="color: #66cc66;">&#40;</span>AuthorModel<span style="color: #66cc66;">&#41;</span>;
&nbsp;
  mediatorMap.<span style="color: #006600;">mapView</span><span style="color: #66cc66;">&#40;</span>SimpleListExample, ApplicationMediator<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>To map the model, or any other class that you would like to inject, you will use the context's injector. The injector has several methods for mapping classes and values for injection. The mapSingleton() method is very common for mapping a simple class for injection. It should be noted that the term "singleton" in this context is not referring to a Singleton in the strict sense of the design pattern. In this case, mapSingleton() means that the injector will create and supply one single instance of the AuthorModel whenever it is asked to do so. You can have as many instances of AuthorModel as you want, but the injector will only create and inject the one you asked for with the above mapping. There are several other methods for mapping with the injector, and I highly recommend reading <a href="http://github.com/tschneidereit/SwiftSuspenders#readme">Till Schneideriet's documentation</a> for the SwiftSuspenders Injector. By default this is what Robotlegs uses. The <a href="http://wiki.github.com/robotlegs/robotlegs-framework/best-practices#mappingwithinjector">Robotlegs Best Practices documentation</a> also discusses using the Injector. In addition to the documentation, we will be discussing the other injection mapping options in future articles.</p>
<p>With the model mapped for injection, we are one step closer to getting the Authors into their list. To do that, we will mediate the ListView and inject the AuthorModel into the mediator. here is the mediator for ListView:</p>
<h3>ListViewMediator.as</h3>

<div class="wp_codebox"><table><tr id="p59758"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p597code58"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ListViewMediator <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:ListView;
&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> authorModel:AuthorModel;
&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;">items</span> = authorModel.<span style="color: #0066CC;">list</span>;
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Outside of the [Inject] tag for the mediated view components we haven't looked at the use of the [Inject] tag or discussed how Robotlegs provides access to dependency injection for your classes. In the ListViewMediator you will see the view being injected. In addition to the view there is a public property called authorModel of the type AuthorModel marked with the [Inject] tag as well. When you place the [Inject] tag over a public property, when the class is created by Robotlegs it will "inject" that property based on the rules you have mapped. Note, that if you haven't supplied a rule for the injection you will receive a runtime error directing you to the problem. If this occurs, be sure to check your mapping. In this case, we have already used mapSingleton() to prepare AuthorModel for injection. </p>
<p>In the onRegister() method of the mediator that is run when the mediator has been fully constructed and provided injection we are accessing the model and assigning the list property to the items property of the view. Nice! We should see the items in the list now.</p>
<p>...well...</p>
<p>...oh ya!...</p>
<p>First we need to map the mediator:</p>
<h3>SimpleListExampleContext.as</h3>

<div class="wp_codebox"><table><tr id="p59759"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p597code59"><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>
  injector.<span style="color: #006600;">mapSingleton</span><span style="color: #66cc66;">&#40;</span>AuthorModel<span style="color: #66cc66;">&#41;</span>;
&nbsp;
  mediatorMap.<span style="color: #006600;">mapView</span><span style="color: #66cc66;">&#40;</span>ListView, ListViewMediator<span style="color: #66cc66;">&#41;</span>;
&nbsp;
  mediatorMap.<span style="color: #006600;">mapView</span><span style="color: #66cc66;">&#40;</span>SimpleListExample, ApplicationMediator<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>There we go. The ListView is mediated. Take note of the order of the mappings. The ListView mediator mapping is ABOVE the SimpleListExample (main view) mapping. The SimpleListExample is the main view, as well as the contextView. The contextView is treated a bit differently when its class is mapped. Instead of listening for the ADDED_TO_STAGE event to occur, the contextView is immediately mediated. Since the contextView is generally already on the stage, we can't reliably listen for an event that might never occur. Since we want ListView to be mediated when it is added to stage we make sure that it is mapped before the ApplicationMediators onRegister() ever has a chance to be called. If you recall, the ApplicationMediator's onRegister() is where we tell the main view to add its children, including the ListView.</p>
<p><span class="mt-enclosure mt-enclosure-image" style="display: inline;"><img alt="view-requests-from-model.png" src="http://www.developria.com/2010/06/10/view-requests-from-model.png" width="290" height="186" class="mt-image-center" style="text-align: center; display: block; margin: 0 auto 20px;" /></span> </p>
<p>So with the ListView being populated with data, you can now select from the list of Authors in the list. Pretty exciting, no? Try to contain yourself for a bit. There is still a ways to go. Now we want to fill the QuoteTextArea with some text. Preferably a quote from the selected Author. To do that, we will be making additions to the AuthorModel so that it will keep track of the selected Author. When an Author is selected in the ListView the ListViewMediator will update the AuthorModel. The AuthorModel will then send an event to notify anybody that might be listening that an Author was selected. We will need to also create a mediator for the QuoteTextArea so that it can listen for that event and update with the quote from the selected Author. We know we will need the event, so lets make that first:</p>
<h3>SelectedAuthorEvent.as</h3>

<div class="wp_codebox"><table><tr id="p59760"><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
</pre></td><td class="code" id="p597code60"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SelectedAuthorEvent <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 SELECTED:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;authorSelected&quot;</span>;
&nbsp;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _author:Author;
&nbsp;
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> author<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Author
  <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">return</span> _author;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> SelectedAuthorEvent<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">type</span>:<span style="color: #0066CC;">String</span>, author:Author = <span style="color: #000000; font-weight: bold;">null</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>;
    _author = author;
  <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> SelectedAuthorEvent<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">type</span>, author, 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>The event is rather unremarkable. It is a typical custom event with a single constant, SELECTED, for a type and an optional author parameter. Now that we have the event, we need to updated the AuthorModel to keep track of the selected Author and notify the application when the it has changed:</p>
<h3>AuthorModel.as</h3>

<div class="wp_codebox"><table><tr id="p59761"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p597code61"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _selected:Author;
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> selected<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Author
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #b1b100;">return</span> _selected;
<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> selected<span style="color: #66cc66;">&#40;</span>value:Author<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
  _selected = value;
  dispatch<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> SelectedAuthorEvent<span style="color: #66cc66;">&#40;</span>SelectedAuthorEvent.<span style="color: #006600;">SELECTED</span>, selected<span style="color: #66cc66;">&#41;</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 AuthorModel we are effectively able to track the currently selected Author. To set this property on the model we will update the ListViewMediator to listen for a selection event from the ListView and update the model when it occurs:</p>
<h3>ListViewMediator.as</h3>

<div class="wp_codebox"><table><tr id="p59762"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p597code62"><pre class="actionscript" style="font-family:monospace;">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;">items</span> = authorModel.<span style="color: #0066CC;">list</span>;
&nbsp;
  addViewListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">SELECT</span>, handleSelected<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> handleSelected<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>
  authorModel.<span style="color: #006600;">selected</span> = view.<span style="color: #006600;">selectedItem</span> as Author;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Inside of the ListViewMediator's onRegister() we will use the addViewListener() method to add an event listener on the view for Event.SELECT to be handled by the handleSelected method. Now, when an item in the list is selected, the event handler method will access the selected property of the authorModel and updated it with the item that was selected. That value will be set, and the AuthorModel will dispatch the event notifying the rest of the application that this has occurred.</p>
<p>"Why not just dispatch the selected event here and be done with it?"</p>
<p>You very well could, and if your application is this simple it is probably the appropriate way to go. There aren't many apps this simple in my life, and this is an example of how to use a Model, so that is the reason we are taking the seemingly long way here. And now that we have traveled down that path, we still need to get that text into the QuoteTextArea, so lets get it mediated:</p>
<h3>QuoteTextAreaMediator.as</h3>

<div class="wp_codebox"><table><tr id="p59763"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code" id="p597code63"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> QuoteTextAreaMediator <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:QuoteTextArea;
&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>SelectedAuthorEvent.<span style="color: #006600;">SELECTED</span>, handleSelectedAuthorChanged<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> handleSelectedAuthorChanged<span style="color: #66cc66;">&#40;</span>event:SelectedAuthorEvent<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> author:Author = event.<span style="color: #006600;">author</span>;
    view.<span style="color: #0066CC;">text</span> = author.<span style="color: #006600;">quote</span>;
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>So there it is. The QuoteTextArea now has a connection to the rest of the application. In its onRegister we will use the addContextListener() to listen for the SelectedAuthorEvent.SELECTED and handle it with handleSelectedAuthorChanged.</p>
<p><span class="mt-enclosure mt-enclosure-image" style="display: inline;"><img alt="view-to-model-to-view.png" src="http://www.developria.com/2010/06/10/view-to-model-to-view.png" width="455" height="186" class="mt-image-center" style="text-align: center; display: block; margin: 0 auto 20px;" /></span> </p>
<p>The handleSelectedAuthorChanged method takes the information from that event and updates the view's text property with the quote from the selected Author. After we get that mediator mapped, we will have achieved the extent of this example's functionality:</p>
<h3>SimpleListExampleContext.as</h3>

<div class="wp_codebox"><table><tr id="p59764"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p597code64"><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>
    injector.<span style="color: #006600;">mapSingleton</span><span style="color: #66cc66;">&#40;</span>AuthorModel<span style="color: #66cc66;">&#41;</span>;
&nbsp;
    mediatorMap.<span style="color: #006600;">mapView</span><span style="color: #66cc66;">&#40;</span>ListView, ListViewMediator<span style="color: #66cc66;">&#41;</span>;
    mediatorMap.<span style="color: #006600;">mapView</span><span style="color: #66cc66;">&#40;</span>QuoteTextArea, QuoteTextAreaMediator<span style="color: #66cc66;">&#41;</span>;
&nbsp;
    mediatorMap.<span style="color: #006600;">mapView</span><span style="color: #66cc66;">&#40;</span>SimpleListExample, ApplicationMediator<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>With that you have an example that covers the basics of using Models in a Robotlegs application. The guardians of your data, Models play an extremely important part in your applications. By utilizing models as the access point for your data, you isolate where the data is stored an manipulated. When you sit down to solve a problem, you know where to look for issues regarding data and its subsequent representation of the state of your application. If your data is scattered in junks all across your application it becomes a murky stew making it difficult to quickly isolate trouble spots or add functionality to the application.</p>
<p>A few pages in a blog post is a very brief introduction to models. I highly recommend diving into some research on the M in MVC. In the next example will take a look at Services, and see how they are used within a Robotlegs application. After Services we will have a look at Commands to complete the Robotlegs MVC+S implementation before moving on to some more advanced topics.</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 the forthcoming Flex 4 in Action book, which has 22 pages of Robotlegs material.</p>
]]></content:encoded>
			<wfw:commentRss>http://joelhooks.com/2011/03/12/an-introduction-to-robotlegs-as3-part-2-models/feed/</wfw:commentRss>
		<slash:comments>12</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="p58978"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p589code78"><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="p58979"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p589code79"><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="p58980"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p589code80"><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="p58981"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p589code81"><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="p58982"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p589code82"><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="p58983"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p589code83"><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="p58984"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p589code84"><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="p58985"><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="p589code85"><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="p58986"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p589code86"><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="p58987"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p589code87"><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="p58988"><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="p589code88"><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="p58989"><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="p589code89"><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="p58990"><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="p589code90"><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>20</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>Write high quality unit tested Actionscript 3 for fun and profit&#8230;</title>
		<link>http://joelhooks.com/2010/12/20/write-high-quality-unit-tested-actionscript-3-for-fun-and-profit/</link>
		<comments>http://joelhooks.com/2010/12/20/write-high-quality-unit-tested-actionscript-3-for-fun-and-profit/#comments</comments>
		<pubDate>Mon, 20 Dec 2010 23:50:00 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://joelhooks.com/?p=564</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>
For the past year or so unit testing has been one of my favorite subjects. There are tons of books on the subject, but several really stand out. Here are my favorites so far: Clean Code by Uncle Bob Martin This book does cover unit testing a bit. It isn't the focus of the book, [...]]]></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>For the past year or so unit testing has been one of my favorite subjects. There are tons of books on the subject, but several really stand out. Here are my favorites so far:</p>
<p><a href="http://joelhooks.com/wp-content/uploads/2010/12/clean-code.jpg"><img class="size-full wp-image-565 alignnone" title="clean code" src="http://joelhooks.com/wp-content/uploads/2010/12/clean-code.jpg" alt="" width="120" height="160" /></a><br />
<a href="http://www.amazon.com/exec/obidos/ASIN/0132350882/joehoobuiblo-20/ref=nosim/">Clean Code</a> by Uncle Bob Martin</p>
<p>This book does cover unit testing a bit. It isn't the focus of the book, but regardless it sits here at the top of my unit testing list. Why? Because the hardest part of writing unit tests is writing code that can be tested in the first place. Get it. Read it. Learn it. Seriously. Uncle Bob rides in the vanguard in the battle against horrible dirty code.</p>
<p><a href="http://www.amazon.com/exec/obidos/ASIN/1933988274/joehoobuiblo-20/ref=nosim/"><img class="size-full wp-image-569 alignnone" title="art of unit testing" src="http://joelhooks.com/wp-content/uploads/2010/12/art-of-unit-testing.jpg" alt="" width="128" height="160" /></a><br />
<a href="http://www.amazon.com/exec/obidos/ASIN/1933988274/joehoobuiblo-20/ref=nosim/">The Art of Unit Testing</a> by Roy Osherove</p>
<p>This is a small book, but it is stuffed to the gills with distilled information on the subject of unit testing. Don't be put off with the .NET examples. It doesn't make a difference at all. There are a couple of chapters that are .NET specific that you can just skip, but 90% of the book is killer information on testing your code.</p>
<p><a href="http://www.amazon.com/exec/obidos/ASIN/0131177052/joehoobuiblo-20/ref=nosim/"><img class="size-full wp-image-570 alignnone" title="working effectively" src="http://joelhooks.com/wp-content/uploads/2010/12/working-effectively.jpg" alt="" width="121" height="160" /></a><br />
<a href="http://www.amazon.com/exec/obidos/ASIN/0131177052/joehoobuiblo-20/ref=nosim/">Working Effectively With Legacy Code</a> by Michael Feathers</p>
<p>Like <a href="http://www.amazon.com/exec/obidos/ASIN/0132350882/joehoobuiblo-20/ref=nosim/">Clean Code</a>, this book isn't specifically focused on writing unit tests, but it totally is. Feathers defines "legacy code" as "code with no unit tests". You can quibble about the semantics of this definition, but you'd be missing the point of this kickass book. How do you take a pile of code with no tests and make it better in a safe incremental way? How do you stop producing legacy code at a furious pace? That's right, the code you wrote today is probably sitting in the "legacy" column already. Even if you are writing tight unit tested code you will likely stumble into code that isn't, and this book has tons of good advice on how to deal with it.</p>
<p><a href="http://www.amazon.com/exec/obidos/ASIN/0131495054/joehoobuiblo-20/ref=nosim/"><img class="size-full wp-image-571 alignnone" title="xUnit Patterns" src="http://joelhooks.com/wp-content/uploads/2010/12/xUnit-Patterns.jpg" alt="" width="116" height="160" /></a><br />
<a href="http://www.amazon.com/exec/obidos/ASIN/0131495054/joehoobuiblo-20/ref=nosim/">xUnit Test Patterns</a> by Gerard Meszaros</p>
<p>This one is a monster. It is a <a href="http://www.martinfowler.com/bliki/DuplexBook.html">duplex book</a> with a strong narrative up front and a massive patterns catalog bringing up the rear. if you read a lot of unit testing books you will "feel" this one under the best ones as the giant upon whom they are standing. This is source material here, and might be my personal favorite. I think <a href="http://www.amazon.com/exec/obidos/ASIN/1933988274/joehoobuiblo-20/ref=nosim">The Art of Unit Testing</a> makes for an easier (quicker anyway) read and if you were to choose one or the other go for it. Don't choose one or the other though. Read both!</p>
<p><a href="http://www.amazon.com/exec/obidos/ASIN/0321503627/joehoobuiblo-20/ref=nosim/"><img class="size-full wp-image-572 alignnone" title="growing oos" src="http://joelhooks.com/wp-content/uploads/2010/12/growing-oos.jpg" alt="" width="120" height="160" /></a><br />
<a href="http://www.amazon.com/exec/obidos/ASIN/0321503627/joehoobuiblo-20/ref=nosim/">Growing Object Oriented Software Guided by Tests</a> by Steve Freeman</p>
<p>While the above books touch on the concept of TDD it isn't their focus. This relatively small volume is a great introduction to the subject of test-driving your code. It steps out of simple unit tests and gets into the realm of integration and acceptance testing. It has a fabulous explanation of mocking and is a fantastic introduction to the "testing layer cake" that high quality maintainable applications are guarded with.</p>
<p>So what is the take away from all of these books? Unit testing is actually pretty easy. Writing clean testable code is harder. We (developers) tend to complicate unit tests a lot. We write them as integration, acceptance, or other more complex test types that are orders of magnitude harder to write and maintain. A unit should be tiny and easy to test. If it isn't, then something is probably wrong with the unit or the scope of the test. Unit tests are <strong>for developers by developers</strong> and shouldn't be considered an afterthought or some line item on a managers spreadsheet. A high percentage of test coverage should <strong>not</strong> be the goal. Quality over quantity. Confusing unmaintainable tests are a liability to a project. Concise readable unit tests can be a trusted asset.</p>
<p>As with all code, complexity is easy. It is the default. Elegant simplicity is much more difficult. At the end of the day, cramming all the information the above books offer into my head has me writing dead simple unit tests. It seems weird as I examine it, but that's how it has worked out. Study up and write some high quality tests. You will be well thanked by both your fellow developers and your future self.</p>
]]></content:encoded>
			<wfw:commentRss>http://joelhooks.com/2010/12/20/write-high-quality-unit-tested-actionscript-3-for-fun-and-profit/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
	</item>
		<item>
		<title>Do we really need THAT much metadata in AS3? Not with Robotlegs&#8230;</title>
		<link>http://joelhooks.com/2010/06/16/do-you-need-metadata-as3-robotlegs/</link>
		<comments>http://joelhooks.com/2010/06/16/do-you-need-metadata-as3-robotlegs/#comments</comments>
		<pubDate>Wed, 16 Jun 2010 14:13:23 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[design patterns]]></category>
		<category><![CDATA[dev tools]]></category>
		<category><![CDATA[robotlegs]]></category>
		<category><![CDATA[software architecture]]></category>

		<guid isPermaLink="false">http://joelhooks.com/?p=527</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>
There was a time in the push for a Robotlegs 1.0 that a nometa implementation existed. Robert Penner still uses it, if I'm not mistaken. It doesn't have as much convenience as the regular meta-loving MVCS implementation, meaning it doesn't inject a bunch of classes into the MVCS base classes. Is that bad? Not really. [...]]]></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>There was a time in the push for a Robotlegs 1.0 that a nometa implementation existed. <a href="http://github.com/robertpenner">Robert Penner</a> still uses it, if I'm not mistaken. It doesn't have as much convenience as the regular meta-loving MVCS implementation, meaning it doesn't inject a bunch of classes into the MVCS base classes. Is that bad? Not really. I find myself rarely using the injected classes anyway. At the time I pushed back on the nometa implementation and its inclusion in the core. I still think that is proper, because it would just tend to cloud the implementation and make it more difficult to document and support, both of these being key pillars in making Robotlegs a successful project that people will actually use. The concept of using as little metadata as possible in my applications is very appealing.</p>
<div align="center"><a href="http://xkcd.com/754/"><img src="http://imgs.xkcd.com/comics/dependencies.png" alt="" /></a></div>
<p><span id="more-527"></span><br />
<a href="http://github.com/tschneidereit/SwiftSuspenders#readme">SwiftSuspenders</a> beautifully supports constructor injection. This means that you don't need to annotate any methods or the class with metadata for it to honor injection into the constructor. With the [Inject] tag, you are marking required dependencies. They aren't optional. If there is no rule to supply the injection you will get a runtime error saying as such. So, given these non-optional dependencies, injecting them into the constructor makes a lot of sense. One drawback to this approach is that if you are injecting a lot of dependencies your constructor might get really big. In fact, the limit is 10 parameters. Guess what? If your class requires that many dependencies, it already smells bad. Clean it up and think <a href="http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod">single responsibility</a>. For the love of Uncle Bob, keep it clean.</p>
<p>To illustrate this point I have refactored the Image Gallery example. It was my first example for Robotlegs almost a year ago. The original version resides in the Robotlegs Demo Bundle. In this refactoring I removed all occurrences of the [Inject] metadata tag. In addition, I removed all of the extensions of Robotlegs mvcs implementation sub-classes with the exception of Mediator. There are no Actor or Command subclasses. So what you end up with is a Command, Model, and Service tier that is completely meta and framework free. That rocks. It is just AS3 code doing its job. I love that.</p>

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_RobotlegsFlickrGallery_1496873436"
			class="flashmovie"
			width="700"
			height="700">
	<param name="movie" value="http://joelhooks.com/examples/galleryNoMeta/RobotlegsFlickrGallery.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://joelhooks.com/examples/galleryNoMeta/RobotlegsFlickrGallery.swf"
			name="fm_RobotlegsFlickrGallery_1496873436"
			width="700"
			height="700">
	<!--<![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>
<p><a href="http://github.com/joelhooks/robotlegs-examples-FlickrGalleryNoMeta">Here's a link to the source for this project on Github</a>.</p>
<p>Tooling is very lacking for metadata in all of the IDEs currently available. The <a href="http://www.elementriver.com/sourcemate/">SourceMate</a> plugin from ElementRiver is a nice addition to Flash Builder if you make use of metadata. It actually gives you code completion, which in and of itself is a huge improvement over... nothing. I hope to see more IDE support for metadata soon. It would really make it a lot easier to manage.</p>
<p>In my professional life, I use all of the Actionscript DI frameworks (<a href="http://www.robotlegs.org/">Robotlegs</a>, <a href="http://swizframework.org/">Swiz</a>, and <a href="http://www.spicefactory.org/parsley/">Parsley</a>). They are all great, when used appropriately within a defined set of patterns and practices. I worry about the trend for increased use of metadata. I think the complexity overhead is large. Sure, <strong>you</strong> get it, but there is a responsibility to build systems that can be understood by a wide range of developers without weeks of remedial training on a complex metadata based <a href="http://en.wikipedia.org/wiki/Domain-specific_language">DSL</a>. The less complex we make our complex systems the better. They are easier to train for, document, and build.</p>
]]></content:encoded>
			<wfw:commentRss>http://joelhooks.com/2010/06/16/do-you-need-metadata-as3-robotlegs/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
	</item>
		<item>
		<title>Slides from Flash and the City Presentation on Robotlegs AS3</title>
		<link>http://joelhooks.com/2010/05/18/slides-from-flash-and-the-city-presentation-on-robotlegs-as3/</link>
		<comments>http://joelhooks.com/2010/05/18/slides-from-flash-and-the-city-presentation-on-robotlegs-as3/#comments</comments>
		<pubDate>Wed, 19 May 2010 01:19:52 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[robotlegs]]></category>

		<guid isPermaLink="false">http://joelhooks.com/?p=510</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>
]]></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>
<div align="center">
<div style="width:425px" id="__ss_4145254"><object id="__sse4145254" width="425" height="355"><param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=fatc2010-100518193313-phpapp01&#038;stripped_title=fatc-2010" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed name="__sse4145254" src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=fatc2010-100518193313-phpapp01&#038;stripped_title=fatc-2010" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="355"></embed></object></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://joelhooks.com/2010/05/18/slides-from-flash-and-the-city-presentation-on-robotlegs-as3/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
	</item>
		<item>
		<title>Modular Robotlegs</title>
		<link>http://joelhooks.com/2010/05/02/modular-robotlegs/</link>
		<comments>http://joelhooks.com/2010/05/02/modular-robotlegs/#comments</comments>
		<pubDate>Mon, 03 May 2010 04:08:50 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[libraries]]></category>
		<category><![CDATA[robotlegs]]></category>
		<category><![CDATA[software architecture]]></category>

		<guid isPermaLink="false">http://joelhooks.com/?p=477</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>
WTF is a Modular? Modular programming is a versatile technique for separating an application into smaller parts. Each module is effectively an application and can be developed independently from one another. In a typical modular application you will have a Shell that is loaded initially. The Shell will manage the loading of modules and displaying [...]]]></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>
<h2>WTF is a Modular?</h2>
<p>Modular programming is a versatile technique for separating an application into smaller parts. Each module is effectively an application and can be developed independently from one another. In a typical modular application you will have a Shell that is loaded initially. The Shell will manage the loading of modules and displaying their contents. Flex modules can be visual components that extend the <a href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/modules/Module.html">Module</a> class, but this is certainly not the extent of what can be considered a module.<br />
<img src="http://69.164.207.135/wp-content/uploads/2010/05/ModularUtility4.png" alt="" title="ModularUtility" width="472" height="249" class="aligncenter size-full wp-image-502" /><br />
Robotlegs is well suited for modular application development. Each Context is encapsulated and provides its own internal method for communication. A barrier to writing modular applications thus far with Robotlegs is that while these Context objects can exist in abundance in an application, they aren't very useful from a modular standpoint unless they can communicate with one another. As with many things within the Robotlegs eco-system, this is best accomplished through the development of a utility that can be used in conjunction with the core framework to provide the specific functionality that we are looking for. There has been some good work done in this area, but with Robotlegs 1.1 and more specifically the 1.5 release of the default Dependency Injection provider SwiftSuspenders, we have been equipped with better tools to accomplish modular contexts in a Robotlegs application in a clean effective manner.<br />
<span id="more-503"></span></p>
<h2>A Little Backstory</h2>
<p>Prior to Robotlegs I used <a href="http://puremvc.org">PureMVC</a> exclusively. There is a utility for PureMVC called <a href="http://trac.puremvc.org/Utility_AS3_MultiCore_Pipes">Pipes</a>, which I have <a href="http://joelhooks.com/2009/05/18/piping-the-machine-puremvc-multicore-with-pipes-and-the-finite-state-machine-fsm/">written about previously</a>. Pipes is pretty cool. It uses a plumbing metaphor to describe the connections between modules. It is... verbose... and requires a shit-ton of wiring code to be functional. This can be a challenge to get your head around, and can create code that you have to stare at for a good while to fully understand HTF everything is actually being wired.</p>
<p>My original path for a Robotlegs modular utility was to port Pipes. In fact, I didn't really have to port Pipes, but just wrote an adapter that allowed Pipes to fit into a Robotlegs application. The problem was that it didn't feel like Robotlegs. Verbose and confusing goes against the core moral fiber of what Robotlegs is all about. So I started whittling it down. At that point it looked like <a href="http://github.com/Stray/robotlegs-utilities-Modular">Stray's excellent work with the Robotlegs Modular utility</a>. Heh. Full Circle! So instead of using Pipes, I've taken her work and expanded on it for Robotlegs 1.1 and at the same time clarifying some of the concepts within the utility and making it more useful across a broader range of use cases.</p>
<h2>Where we are now</h2>
<p>So with Pipes set aside for now, we have a dead simple modular implementation that can be used for Flex, AS3, and maybe even Flash applications. It doesn't provide all of the functionality that Pipes brings to the table. Specifically it is missing concepts like message filtering and queuing. While these are likely useful tools, I didn't feel that they needed to be implemented just yet. I have some ideas about how they might be implemented, but feel that the Robotlegs Modular Utility is clean and simple covering a big majority of typical use cases. Start simple and expand from there. This is where I am relying on you, gentle reader, to help guide the utility into something more useful while still keeping the clean and simple Robotlegs aesthetic.</p>
<p>With that behind us, lets look at an example of a modular application written with the <a href="http://github.com/joelhooks/robotlegs-utilities-Modular">Robotlegs Modular Utility</a>. It is fairly useless, but covers the core concepts of what a modular application should be able to do.</p>
<h2>Modular Doodads: A Lame but Functional Example</h2>
<div align="center">
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_ModularDoodads_1631941975"
			class="flashmovie"
			width="450"
			height="550">
	<param name="movie" value="http://joelhooks.com/examples/modularDoodads/ModularDoodads.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://joelhooks.com/examples/modularDoodads/ModularDoodads.swf"
			name="fm_ModularDoodads_1631941975"
			width="450"
			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><strong><a href="http://github.com/joelhooks/robotlegs-examples-ModularDoodads">The full source for this example can be found HERE...</a></strong></p>
<div class="note-block">It was noted in the comments that this gets sluggish after more than a few Doodads are added. This is a Flex invalidation issue. <a href="http://joelhooks.com/examples/modularDoodadsAS3/ModularDoodads.html">Here is the example in pure AS3 with MinimalComps (view source is enabled)</a></div>
<p>In this example you have three separate modules: the Shell, or the main application, a logging module that provides "console" output, and Doodads. Doodads are simple modules with not a whole lot of functionality. When you add a Doodad the shell creates a new Doodad and adds it to a container. The Doodads have a "request" button that will ask any other Doodads to change color. In addition to that, they have a close button to remove the Doodad. Above the Doodad container is a "trigger" button. This sends out an event that triggers a command on all of the Doodad modules (causing them to flash violently).</p>
<h3>ModularDoodadsContext.as</h3>

<div class="wp_codebox"><table><tr id="p50394"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p503code94"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ModularDoodadsContext <span style="color: #0066CC;">extends</span> ModuleContext
<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;">//map the modules so that instances will be properly supplied (injected) with an injector.</span>
        viewMap.<span style="color: #006600;">mapType</span><span style="color: #66cc66;">&#40;</span>LoggerModule<span style="color: #66cc66;">&#41;</span>;
        viewMap.<span style="color: #006600;">mapType</span><span style="color: #66cc66;">&#40;</span>DoodadModule<span style="color: #66cc66;">&#41;</span>;
&nbsp;
        mediatorMap.<span style="color: #006600;">mapView</span><span style="color: #66cc66;">&#40;</span>ModularDoodads, ModuleDoodadsMediator<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 main shell application context context is doing a couple of things. As with the other modules within the application, this Context extends ModuleContext. ModuleContext will create the ModuleEventDispatcher (IModuleEventDispatcher) as well as a ModuleCommandMap that can be used to map commands that respond to events on the ModuleEventDispatcher. ModuleContext is a convenience mechanism.</p>
<p>Since the modules in this application are view components we can use the ViewMap to map their types. This facilitates injection into the modules when they are added to the stage. The reason for this will become clear as we look at one of these modules.</p>
<h3>LoggerModule.mxml</h3>

<div class="wp_codebox"><table><tr id="p50395"><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
59
60
61
62
63
64
65
66
</pre></td><td class="code" id="p503code95"><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> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;</span>mx:Module 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;">implements</span>=<span style="color: #ff0000;">&quot;org.robotlegs.utilities.modular.core.IModule&quot;</span>
           layout=<span style="color: #ff0000;">&quot;absolute&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;75&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;">core</span>.<span style="color: #006600;">IContext</span>;
            <span style="color: #0066CC;">import</span> org.<span style="color: #006600;">robotlegs</span>.<span style="color: #006600;">core</span>.<span style="color: #006600;">IInjector</span>;
            <span style="color: #0066CC;">import</span> org.<span style="color: #006600;">robotlegs</span>.<span style="color: #006600;">utilities</span>.<span style="color: #006600;">modular</span>.<span style="color: #006600;">core</span>.<span style="color: #006600;">IModule</span>;
            <span style="color: #0066CC;">import</span> org.<span style="color: #006600;">robotlegs</span>.<span style="color: #006600;">utilities</span>.<span style="color: #006600;">modular</span>.<span style="color: #006600;">core</span>.<span style="color: #006600;">IModuleContext</span>;
&nbsp;
            <span style="color: #0066CC;">import</span> robotlegs.<span style="color: #006600;">examples</span>.<span style="color: #006600;">modulardoodads</span>.<span style="color: #006600;">modules</span>.<span style="color: #006600;">logger</span>.<span style="color: #006600;">skins</span>.<span style="color: #006600;">LoggingTextArea</span>;
&nbsp;
            protected <span style="color: #000000; font-weight: bold;">var</span> context:IModuleContext;
&nbsp;
            <span style="color: #66cc66;">&#91;</span>Embed<span style="color: #66cc66;">&#40;</span>mimeType=<span style="color: #ff0000;">'application/x-font'</span>, source=<span style="color: #ff0000;">&quot;assets/AnonPro.ttf&quot;</span>, fontName=<span style="color: #ff0000;">&quot;Anon&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
            <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> anon:<span style="color: #000000; font-weight: bold;">Class</span>;
&nbsp;
            <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;">var</span> messages:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;&quot;</span>;
&nbsp;
            <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> addLoggingMessage<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">message</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
            <span style="color: #66cc66;">&#123;</span>
                <span style="color: #0066CC;">message</span> += <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\r</span>&quot;</span>;
                messages += <span style="color: #0066CC;">message</span>;
                scrollToMax<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> scrollToMax<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
            <span style="color: #66cc66;">&#123;</span>
                messageDisplay.<span style="color: #006600;">validateNow</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
                messageDisplay.<span style="color: #006600;">scroller</span>.<span style="color: #006600;">verticalScrollBar</span>.<span style="color: #006600;">value</span> = messageDisplay.<span style="color: #006600;">scroller</span>.<span style="color: #006600;">verticalScrollBar</span>.<span style="color: #006600;">maximum</span>;
            <span style="color: #66cc66;">&#125;</span>
&nbsp;
            <span style="color: #808080; font-style: italic;">/**
             * We need to initialize our context by setting the parent
             * injector for the module. This is actually injected by the
             * shell, so no need to worry about it!
            */</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;">function</span> <span style="color: #0066CC;">set</span> parentInjector<span style="color: #66cc66;">&#40;</span>value:IInjector<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
            <span style="color: #66cc66;">&#123;</span>
                context = <span style="color: #000000; font-weight: bold;">new</span> LoggerModuleContext<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>, value<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> dispose<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
            <span style="color: #66cc66;">&#123;</span>
                context.<span style="color: #006600;">dispose</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
                context = <span style="color: #000000; font-weight: bold;">null</span>;
            <span style="color: #66cc66;">&#125;</span>
&nbsp;
        <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>fx:Declarations<span style="color: #66cc66;">&gt;</span>
        <span style="color: #66cc66;">&lt;!</span>-- Place non-visual elements <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #006600;">g</span>., services, value objects<span style="color: #66cc66;">&#41;</span> here --<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:TextArea
        id=<span style="color: #ff0000;">&quot;messageDisplay&quot;</span>
        fontFamily=<span style="color: #ff0000;">&quot;Anon&quot;</span>
        fontSize=<span style="color: #ff0000;">&quot;12&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: #0066CC;">text</span>=<span style="color: #ff0000;">&quot;{messages}&quot;</span>
        skinClass=<span style="color: #ff0000;">&quot;robotlegs.examples.modulardoodads.modules.logger.skins.LoggingTextArea&quot;</span><span style="color: #66cc66;">/&gt;</span>
<span style="color: #66cc66;">&lt;/</span>mx:Module<span style="color: #66cc66;">&gt;</span></pre></td></tr></table></div>

<p>Looking at the LoggerModule.mxml it is important to note that it implements the org.robotlegs.utilities.modular.core.IModule interface. This provides a contract to ensure that we supply the appropriate API to initialize the module. The IModule interface provides a setter for the parentInjector as well as a dispose() method that we can use to cleanup the module when it is removed. The key here is the setter for the parentInjector that supplies an injector to the module. This setter actually creates the context or the module and passes the injector into the context. As you will recall in the ModularDoodadContext above, the LoggerModule was mapped with the ViewMap. This means that when it is added to the stage its dependencies are injected. parentInjector is marked with the [Inject] metadata so the injector is automatically provided to the module. The context will then use that injector and create a child injector.</p>
<h5>Child Injectors</h5>
<p>A child injector is a new concept in Robotlegs 1.1. It is a powerful tool. When the parentInjector is set, the module context uses it to create a child injector. This child injector has a reference to its parent and the parent's injection mappings. This means that if you don't create a mapping in the child injector it will supply the injection a mapped within the parent (or grandparent). The parent has no reference at all to the child, and doesn't even know that it exists so if you create mappings in a child injector they are not reflected up the injector chain to ancestors. If you create a mapping that is identical to a mapping within an injector's family tree, the first mapping is honored and the injector will not check with its ancestors to see if the mapping exists.</p>
<p>Through this mechanism of child injectors we are able to map an event dispatcher within the top level application context that can then be shared amongst any number of other contexts providing that they are able to create and use a child injector. This is the core of how these modules are able to communicate with one another. We create the IModuleEventDispatcher in the shell and all sub-modules now have access to it through their injector. As long as you don't map a IModuleEventDispatcher with the child, any injections that call for it will be supplied with the original dispatcher from the parent injector.</p>
<p> Now what? For the most part, you develop modules <a href="http://wiki.github.com/robotlegs/robotlegs-framework/best-practices">as you would any other Robotlegs application</a>. The Modular Utility does provide one other convenient mechanism to help save you on some typing. That is the ModuleMediator.</p>
<h3>DoodadModuleMediator.as</h3>

<div class="wp_codebox"><table><tr id="p50396"><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
</pre></td><td class="code" id="p503code96"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DoodadModuleMediator <span style="color: #0066CC;">extends</span> ModuleMediator
<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:DoodadModule;
&nbsp;
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> madeRequest:<span style="color: #0066CC;">Boolean</span>;
&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>DoodadModuleEvent.<span style="color: #006600;">DO_STUFF_REQUESTED</span>, handleDoStuffRequested, DoodadModuleEvent<span style="color: #66cc66;">&#41;</span>;
        addViewListener<span style="color: #66cc66;">&#40;</span>DoodadModuleEvent.<span style="color: #006600;">REMOVE</span>, handleRemove<span style="color: #66cc66;">&#41;</span>;
        addModuleListener<span style="color: #66cc66;">&#40;</span>DoodadModuleEvent.<span style="color: #006600;">DO_STUFF_REQUESTED</span>, handleDoStuffRequest, DoodadModuleEvent<span style="color: #66cc66;">&#41;</span>;
        addContextListener<span style="color: #66cc66;">&#40;</span>DoodadModuleEvent.<span style="color: #006600;">FLASH_YOUR_DOODAD</span>, handleDoodadFlashRequest<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> handleRemove<span style="color: #66cc66;">&#40;</span>event:DoodadModuleEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
    <span style="color: #66cc66;">&#123;</span>
        dispatchToModules<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> LoggingEvent<span style="color: #66cc66;">&#40;</span>LoggingEvent.<span style="color: #0066CC;">MESSAGE</span>, <span style="color: #ff0000;">&quot;Removing DoodadModule&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
        dispatchToModules<span style="color: #66cc66;">&#40;</span>event<span style="color: #66cc66;">&#41;</span>;
        view.<span style="color: #006600;">dispose</span><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> handleDoStuffRequested<span style="color: #66cc66;">&#40;</span>event:DoodadModuleEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
    <span style="color: #66cc66;">&#123;</span>
        madeRequest = <span style="color: #000000; font-weight: bold;">true</span>;
        dispatchToModules<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> LoggingEvent<span style="color: #66cc66;">&#40;</span>LoggingEvent.<span style="color: #0066CC;">MESSAGE</span>, <span style="color: #ff0000;">&quot;DoodadModule made request...&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
        moduleDispatcher.<span style="color: #006600;">dispatchEvent</span><span style="color: #66cc66;">&#40;</span>event<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> handleDoStuffRequest<span style="color: #66cc66;">&#40;</span>event:DoodadModuleEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span>madeRequest<span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
            view.<span style="color: #0066CC;">color</span> = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">random</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">*</span>0xFFFFFF;
            dispatchToModules<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> LoggingEvent<span style="color: #66cc66;">&#40;</span>LoggingEvent.<span style="color: #0066CC;">MESSAGE</span>, <span style="color: #ff0000;">&quot;DoodadModule changed color: &quot;</span> + view.<span style="color: #0066CC;">color</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
        madeRequest = <span style="color: #000000; font-weight: bold;">false</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> handleDoodadFlashRequest<span style="color: #66cc66;">&#40;</span>event:DoodadModuleEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
    <span style="color: #66cc66;">&#123;</span>
        view.<span style="color: #006600;">flashIt</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 DoodadModuleMediator extends ModuleMediator. The ModuleMediator provides basic injections for the ModuleCommandMap and the ModuleEventDispatcher. ModuleMediator also provides some convenience with a bit of syntactic sugar to make it easy to dispatch events to the ModuleEventDispatcher. The dispatchToModules() method sends events over the ModuleEventDispatcher in contrast to the dispatch() method which sends events over the EventDispatcher that is local to the context. Additionally you are provided with addModuleListener() that makes it easy to add a listener for an event type that is expected to be dispatched from another module (including the shell). These methods simply abstract the eventMap.mapListener() method, which in turn is abstracting eventDispatcher.addEventListener(). You have nothing if not options when it comes to adding a listener within a mediator!</p>
<h2>Modular Application Development in Flex... Some Caveats</h2>
<p>Possibly the biggest gotcha with modular application development is managing memory. With a typical application you are dealing with a single "module" - the application itself. You do not typically attempt to unload your entire application from memory. Simply refreshing the page or navigating away more effectively does this. We do try to prevent memory leaks diligently (right?) to provide our users with the smoothest possible experience, but this is different from trying to release all of the memory the application uses at runtime. Modules, on the other hand, can come into existence at runtime at any point during the lifecycle of your application. On the flip side of that is that the modules should be able to completely unload and release all of the memory that they have used.</p>
<p>There are a host of "tricks" to getting modules out of memory. This <a href="http://blogs.adobe.com/tomsugden/2010/02/how_to_unload_modules_effectively.html">article</a> is a good overview of the major ones. In addition to these sneaky memory peggers, you will also want to carefully ensure that your modules release references and dispose of objects properly.</p>
<p>The Modular Utility makes every effort to provide mechanisms for disposing of your modules, but when it gets down to it most of that responsibility will be left up to you. If you notice that Robotlegs or the Modular Utility is preventing a module from unloading <b>please let me know</b> so that I can address it as soon as possible. The end goal is to provide a robust tool for developing Robotlegs applications with modules. The Modular Utility is currently very modest, but what it isn't is a solution looking for a problem. As it grows it will be in response to real problems, and your feedback and use is critical to meeting that goal.</p>
<h3>The bits you'll need...</h3>
<p><a href="http://github.com/joelhooks/robotlegs-utilities-Modular">Robotlegs Modular Utility (my fork)</a><br />
<a href="http://github.com/joelhooks/robotlegs-examples-ModularDoodads">The full source for this example</a><br />
<a href="http://www.robotlegs.org">Robotlegs AS3</a></p>
<h3>On a related note...</h3>
<p>I recently had the pleasure of writing a full 22 pages on Robotlegs in the upcoming <a href="http://www.manning.com/ahmed2/">Flex 4 in Action from Manning</a>. It is a great book overall and if you'd like to learn more about Robotlegs (or Flex 4 in general) then I highly recommend it.</p>
]]></content:encoded>
			<wfw:commentRss>http://joelhooks.com/2010/05/02/modular-robotlegs/feed/</wfw:commentRss>
		<slash:comments>53</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
	</item>
	</channel>
</rss>

