<?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>Tom Cornilliac &#187; PureMVC</title>
	<atom:link href="http://www.tomcornilliac.com/tag/puremvc/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tomcornilliac.com</link>
	<description>Flex, AIR and Rich Internet Goodness</description>
	<lastBuildDate>Wed, 17 Jun 2009 15:40:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Handling deferred view component creation within the PureMVC framework</title>
		<link>http://www.tomcornilliac.com/2008/01/handling-deferred-view-component-creation-within-the-puremvc-framework/</link>
		<comments>http://www.tomcornilliac.com/2008/01/handling-deferred-view-component-creation-within-the-puremvc-framework/#comments</comments>
		<pubDate>Fri, 25 Jan 2008 19:24:15 +0000</pubDate>
		<dc:creator>Tom Cornilliac</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[PureMVC]]></category>

		<guid isPermaLink="false">http://www.cornilliac.com/blog/?p=44</guid>
		<description><![CDATA[For my last AIR project and my current Flex project I&#8217;ve been using the PureMVC ActionScript framework. It&#8217;s a solid framework and on the whole I&#8217;m enjoying working with it. When using PureMVC for Flex and AIR development one of the questions I see consistently is how to create mediators for deferred components. In other [...]]]></description>
			<content:encoded><![CDATA[<p> For my last AIR project and my current Flex project I&#8217;ve been using the <a href="http://www.puremvc.org">PureMVC</a> ActionScript framework. It&#8217;s a solid framework and on the whole I&#8217;m enjoying working with it. When using PureMVC for Flex and AIR development one of the questions I see consistently is how to create mediators for deferred components. In other words, if the view of your app uses a view stack how do you handle creating mediators on the children of the viewstack that are not created yet?  <span id="more-44"></span></p>
<p>PureMVC uses mediators for all communication from the model and controller to the view. Out of the box PureMVC normally creates the view mediators after creationComplete has fired and the model has been initialized. The reason for this is that mediators require a reference to a view component to work. In order to construct a mediator you first need an object or view component to pass to the constructor.</p>
<p>There are basically three different ways to handle this, two of which I don&#8217;t recommend using.</p>
<p><strong>1. <u>Use dummy objects to initialize mediators</u></strong><br />
<strong> </strong>If you know that your view component is not on the stage yet you could still create it&#8217;s mediator by passing a POASO (plain old actionscript object) to it&#8217;s constructor. I have two problems with this solution; One you&#8217;re unnecessarily using memory and two you have not treated the whole problem, you still need some type of event schema to reinitialize the mediator once the component has been created. <strong>I don&#8217;t recommend using this solution.</strong></p>
<p><strong>2. <u>Use creationPolicy=&#8221;all&#8221; to avoid deferred views</u><br />
</strong>By default components like ViewStack and TabNavigator on create their top level children at runtime. The remainder are created as the user navigates to them. You can use the creationPolicy=&#8221;all&#8221; attribute to override this behavior. This causes the application to create all the view components at runtime. <strong>This is a terrible idea and I cannot recommend it. </strong>What you are in effect saying to your user is &#8220;I don&#8217;t care about your memory or performance because I&#8217;m to lazy to code a decent solution&#8221; I know this is a bit dramatic but I hate when people use this attribute.</p>
<p><strong>3. <u>Use a custom Event subclass</u><br />
</strong>The idea here is to create an event subclass that the application mediator or another mediator can listen for and use to create new mediators. This event class has an extra property which holds a reference to the newly created component, other mediators can use this reference to construct a mediator for the component. This approach not only keeps your memory footprint smaller but it allows you to easily create and destroy mediators. <strong>Here&#8217;s an example of an Event subclass:</strong></p>
<p>You create your own constant names based on the event type you want your mediator to listen for.</p>
<p>[as]<br />
package com.tomcornilliac.myproject.events<br />
{<br />
	import flash.events.Event;</p>
<p>   	public class ComponentCreationEvent extends Event<br />
   	{<br />
      	// Publically accessible properties<br />
      	public var component:Object;</p>
<p>		//Constants used for routing withing PureMVC mediators<br />
		public static const SOME_EVENT:String = &#8220;gatewayCreated&#8221;;<br />
		public static const SOME_OTHER_EVENT:String = &#8220;gatewayStackCreated&#8221;;</p>
<p>		public function ComponentCreationEvent(type:String, component:Object, bubbles:Boolean=false, cancelable:Boolean=false)<br />
		{<br />
			super(type, bubbles, cancelable);<br />
			//Probably should have a getter/setter for this property<br />
			this.component = component;<br />
		}</p>
<p>		//You must override the clone method<br />
		override public function clone():Event<br />
		{<br />
			return new ComponentCreationEvent(type, component, bubbles, cancelable);<br />
		}<br />
	}<br />
}<br />
[/as]</p>
<p>An instance of this event is dispatched from your view component with a reference to its self. <em>Note that &#8220;bubbles&#8221; is set to true, this is required for the event to make it up the chain to whatever mediator is listening.</em><br />
[xml]<br />
<?xml version="1.0" encoding="utf-8"?></p>
<p><mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml"<br />
	width="100%" height="100%" creationComplete="init(event)"></p>
<p>	<mx:Metadata><br />
		[Event(name="gatewayCreated", type="org.deschutes.grizzly.events.ComponentCreationEvent")]<br />
	</mx:Metadata></p>
<p>	<mx:Script><br />
		<![CDATA[<br />
			import com.tomcornilliac.myproject.events.ComponentCreationEvent;</p>
<p>			private function init(e:Event):void<br />
			{<br />
				dispatchEvent(new ComponentCreationEvent(ComponentCreationEvent.GATEWAY_CREATED, this, true));<br />
			}<br />
		]]&gt;<br />
	</mx:Script></p>
<p></mx:Canvas><br />
[/xml]</p>
<p>The listening mediator (in this case the ApplictionMediator) will respond to the event and create and register a new mediator for the newly created view component. Now it&#8217;s important to note that while I&#8217;m using the ApplicationMediator in this example, you can use any mediator you want, so long as the event will bubble-up to it.</p>
<p>[as]<br />
package com.tomcornilliac.myproject.view<br />
{<br />
	import com.tomcornilliac.myproject.events.ComponentCreationEvent;<br />
	import com.tomcornilliac.myproject.view.components.SomeComponent;<br />
	import org.puremvc.interfaces.IMediator;<br />
	import org.puremvc.interfaces.INotification;<br />
	import org.puremvc.patterns.mediator.Mediator;</p>
<p>	public class ApplicationMediator extends Mediator implements IMediator<br />
	{<br />
		//Canonical name of the mediator<br />
		public static const NAME:String = &#8216;ApplicationMediator&#8217;;</p>
<p>		//Constructor<br />
		public function ApplicationMediator(viewComponent:Object)<br />
		{<br />
			super(viewComponent);</p>
<p>			/**<br />
			 * Setup event listeners for components created dynamically<br />
			 */<br />
			 app.addEventListener(ComponentCreationEvent.SOME_EVENT, createNewMediator);<br />
		}</p>
<p>		private function createNewMediator(e:ComponentCreationEvent):void<br />
		{<br />
			//Create the new Mediator and initialize it with the component from our event<br />
			var mediator:SomeComponentMediator = new SomeComponentMediator(e.component);<br />
			//Register the mediator with the Application Facade<br />
			facade.registerMediator(mediator)<br />
			//That&#8217;s it now your view component is hooked-up and ready to handle I/O<br />
		}</p>
<p>		&#8230;.other methods<br />
[/as]</p>
<p>This technique is not only good for component creation but you could just as easily create a custom ComponentDestructionEvent and use it to remove Mediators off the stack when no longer needed. </p>
<p>If you would like more information about the PureMVC framework I encourage you to <a href="http://www.puremvc.org">visit the PureMVC web site</a>. The documentation is excellent and there&#8217;s even a training course to help you on your way.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tomcornilliac.com/2008/01/handling-deferred-view-component-creation-within-the-puremvc-framework/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>
