<?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>Vinothbabu&#039;s Desk! &#187; ActionScript 3.0</title>
	<atom:link href="http://vinothbabu.com/category/flex/actionscript-3-0/feed/" rel="self" type="application/rss+xml" />
	<link>http://vinothbabu.com</link>
	<description>When Smart becomes Dumb!</description>
	<lastBuildDate>Thu, 31 Mar 2011 09:53:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.4</generator>
		<item>
		<title>All about ArrayCollection &#8211; Flex</title>
		<link>http://vinothbabu.com/2010/09/01/all-about-arraycollection-flex/</link>
		<comments>http://vinothbabu.com/2010/09/01/all-about-arraycollection-flex/#comments</comments>
		<pubDate>Wed, 01 Sep 2010 10:28:36 +0000</pubDate>
		<dc:creator>Sachin</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[Adobe Flex]]></category>
		<category><![CDATA[featured]]></category>

		<guid isPermaLink="false">http://vinothbabu.com/?p=343</guid>
		<description><![CDATA[Today i was feeling very bored, then finally decided to get into one of the basics of Flex. Yes its ArrayCollection. Before we go into ArrayCollection, one must construe Array.Array Array is a data structure consisting of a group of elements that are accessed via indexing. It can store simple data types such as number, [...]]]></description>
			<content:encoded><![CDATA[<p>Today i was feeling very bored, then finally decided to get into one of the basics of Flex. Yes its ArrayCollection. Before we go into ArrayCollection, one must construe Array.<span id="more-343"></span><strong>Array</strong></p>
<p>Array is a data structure consisting of a group of elements that are accessed via indexing. It can store simple data types such as number, boolean, string or even complex data type such as other objects or other Arrays.</p>
<p>[codesyntax lang="actionscript3"]</p>
<pre>private var myArray:Array = new Array();
myArray[0] = "vinoth"; // string
myArray[1] = 2; // number
myArray[2] = new Object(); // object</pre>
<p>[/codesyntax]</p>
<p>The above example shows how we define an Array in ActionScript. Now that we have declared an array, lets see how we can retrive the value.</p>
<p>[codesyntax lang="actionscript3"]</p>
<pre>// retrieving the value of an array element
private var name:String = myArray[0];</pre>
<p>[/codesyntax]</p>
<p>Arrays are dynamic and, therefore, can be resized simply by adding or removing elements.</p>
<p>[codesyntax lang="actionscript3"]</p>
<pre>// adds an element to the end of an array and increases the array length by one
myArray.push("Vijay");

// removes the last element from an array and decreases the array length by one
myArray.pop();</pre>
<p>[/codesyntax]</p>
<p>Arrays can also be built using MXML:</p>
<p>[codesyntax lang="php"]</p>
<pre>&lt;mx:Array id="myArray"/&gt; &lt;!--creates an array with zero elements--&gt;

&lt;mx:Array id="myArray"&gt; &lt;!--creates an array with three elements--&gt;
   &lt;mx:String&gt;Vinoth&lt;/mx:String&gt; &lt;!--string--&gt;
   &lt;mx:Number&gt;98&lt;/mx:Number&gt; &lt;!--floating-point number--&gt;
   &lt;mx:Object name="Vijay" age="25"/&gt; &lt;!--object--&gt;
&lt;/mx:Array&gt;</pre>
<p>[/codesyntax]</p>
<p>Array is an object and as such it contains some usefel properties and methods. The length property represents the number of array elements.</p>
<p>[codesyntax lang="actionscript3"]</p>
<pre>private var myArray:Array = new Array(10);
private var numberOfArrayElements:uint = myArray.length; // 10</pre>
<p>[/codesyntax]</p>
<p>By utilizing the methods of the Array class, you can perform a number  of operations on the Array elements or the Array itself. Popular Array  methods include:</p>
<p>* <a href="http://livedocs.adobe.com/flex/3/langref/Array.html#filter%28%29">filter</a><br />
* <a href="http://livedocs.adobe.com/flex/3/langref/Array.html#indexOf%28%29">indexOf</a><br />
* <a href="http://livedocs.adobe.com/flex/3/langref/Array.html#join%28%29">join</a><br />
* <a href="http://livedocs.adobe.com/flex/3/langref/Array.html#push%28%29">push</a><br />
* <a href="http://livedocs.adobe.com/flex/3/langref/Array.html#reverse%28%29">reverse</a><br />
* <a href="http://livedocs.adobe.com/flex/3/langref/Array.html#slice%28%29">slice</a><br />
* <a href="http://livedocs.adobe.com/flex/3/langref/Array.html#sort%28%29">sort</a><br />
* <a href="http://livedocs.adobe.com/flex/3/langref/Array.html#toString%28%29">toString</a></p>
<p>Though Array has powerful features, but the inability to support data binding is a significant shortcoming. For this we have something called <strong>ArrayCollection</strong>.</p>
<p><strong>Array Collection<br />
</strong></p>
<p>Array Collection is a “wrapper class that exposes an Array as a collection that can be accessed and manipulated using the methods and properties of the ICollectionView or IList interfaces”.</p>
<p>To participate in data binding, an object must be able to:</p>
<p>1. It should dispatch event when something changes in the object.<br />
2. It should allow other objects to listen and respond to events.</p>
<p>[codesyntax lang="actionscript3"]</p>
<pre>// creates a bindable ArrayCollection with zero elements
[Bindable] private var myArrayCollection:ArrayCollection = new ArrayCollection();</pre>
<p>[/codesyntax]</p>
<p>An ArrayCollection can be created in one of three ways:</p>
<p>[codesyntax lang="actionscript3"]</p>
<pre>// creating ArrayCollection with zero elements
private var myArrayCollection:ArrayCollection = new ArrayCollection();
// passing an existing Array into an ArrayCollection via the constructor
private var myArrayCollection:ArrayCollection = new ArrayCollection(myArray);
// assigning an existing Array to the source property of an ArrayCollection
private var myArrayCollection:ArrayCollection = new ArrayCollection();
myArrayCollection.source = myArray;</pre>
<p>[/codesyntax]</p>
<p>Extending the <a href="http://livedocs.adobe.com/flex/3/langref/mx/collections/ListCollectionView.html">ListCollectionView </a>class  that implements the ICollectionView and IList interfaces, an  ArrayCollection inherits a handful of properties, methods and events.  Therefore, your plain old Array now has a new set of functionality.</p>
<p>New properties include (but not limited to):</p>
<p>* <a href="http://livedocs.adobe.com/flex/3/langref/mx/collections/ListCollectionView.html#filterFunction">filterFunction</a><br />
* <a href="http://livedocs.adobe.com/flex/3/langref/mx/collections/ListCollectionView.html#length">length</a><br />
* <a href="http://livedocs.adobe.com/flex/3/langref/mx/collections/ListCollectionView.html#list">list</a><br />
* <a href="http://livedocs.adobe.com/flex/3/langref/mx/collections/ListCollectionView.html#sort">sort</a><br />
* <a href="http://livedocs.adobe.com/flex/3/langref/mx/collections/ArrayCollection.html#source">source</a></p>
<p>New methods include (but not limited to):</p>
<p>* <a href="http://livedocs.adobe.com/flex/3/langref/mx/collections/ListCollectionView.html#addEventListener%28%29">addEventListener</a><br />
* <a href="http://livedocs.adobe.com/flex/3/langref/mx/collections/ListCollectionView.html#addItem%28%29">addItem</a><br />
* <a href="http://livedocs.adobe.com/flex/3/langref/mx/collections/ListCollectionView.html#addItemAt%28%29">addItemAt</a><br />
* <a href="http://livedocs.adobe.com/flex/3/langref/mx/collections/ListCollectionView.html#contains%28%29">contains</a><br />
* <a href="http://livedocs.adobe.com/flex/3/langref/mx/collections/ListCollectionView.html#getItemAt%28%29">getItemAt</a><br />
* <a href="http://livedocs.adobe.com/flex/3/langref/mx/collections/ListCollectionView.html#getItemIndex%28%29">getItemIndex</a><br />
* <a href="http://livedocs.adobe.com/flex/3/langref/mx/collections/ListCollectionView.html#refresh%28%29">refresh</a><br />
* <a href="http://livedocs.adobe.com/flex/3/langref/mx/collections/ListCollectionView.html#removeAll%28%29">removeAll</a><br />
* <a href="http://livedocs.adobe.com/flex/3/langref/mx/collections/ListCollectionView.html#removeItemAt%28%29">removeItemAt</a><br />
* <a href="http://livedocs.adobe.com/flex/3/langref/mx/collections/ListCollectionView.html#setItemAt%28%29">setItemAt</a></p>
<p>Once an Array is wrapped inside an ArrayCollection, the properties  and methods of the Array class are no longer accessible unless you  access them through the Array object directly. For example, if you  really want to use the Array’s push method, you could do the following:</p>
<pre>[codesyntax lang="actionscript3"]
<pre>myArrayCollection.source.push("new element");</pre>
<p>[/codesyntax]</pre>
<p>Using an ArrayCollection in place of or in tandem with an existing  Array provides Flex developers with a valuable and responsive data  storage and retrieval mechanism.</p>
<pre></pre>
]]></content:encoded>
			<wfw:commentRss>http://vinothbabu.com/2010/09/01/all-about-arraycollection-flex/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating flex components in runtime using XML</title>
		<link>http://vinothbabu.com/2010/05/18/creating-flex-components-in-runtime-using-xml/</link>
		<comments>http://vinothbabu.com/2010/05/18/creating-flex-components-in-runtime-using-xml/#comments</comments>
		<pubDate>Tue, 18 May 2010 20:36:22 +0000</pubDate>
		<dc:creator>Sachin</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[Adobe Flex]]></category>
		<category><![CDATA[featured]]></category>

		<guid isPermaLink="false">http://vinothbabu.com/?p=296</guid>
		<description><![CDATA[Today i was going through an requirement on how to create components in runtime where we have parse an XML as input and then create components in the stage.[codesyntax lang="actionscript3" container="none"] &#60;?xml version="1.0" encoding="utf-8"?&#62; &#60;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="creationCompleteHandler()"&#62; &#60;mx:Script&#62; &#60;![CDATA[ import flash.display.DisplayObject; import mx.core.UIComponent; import mx.controls.ComboBox; ComboBox; import mx.controls.RadioButton; RadioButton; import mx.controls.CheckBox; CheckBox; import mx.controls.TextArea; TextArea; [...]]]></description>
			<content:encoded><![CDATA[<p>Today i was going through an requirement on how to create components in runtime where we have parse an XML as input and then create components in the stage.<span id="more-296"></span>[codesyntax lang="actionscript3" container="none"]</p>
<pre>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
 creationComplete="creationCompleteHandler()"&gt;

 &lt;mx:Script&gt;
 &lt;![CDATA[

 import flash.display.DisplayObject;
 import mx.core.UIComponent;
 import mx.controls.ComboBox; ComboBox;
 import mx.controls.RadioButton; RadioButton;
 import mx.controls.CheckBox; CheckBox;
 import mx.controls.TextArea; TextArea;

 protected function creationCompleteHandler():void
 {
 var components:Array = getComponentsFromXML(xml.component);
 var i:int = 0;
 var n:int = components.length;
 for (i; i &lt; n; i++)
 {
 panel.addChild(components[i] as DisplayObject);
 }
 }

 /**
 *  Parses an XML string, returns array of new components.
 */
 public function getComponentsFromXML(components:XMLList):Array
 {
 var result:Array = [];
 var child:Object;
 var component:UIComponent;
 var type:String;
 var clazz:Class;
 var i:int = 0;
 var n:int = components.length();
 for (i; i &lt; n; i++)
 {
 child = components[i];
 type = child.@type;
 try {
 clazz = flash.utils.getDefinitionByName(type) as Class;
 } catch (error:ReferenceError) {
 traceImportError(type);
 }

 component = new clazz(); // dynamic

 var properties:XMLList = child.elements();
 var property:XML;
 var name:String;
 var value:Object;

 // for each child node
 for each (property in properties)
 {
 name = property.localName();
 value = property.toString();
 // create a more generic method to convert
 // strings to numbers and whatnot
 // this is a regular expression matching any digit
 // check out rubular.com
 if (/\d+/.test(value.toString()))
 value = Number(value);

 if (property.attribute("isStyle") == "true")
 component.setStyle(name, value);
 else
 component[name] = value;
 }
 result.push(component);
 }
 return result;
 }

 protected function traceImportError(type:String):void
 {
 trace("Please include the class '" + type + "' in the swf.");
 var names:Array = type.split(".");
 var last:String = names[names.length - 1];
 trace("import " + type + "; " + last + ";");
 }

 ]]&gt;
 &lt;/mx:Script&gt;

 &lt;!-- sample data --&gt;
 &lt;mx:XML id="xml" source="components.xml" /&gt;

 &lt;!-- sample container --&gt;
 &lt;mx:Panel id="panel" width="100%" height="100%"/&gt;

&lt;/mx:Application&gt;</pre>
<p>[/codesyntax]</p>
<p><a href="http://www.vinothbabu.com/Flex-Samples/creatingComponentsFromXML/creatingComponentsFromXML.html" target="_blank">Live Demo</a></p>
<p><a href="http://www.vinothbabu.com/Flex-Samples/creatingComponentsFromXML.zip" target="_self">Application source</a></p>
]]></content:encoded>
			<wfw:commentRss>http://vinothbabu.com/2010/05/18/creating-flex-components-in-runtime-using-xml/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Design Patterns : Singleton Pattern</title>
		<link>http://vinothbabu.com/2010/04/15/design-patterns-singleton-pattern/</link>
		<comments>http://vinothbabu.com/2010/04/15/design-patterns-singleton-pattern/#comments</comments>
		<pubDate>Thu, 15 Apr 2010 04:40:59 +0000</pubDate>
		<dc:creator>Sachin</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Adobe Flex]]></category>
		<category><![CDATA[Chess board]]></category>
		<category><![CDATA[Singleton Pattern]]></category>

		<guid isPermaLink="false">http://vinothbabu.com/?p=283</guid>
		<description><![CDATA[The Singleton pattern is very similar to creator pattern. This is very widely used pattern. Now a question might raise in the mind that why do we need this pattern and when it should be applied. Let us take a situation where you want to build a chess game. Where does the Singleton Pattern apply [...]]]></description>
			<content:encoded><![CDATA[<p>The Singleton pattern is very similar to creator pattern. This is very widely used pattern. Now a question might raise in the mind that why do we need this pattern and when it should be applied. Let us take a situation where you want to build a chess game. Where does the Singleton Pattern apply here?<span id="more-283"></span></p>
<p>chessboard is one, which is a square-checkered board with 64 squares arranged in an eight-by-eight grid. At the start, each player controls sixteen pieces: one king, one queen, two rooks, two knights, two bishops, and eight pawns. The object of the game is to checkmate  the opponent&#8217;s king, whereby the king is under immediate attack (in &#8220;check&#8221;) and there is no way to remove or defend it from attack on the next move.</p>
<p>Here your chessboard is Singleton class as its state should never change. It means even at nth movement, the board should hold all the records of pieces moved and its state. Let us now drive back to technical discussion.</p>
<p>The Singleton pattern allows a class to only one instance and it does provide a global access to the instance. How do we go on to make a Singleton class.</p>
<blockquote><p>Sample code : SingletonClass.as</p>
<p>package com.sophiacom.as3.designpatterns.creational.singleton<br />
{<br />
public class SingletonClass<br />
{<br />
private static var _instance:SingletonClass;</p>
<p>public function SingletonClass(enforcer:SingletonEnforcer) {}</p>
<p>public static function getInstance():SingletonClass {<br />
if (SingletonClass._instance == null) {<br />
SingletonClass._instance = new SingletonClass(new SingletonEnforcer());<br />
}<br />
return SingletonClass._instance;<br />
}<br />
}<br />
}</p>
<p>class SingletonEnforcer {}</p>
</blockquote>
<p>First we are defining a private static property that holds the single instance of the class.<br />
Then we go with a public static function that provides access to the single instance created.<br />
A private constructor that restrict access in instantiating the class.</p>
<p>Oops ActionScript 3.0 does not have a private constructor! So, to do this, the solution is to force a parameter in the constructor while type isn&#8217;t accessible. Just with a class outer the package of the class but defined in the same file, because in ActionScript 3, only one class can be visible.</p>
]]></content:encoded>
			<wfw:commentRss>http://vinothbabu.com/2010/04/15/design-patterns-singleton-pattern/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Custom color in Flex bar Chart</title>
		<link>http://vinothbabu.com/2010/04/13/flex-bar-chart-with-different-colors/</link>
		<comments>http://vinothbabu.com/2010/04/13/flex-bar-chart-with-different-colors/#comments</comments>
		<pubDate>Tue, 13 Apr 2010 02:00:54 +0000</pubDate>
		<dc:creator>Sachin</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[Adobe Flex]]></category>
		<category><![CDATA[Flex Charts]]></category>
		<category><![CDATA[Bar Chart]]></category>
		<category><![CDATA[Color Component]]></category>
		<category><![CDATA[Custom component]]></category>

		<guid isPermaLink="false">http://vinothbabu.com/?p=276</guid>
		<description><![CDATA[Today one of my friend asked me on how to have a bar chart with different colors for each series. I thought of putting up this code online that someone can use it up or extend for more functionality too. Link to the Application [ View Source enabled ]]]></description>
			<content:encoded><![CDATA[<p>Today one of my friend asked me on how to have a bar chart with different colors for each series. I thought of putting up this code online that someone can use it up or extend for more functionality too.<span id="more-276"></span><a href="http://vinothbabu.com/Flex-Samples/FlexBarChart/FlexBarChart.html" target="_blank"></a></p>
<p><a href="http://vinothbabu.com/Flex-Samples/FlexBarChart/FlexBarChart.html" target="_blank">Link to the Application</a> [ View Source enabled ]</p>
]]></content:encoded>
			<wfw:commentRss>http://vinothbabu.com/2010/04/13/flex-bar-chart-with-different-colors/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Formatting the SelectedDate in DateChooser</title>
		<link>http://vinothbabu.com/2010/03/31/formatting-the-selecteddate-in-datechooser/</link>
		<comments>http://vinothbabu.com/2010/03/31/formatting-the-selecteddate-in-datechooser/#comments</comments>
		<pubDate>Wed, 31 Mar 2010 15:26:10 +0000</pubDate>
		<dc:creator>Sachin</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[Adobe Flex]]></category>

		<guid isPermaLink="false">http://vinothbabu.com/?p=252</guid>
		<description><![CDATA[I was suddenly struck up with this DateChooser as my the date object doesn&#8217;t recognize the dash as a valid separator. var date:Date = new Date("2009/12/30"); myDateChooser.selectedDate = date; The above one works, but if you take the below one it does not work. var date:Date = new Date(&#8220;2009-12-30&#8243;); myDateChooser.selectedDate = date; The solution for [...]]]></description>
			<content:encoded><![CDATA[<p>I was suddenly struck up with this DateChooser as my the date object doesn&#8217;t recognize the dash as a valid  separator.<span id="more-252"></span></p>
<blockquote>
<pre>var date:Date = new Date("2009/12/30");
myDateChooser.selectedDate = date;</pre>
</blockquote>
<p>The above one works, but if you take the below one it does not work.</p>
<blockquote><p>var date:Date = new Date(&#8220;2009-12-30&#8243;);<br />
myDateChooser.selectedDate = date;</p></blockquote>
<p>The solution for this is DateFormatter.</p>
<blockquote><p>var DateStr:String = dateFormatter.format(&#8220;2009-12-30&#8243;);<br />
DateChooser.selectedDate = new Date(DateStr);</p>
<p>&lt;mx:DateFormatter id=&#8221;dateFormatter&#8221; formatString=&#8221;MMM D, YYYY&#8221;/&gt;</p>
<p>&lt;mx:DateChooser showToday=&#8221;false&#8221; id=&#8221;DateChooser&#8221; x=&#8221;130&#8243; y=&#8221;55&#8243;  width=&#8221;159&#8243;&gt;&lt;/mx:DateChooser&gt;</p></blockquote>
<p>This will solve your problem. Have a nice day.</p>
]]></content:encoded>
			<wfw:commentRss>http://vinothbabu.com/2010/03/31/formatting-the-selecteddate-in-datechooser/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Introduction to MVP for Flex</title>
		<link>http://vinothbabu.com/2010/03/29/introduction-to-mvp-for-flex/</link>
		<comments>http://vinothbabu.com/2010/03/29/introduction-to-mvp-for-flex/#comments</comments>
		<pubDate>Mon, 29 Mar 2010 13:46:00 +0000</pubDate>
		<dc:creator>Sachin</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[Adobe Flex]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[featured]]></category>
		<category><![CDATA[MVP]]></category>

		<guid isPermaLink="false">http://vinothbabu.com/?p=231</guid>
		<description><![CDATA[I was today having a discussion with my amigos on the various design patterns and we were usually struck with mind blowing MVP pattern. I did a Bing on reseearch but could not eventually find a single article which explains the users very clear of this pattern. That&#8217;s the reason of this post. Many people [...]]]></description>
			<content:encoded><![CDATA[<p>I was today having a discussion with my amigos on the various design patterns and we were usually struck with mind blowing MVP pattern. I did a Bing on reseearch but could not eventually find a single article which explains the users very clear of this pattern. That&#8217;s the reason of this post.<span id="more-231"></span></p>
<p>Many people might initially get confused over the MVC [ Model, view and Controller ] and the MVP [ Model, View and Presenter ] pattern. There is a slight variation, but this pattern reduces the coupling.</p>
<p><strong>Model View and Control Pattern</strong></p>
<p><img class="aligncenter size-full wp-image-232" title="mvc" src="http://vinothbabu.com/wp-content/uploads/2010/03/mvc.png" alt="mvc" width="323" height="310" /><br />
In this pattern, view dispatches the events to the controller which in turn interacts with business logic of the application and the data binding techniques which updates the view automatically when ever there is a change in our model.</p>
<p><strong>Model View and Presenter Pattern</strong></p>
<p><img class="aligncenter size-full wp-image-233" title="MVP_Model4" src="http://vinothbabu.com/wp-content/uploads/2010/03/MVP_Model4.jpg" alt="MVP_Model4" width="400" height="228" /><br />
In this pattern, controller plays a major role in handling the user input and also an interaction with business logic also responsible of the update of the view. In this way there remains now link of view with Model and it totally relies on controller for all the presentation logic. As a result, there is no dependencies in either direction between the view and the model.</p>
<p>Let us now go through an example in RajiniKanth style. The below defines the the structure of our application.</p>
<p><img class="alignright size-medium wp-image-242" title="Structure" src="http://vinothbabu.com/wp-content/uploads/2010/03/Structure-300x230.jpg" alt="Structure" width="300" height="230" />The presenter folder contains the AdditionPresenter which acts as a controller handling the input and also does the business logic.</p>
<p>The view is area where our form is displayed with an <strong>Interface </strong>IAddView (implemented by the AdditionForm.mxml file) define  the methods we need in order to update the view and get the the numbers from the view to complete our <strong>Addition </strong>operation.</p>
<p>The <strong>MVPAddition.mxml</strong> is the main file, it only defines the custom component view.</p>
<blockquote><p>&lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;utf-8&#8243;?&gt;<br />
&lt;mx:Application xmlns:mx=&#8221;http://www.adobe.com/2006/mxml&#8221; layout=&#8221;absolute&#8221; xmlns:view=&#8221;com.vinoth.MVP.view.*&#8221;&gt;<br />
&lt;view:AdditionForm/&gt;<br />
&lt;/mx:Application&gt;</p></blockquote>
<p>The Interfact IAddView defines the methods we need in order to update the view and get the numbers from the view. It is implemented by the AdditionForm.mxml</p>
<blockquote><p>package com.vinoth.MVP.view<br />
{<br />
public interface IAddView<br />
{</p>
<p>function set Number3(value:String):void;</p>
<p>function get Number1():Number;<br />
function get Number2():Number;</p>
<p>}<br />
}</p></blockquote>
<p>The AddView custom component contains the MXML tag needed to define the  UI and it calls the addData() method in the Presenter.</p>
<blockquote><p>&lt;mx:Label text=&#8221;Addition of Two Numbers&#8221; width=&#8221;100%&#8221; fontWeight=&#8221;bold&#8221;/&gt;</p>
<p>&lt;mx:Form width=&#8221;100%&#8221; height=&#8221;50%&#8221;&gt;<br />
&lt;mx:FormItem label=&#8221;Number One&#8221; width=&#8221;100%&#8221;&gt;<br />
&lt;mx:TextInput id=&#8221;_Number1&#8243; editable=&#8221;true&#8221; width=&#8221;100%&#8221; restrict=&#8221;0-9&#8243;/&gt;<br />
&lt;/mx:FormItem&gt;<br />
&lt;mx:FormItem label=&#8221;Number Two&#8221; width=&#8221;100%&#8221;&gt;<br />
&lt;mx:TextInput id=&#8221;_Number2&#8243; editable=&#8221;true&#8221; width=&#8221;100%&#8221; restrict=&#8221;0-9&#8243;/&gt;<br />
&lt;/mx:FormItem&gt;<br />
&lt;/mx:Form&gt;<br />
&lt;mx:HBox width=&#8221;100%&#8221;&gt;<br />
&lt;mx:Label text=&#8221;Result&#8221; /&gt;<br />
&lt;mx:Label width=&#8221;100%&#8221; id=&#8221;_Number3&#8243; /&gt;<br />
&lt;mx:Button label=&#8221;Add&#8221; click=&#8221;_presenter.addData()&#8221; /&gt;<br />
&lt;/mx:HBox&gt;</p></blockquote>
<p>It also contains the methods defined in the IAddView and a private  method that is registered as a listener for the creationComplete event  that initialize the presenter.</p>
<blockquote><p>import com.vinoth.MVP.presenter.AdditonPresenter;</p>
<p>private var _presenter:AdditonPresenter;</p>
<p>private function init():void{<br />
_presenter = new AdditonPresenter(this);<br />
}</p>
<p>public function get Number1():Number {<br />
return Number(_Number1.text);<br />
}</p>
<p>public function get Number2():Number{<br />
return Number(_Number2.text);<br />
}</p>
<p>public function set Number3(value:String):void{<br />
_Number3.text = value;<br />
}</p></blockquote>
<p>The presenter is the one which handles the business logic and updates the view through the interface without accessing the View Form.The presenter stores a private member of the view and it updates accordingly.</p>
<blockquote><p>public var _view:IAddView</p></blockquote>
<p>The Patter is simple but yet very powerful because of its least dependency and the presenter relies only on the interface and not on the UI.</p>
<p><a href="http://vinothbabu.com/Flex-Samples/MVP-Addition.rar" target="_blank">Application Souce</a></p>
]]></content:encoded>
			<wfw:commentRss>http://vinothbabu.com/2010/03/29/introduction-to-mvp-for-flex/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

