<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Obecto Training Portal &#187; controller</title>
	<atom:link href="http://training.obecto.com/tag/controller/feed/" rel="self" type="application/rss+xml" />
	<link>http://training.obecto.com</link>
	<description>sharing our knowledge</description>
	<lastBuildDate>Wed, 24 Nov 2010 16:09:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>MVCS Example</title>
		<link>http://training.obecto.com/2009/05/mvcs-example/</link>
		<comments>http://training.obecto.com/2009/05/mvcs-example/#comments</comments>
		<pubDate>Thu, 07 May 2009 10:24:58 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[VMware Flex]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[model]]></category>
		<category><![CDATA[MVCS]]></category>
		<category><![CDATA[services]]></category>
		<category><![CDATA[view]]></category>

		<guid isPermaLink="false">http://training.obecto.com/?p=169</guid>
		<description><![CDATA[DOWNLOAD MVC EXAMPLE Just as I&#8217;ve promised an example MVC module was demonstrated and discussed in today&#8217;s session. The example is far from perfect but it provides a fundament for discussing various design decisions when we implement an MVC example. Sample Requirements The request is to implement a Programmer Statistics &#8230; <a href="http://training.obecto.com/2009/05/mvcs-example/">More</a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://training.obecto.com/wp-content/uploads/2009/05/MVCExample.zip">DOWNLOAD MVC EXAMPLE</a></p>
<p>Just as I&#8217;ve promised an example MVC module was demonstrated and discussed in today&#8217;s session. The example is far from perfect but it provides a fundament for discussing various design decisions when we implement an MVC example.</p>
<h3>Sample Requirements</h3>
<p>The request is to implement a <strong>Programmer Statistics Module</strong> with the following requirements:</p>
<ul>
<li>the module should provide basic and advanced view for a list of programmers</li>
<li>use text input to filter the list</li>
<li>use checkbox control to switch between the basic and advanced view</li>
<li>in advanced view the user can delete programmers</li>
<li>in advanced view the user can show the programmers statistics</li>
<li>programmers statistics should be represented by a chart showing the lines of code written in a month by the programmer</li>
<li>switching back to basic view should make all expanded items to collapse their statistics view</li>
</ul>
<p><a href="http://training.obecto.com/wp-content/uploads/2009/05/mvc-bin-release/MVCExample.html">RUN THE MVC EXAMPLE</a><br />
<a href="http://training.obecto.com/wp-content/uploads/2009/05/mvc-bin-release/srcview/index.html">VIEW MVC EXAMPLE SOURCE CODE</a></p>
<p>As you look at the working application, you must be able to isolate the <strong>Model</strong>, the <strong>View</strong>, the <strong>Controller</strong> and the <strong>Services</strong> layers.</p>
<h3>The Model</h3>
<p>What&#8217;s comprising the model?</p>
<ul>
<li>collection of programmers&#8217; data</li>
<li>a flag indicating whether we&#8217;re in advanced view</li>
</ul>
<p>Keep in mind that the list of programmers is pure data &#8211; a single item from the list has only data properties and no representation related properties whatsoever.</p>
<h3>The View</h3>
<p>Among all other controls the View also contains a <strong>List</strong> component. The abstraction of the list component can be explained basically by the following 3 things:</p>
<ul>
<li>data provider &#8211; a collection of data items &#8211; the List observes all changes happening to this collection and reacts correspondingly by adding or removing list items</li>
<li>item renderer &#8211; a factory for creating item renderer components &#8211; the instances created by the factory we call list items</li>
<li>layout strategy &#8211; a strategy for arranging the positions and dimensions of the list items in the list</li>
</ul>
<p>The List component is the only component that communicates directly with the list items. This is how the List adds a single list item:</p>
<pre class="prettyprint" style="font-size: 12px; overflow: auto;">private function addItem(itemData : Object) : void
{
    var item : IDataRenderer = itemRenderer.newInstance() as IDataRenderer;
    item.data = itemData;

    listItems.addItem(item);
    dataToItemMap[itemData] = item;

    UIComponent(item).addEventListener(ResizableItemChangeEvent.DIMENSIONS_CHANGE, handleItemDimensionsChange);

    addChild(UIComponent(item));
}</pre>
<p/>
In a way the item&#8217;s <em>data</em> property is the item&#8217;s <em>model</em>, but since we feed the List with a collection of programmers&#8217; data items, which are pure data this brings the following problem:</p>
<blockquote><p>How do we initialize all of the properties in the Model of the item renderers&#8217; instances?</p></blockquote>
<p>One solution to this problem is the application of a pattern called Data Binding Adapter.</p>
<h3>The Data Binding Adapter Pattern</h3>
<p>The data binding adapter pattern is a way of structuring data binding expressions.<br />
Currently the List is initialized in the following way:</p>
<pre class="prettyprint" style="font-size: 12px; overflow: auto;">&lt;list:CustomLayoutList id="list" styleName="programmersList" width="550"
    layout="{model.inAdvancedView ? verticalLayout : fluidLayout}"
    dataProvider="{model.programmers}"
    itemRenderer="{new ClassFactory(ProgrammerListItem)}"/&gt;</pre>
<p/>
Let&#8217;s try to adapt the collection:</p>
<pre class="prettyprint" style="font-size: 12px; overflow: auto;">&lt;adapter:ProgrammerListItemAdapter id="adaptedModel"
    programmers="{model.programmers}"
    inAdvancedView="{model.isAdvancedView}"/&gt;</pre>
<p/>
The adapter is bound to the properties of the original Model and can take actions when the original Model changes &#8211; e.g. when the <em>inAdvancedView</em> property changes to <em>false</em> the adapter goes through each item of the itemsModels collection and changes the <strong>statisticsShown</strong> property to false:</p>
<pre class="prettyprint" style="font-size: 12px; overflow: auto;">public var itemsModels : ArrayCollection /* of ProgrammerListItemModel */

private function set inAdvancedView(value : Boolean) : void
{
    if (!value)
    {
        for each (var model : ProgrammerListItemModel in itemsModels)
        {
            model.statisticsShown = false;
        }
    }
}</pre>
<p> 
<p/>
And finally we bind the List to the <em>adaptedModel.itemsModels</em> instead of <em>model.programmers</em>:</p>
<pre class="prettyprint" style="font-size: 12px; overflow: auto;">&lt;list:CustomLayoutList id="list" styleName="programmersList" width="550"
    layout="{model.inAdvancedView ? verticalLayout : fluidLayout}"
    dataProvider="{adaptedModel.itemsModels}"
    itemRenderer="{new ClassFactory(ProgrammerListItem)}"/&gt;</pre>
<p/>
Tell me what you think about this approach?</p>
<h3>Containers</h3>
<p>Let&#8217;s take a look at the <strong>StatisticsModule.mxml</strong> where all components are wired to each other:</p>
<pre class="prettyprint" style="font-size: 12px; overflow: auto;">&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;mx:Canvas
    backgroundColor="0xffffff"
    creationComplete="controller.initializeModel();"
    xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:model="module.statistics.model.*" xmlns:view="module.statistics.view.*"
    xmlns:control="module.statistics.control.*" xmlns:service="module.statistics.service.*"&gt;

    &lt;mx:Style source="module/statistics/style/statistics.css"/&gt;

    &lt;model:StatisticsModel id="model"/&gt;

    &lt;view:StatisticsView id="view" width="600" height="100%"
        model="{model}"
        viewModeChange="controller.switchViewMode();"
        deleteButtonClick="controller.deleteListItem(event.target);"
        requestStatistics="controller.loadProgrammerStatistics(event.programmer);"
        filterChange="controller.filterProgrammers();"/&gt;

    &lt;control:StatisticsController id="controller"
        model="{model}" view="{view}"
        programmersService="{services.programmersService}"
        statisticsService="{services.statisticsService}"/&gt;

    &lt;service:Services id="services"/&gt;

&lt;/mx:Canvas&gt;</pre>
<p/>
Notice anything strange or troubling?<br />
The question is:</p>
<blockquote><p>Do I have to wire the Model, View, Controller and Services inside of a Canvas container? Isn&#8217;t it the View, the only View in the MVC? Why is my MVC a container itself?</p></blockquote>
<p>You can do two different things in this case:</p>
<ol>
<li>Strip out the StatisticsView component and include its composite components directly in the module root Canvas. Thus we eliminate one redundant container. A negative side effect of this modification is that the view is not isolated in a self-container component. The following approach is better:</li>
<li>Use a non-visual component for a root tag. When using a non-visual component for a root tag, there is an extra programming effort to add the View to the application container:</li>
</ol>
<pre class="prettyprint" style="font-size: 12px; overflow: auto;">
private function addStatisticsModule() : void
{
    var module : StatisticsModule = new StatisticsModule();
    container.addChild(module.view);
}
</pre>
<p/>
]]></content:encoded>
			<wfw:commentRss>http://training.obecto.com/2009/05/mvcs-example/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

