<?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>lejnieks &#187; Best Practices</title>
	<atom:link href="http://www.lejnieks.com/tag/best-practices/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.lejnieks.com</link>
	<description></description>
	<lastBuildDate>Fri, 29 Jan 2010 19:22:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Talk to your view through your command</title>
		<link>http://www.lejnieks.com/2008/01/29/talk-to-your-view-through-your-command/</link>
		<comments>http://www.lejnieks.com/2008/01/29/talk-to-your-view-through-your-command/#comments</comments>
		<pubDate>Tue, 29 Jan 2008 17:30:43 +0000</pubDate>
		<dc:creator>klejnieks</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://lejnieks.com/?p=11</guid>
		<description><![CDATA[Admittedly, this is something I have grappled with for quite some time now, and apparently I&#8217;m not the only one. This is by no means a hot topic but for my own purposes, and as I venture deeper into the world of &#8220;Best Practices&#8221; and the pursuit of architectural sanity I felt that this was [...]]]></description>
			<content:encoded><![CDATA[<p>Admittedly, this is something I have grappled with for quite some time now, and apparently I&#8217;m not the only one.  This is by no means a hot topic but for my own purposes, and as I venture deeper into the world of &#8220;Best Practices&#8221; and the pursuit of architectural sanity I felt that this was worth a post.<span id="more-11"></span></p>
<p>I recently found the need for my command to effect a change in my view, specifically, I am loading a config file into my application, once that config has loaded I wanted to popup a login screen on the main application view. The way I have this working now I have my LoadConfigCommand request the xml config file through a delegate then respond back to the commands result handler. LoadConfigCommand&#8217;s result method needs to then popup a login window. This could easily be solved by using</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;">     <span style="color: #000000; font-weight: bold;">var</span> appReference:DisplayObject = DisplayObject<span style="color: #66cc66;">&#40;</span> Application.<span style="color: #006600;">application</span> <span style="color: #66cc66;">&#41;</span>;
     <span style="color: #000000; font-weight: bold;">var</span> popup:IFlexDisplayObject = PopUpManager.<span style="color: #006600;">createPopUp</span><span style="color: #66cc66;">&#40;</span> appReference, Login, <span style="color: #000000; font-weight: bold;">true</span> <span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>but I am not particularly partial to using Application.application references as it has gotten me into trouble a few times before. While this is a valid solution to this, it doesnt fit the needs or solve the problem of talking to my view through my command, at best this is only a bridge to my view, and as more complex problem arise in this realm this quickly becomes a brittle if not unmanageable solution.</p>
<p>So where does this leave me? Research. I looked around, for what others have been doing to solve this problem, and came across a few very interesting approaches. I came across <a href="http://jessewarden.com/2005/11/cairngorms-viewlocator-viewhelpers-explained-for-flash-developers.html" target="_blank">Jesse Warden&#8217;s</a> post on ViewHelpers in Cairngorm to see if I could access my views using the ViewHelpers ViewLocator. I went ahead and registered my views but when it came time to accessing the methods of my views it became apparent that I would need to cast my viewLocator with my View itself:  Projects( ViewLocator.getInstance().getViewHelper(&#8220;projectsHelper&#8221;) ).doLogin(); which to me seems contrary. To use this solution I will be going against to core principals of mvc and cairngorm by introducing a reference of my view into my command, and for that matter for this example I might as well just do: Projects.doLogin() as this is essentially the same solution to this problem. Next I read about <a href="http://weblogs.macromedia.com/auhlmann/archives/2007/02/creating_a_popu.cfm" target="_blank">Alex&#8217;s</a> post on creating a popup window in Cairngorm. Seems like this will work. I started reading more about it, and his usage of the Observer pattern to listent to changes in the model. Alex writes that he is setting a boolean value in his model from his command, and listening for this change through ObserveValue. Again this is a perfectly viable solution to this problem as well, however the more complex the problem becomes, and the more I need to  listen to changes in my model to effect changes in my view this will quickly become a cluttered and almost unidentifiable clutter of boolean values in my model. Besides my view may need to have multiple states which surpass the simple values of True and False.</p>
<p>So im still stumped at this point&#8230; I message <a href="http://eyefodder.com">Paul</a> over at AC to get his views on the matter and hopefully gain some knowledge on the best solution for this. My question to him was &#8220;how do I speak to a view from a command&#8221;.  His logic is, well&#8230; logical. He told me that in architectural decisions if it doesn&#8217;t feel right, it inst right. Think about what the popup represents, a view state change. State change being the optimal word here. Since this is a state change and state is maintained in the model its perfectly inline to have my view listen to my model for a change that is set by my command. This is similar to Alex&#8217;s recommendation however by not using boolean values and binding the view using mx:Binding instead of ObeserveValue I am able to listen to more profound change values such as var applicationState:String = &#8220;preloading&#8221; and then wrap this bound value to a switch case that my view will decide what to do with when a change has been made. The differences between the two paths, from my view, is that Alex&#8217;s solution forces the model to maintain distinct state values for each and every change so that in the event that your single view may have 5 or 6 or more changes then you would have 5 or 6 or more corresponding state values of type boolean in your model that your views would listen to individually whereas by having a single state variable I can write in as many state changes as I need and match them on the switch case in my view.</p>
<p>So lets think about this a bit more abstractly. lets remove the popup example as we have many ways of achieving a solution to this. Lets think about this example: I have an image viewer / editor application, my main application view has a collapsible toolbar that is defaulted to open. The user clicks on the preview button which goes through the Cairngorm daisy chain to get to my command. once at my command I want to hide a few things and show a few things, including hiding my toolbar which at this point is no longer needed the user is now previewing. From my command, using the solution above, I have set a variable in my model var viewMode:ViewModes = ViewModes.EDITOR;  which from my command I will set a from EDITOR to PLAYER so my model will now show viewMode:ViewModes = ViewModes.PLAYER. My main application view is bound to this variable using mx:Binding and is set to a setter in the same view. The setter goes through a quick switch case to determine what to do based on the value set and then reacts accordingly, in this instance it hides the toolbar.</p>
<p>My issue here was that I felt that there must be a way to communicate directly to the view on low levels requests, things that didnt seem logical to clutter the model with, maybe some variation of the ViewLocator might have worked in this instance to gain access to the view for such requests. Well Paul broke this down in a very logical way that to me seems like a good rule of thumb for this problem:</p>
<blockquote><p>If there is a material change in state<br />
If several views would need to respond<strong><br />
Then stick it in the model and bind currentState to the var</strong></p>
<p>If its just in 1 component (for example changing the text on a submit button while waiting for a response)<br />
If it seems just plain pernickity<strong><br />
Then have that logic in the view</strong></p></blockquote>
<blockquote><p>MOST OF ALL:<strong><br />
design and architect and develop in such a way that allows you to change your mind </strong></p></blockquote>
<p>So my take on this is that, when I need to talk to my view all the way from my command, I need to:</p>
<blockquote><p>First identify why I want to talk to my view in the first place</p>
<ul>
<li>Is this something the View can handle on its own</li>
<li>Does this demand to wait for something to happen from the command</li>
<li>Is this a state change</li>
<li>Will more then one view listen to this change</li>
</ul>
<p>Write my command&#8217;s result to set the change variable in the model<br />
Listen to this change through Binding in the various views that will be effected by the change</p></blockquote>
<p>And there you have it, through good clean code and a bit of organization my model will be filled with state as it well should be and my various and infinite views will listen to those states and react to the changes set by my commands.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lejnieks.com/2008/01/29/talk-to-your-view-through-your-command/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
