<?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>Sean Monahan &#187; Flex</title>
	<atom:link href="http://seanmonahan.org/tag/flex/feed/" rel="self" type="application/rss+xml" />
	<link>http://seanmonahan.org</link>
	<description></description>
	<lastBuildDate>Wed, 07 Dec 2011 04:20:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Preventing Users from Accidently Navigating Away From Your Flex App</title>
		<link>http://seanmonahan.org/2009/03/19/preventing-users-from-accidently-navigating-away-from-your-flex-app/</link>
		<comments>http://seanmonahan.org/2009/03/19/preventing-users-from-accidently-navigating-away-from-your-flex-app/#comments</comments>
		<pubDate>Fri, 20 Mar 2009 01:55:08 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[ExternalInterface]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[onbeforeunload]]></category>

		<guid isPermaLink="false">http://seanmonahan.org/?p=368</guid>
		<description><![CDATA[One of the downsides to creating Rich Internet Applicationss is that they defy the standard methods for interacting with websites &#8212; the refresh, back and forward buttons have no real place in an Flex application.  As a developer you probably understand that clicking refresh or back is a no-no but you users aren&#8217;t likely to [...]]]></description>
			<content:encoded><![CDATA[<p>One of the downsides to creating Rich Internet Applicationss is that they defy the standard methods for interacting with websites &#8212; the refresh, back and forward buttons have no real place in an Flex application.  As a developer you probably understand that clicking refresh or back is a no-no but you users aren&#8217;t likely to be as savvy.  This article will describe a method you can employ to prompt users with a confirmation dialog when they attempt to navigate away from you Flex app.  <span id="more-368"></span> For this article I assume you are not using <a title="About Deep Linking" href="http://livedocs.adobe.com/flex/3/html/help.html?content=deep_linking_2.html">deep linking</a> in your Flex application as I&#8217;m not currently using it with any of my projects so I cannot speak to using it in this scenario.  I&#8217;ve also only tested this with the Windows versions of Internet Explorer 7, Firefox 3.x, Chrome 1 and Safari 4 beta so if you&#8217;re using anything else your mileage may vary (will test with Mac browsers once I fix my Flash Player install).</p>
<p>With the disclaimer out of the way we&#8217;ll get down to business.  Our goal here is to produce a close confirmation popup when a user accidentally attempts to navigate away from our Flex app.  Examples of &#8220;navigating away from our Flex app&#8221; include: clicking the refresh, back or forward button, clicking a bookmark or favorite, or typing a new url in the address bar.  Additionally we&#8217;ll disable the prompt when the user intentionally navigates away from our Flex app; when logging out for example.  To accomplish this we&#8217;ll make use of the Javascript onbeforeunload event and Flex/Flash&#8217;s ExternalInterface package.</p>
<p>At this point I feel it&#8217;s important to note that doing what are about to implement is generally considered bad form for websites.  If I, as a user, want to leave a page I&#8217;d damn well better be able to with no hassle.  A Flex app is usually not a typical website however, rather it is an application running inside a web browser and when you close an application like Word with unsaved data you always get a confirmation dialog.</p>
<p>It&#8217;s my opinion that displaying a close confirmation for a Flex app is good form for two reasons.  The first is that RIAs are meant to be similar to desktop applications and any half decent desktop application is going to have you confirm closing the application if you have unsaved information.  The second is that, in being more like a desktop app, an RIA subverts the way users typically interact with web sites and web apps.  For most Flex apps the back and refresh buttons can cause havoc so throwing up a confirmation dialog when these buttons are clicked can enhance your application.</p>
<p>So we&#8217;ve made up our minds that we&#8217;re adding the close confirmation popup now lets get to it!</p>
<h3>The Javascript</h3>
<p>The first thing we need is some Javascript that will allow us to work with the onbeforeunload event.  Specifically we&#8217;ll need:</p>
<ul>
<li>A flag to indicate whether or not the popup should be displayed</li>
<li>A variable to hold the text we want displayed in the popup</li>
<li>A function to set the flag</li>
<li>A function to set the prompt text</li>
</ul>
<p>Here&#8217;s my Javascript object, FlexOnBeforeUnload that encapsulates all of this:</p>
<pre>
<pre class="brush: jscript; title: ; notranslate">

var FlexOnBeforeUnload = {

   /**
    * Flag indicating the user should be prompted each
    * time they attempt to navigate away from or refresh
    * the Admin HTML page.
    */
   prompt: true,

   promptText: &quot;Refreshing or leaving this page will cause you &quot;
             + &quot;to lose any unsaved data.\n&quot;
             + &quot;Are you sure you want to leave?&quot;,

   /**
    * Sets the value of &quot;prompt&quot;
    */
   setPrompt: function (value) {
      if (value) {
         FlexOnBeforeUnload.prompt = true;
      } else if (!value) {
         FlexOnBeforeUnload.prompt = false;
      }
   }

   /**
    * Sets the value of &quot;promptText&quot;
    */
   setPromptText: function (value) {
      FlexOnBeforeUnload.promptText = value;
   }
};
</pre>
</pre>
<p>Using Javascript we can access prompt and priompText directly however the ExternalInterface package in Flex only allows you to make calls to Javascript functions &#8212; you cannot use it to access variables directly.  Besides accessor functions are good practice.</p>
<p>SetPrompt will take a boolean value and assign it to prompt.  Setting prompt to true will cause the close confirmation to be displayed when a user attempts to navigate away from your Flex app.  Setting prompt to false will allow users to navigate away without any nagging.</p>
<p>SetPromptText will allow you to change the text displayed in your confirmation dialog.  Unfortunately you have very little control over the text displayed in the confirmation dialog.  The dialog will always display &#8220;Are you sure you want to navigate away from this page?&#8221; and &#8220;Press OK to continue, or Cancel to stay on the current page.&#8221; along with the OK and Cancel buttons.  The only thing you can customize is the text that appears between &#8220;Are you sure&#8230;&#8221; and &#8220;Press OK&#8230;&#8221;.</p>

<a href="http://seanmonahan.org/wp-content/gallery/flex-close-confirmation/close-confirm.png" title="" class="thickbox" rel="singlepic46" >
	<img class="ngg-singlepic" src="http://seanmonahan.org/wp-content/gallery/cache/46__x_close-confirm.png" alt="close-confirm.png" title="close-confirm.png" />
</a>

<p>In the above pic you can see that &#8220;Refreshing or leaving this page will cause you to lose any unsaved data.  Are you sure you want to leave?&#8221;, the default promptText, is displayed in the close dialog.  Now that we have our Javascript object we&#8217;ll save it in a file called flex-onbeforeunload.js.</p>
<p>Now open up your HTML wrapper (the HTML page that loads your Flex app) and include flex-onbeforeunload.js in the head:</p>
<pre class="brush: xml; title: ; notranslate">

&lt;!-- Assumes the JS file is located in the same folder as the wrapper. --&gt;
&lt;script src=&quot;flex-onbeforeunload.js&quot; language=&quot;javascript&quot;&gt;
&lt;/script&gt;
</pre>
<p>After that you are ready to setup the onbeforeunload event handler.  In a script block beneath the include we just added add the following:</p>
<pre>
<pre class="brush: jscript; title: ; notranslate">

window.onbeforeunload = function(e) {
   if (FlexOnBeforeUnload.prompt) {
      return FlexOnBeforeUnload.promptText;
   }
}
</pre>
</pre>
<p>This code will be executed prior to the page being unloaded (onbeforeunload even).  If FlexOnBeforeUnload.prompt is true the close confirmation will be displayed with our promptText.  If FlexOnBeforeUnload.prompt is false the function returns nothing and no confirmation will be shown.</p>
<h3>The Actionscript</h3>
<p>Since the default for FlexOnBeforeUnload.prompt is true the close confirmation dialog will always be shown unless we take some action to prevent it.  Enter <a href="http://livedocs.adobe.com/flex/3/langref/flash/external/ExternalInterface.html">ExternalInterface</a>, a class used to facilitate communication between Actionscript and Flash Player and the HTML wrapper.  In other words, ExternalInterface allows Actionscript to talk with Javascript and vice versa, though in our situation we&#8217;ll only being going from Actionscript to Javascript.</p>
<p>For our purposes we are interested in two parts of ExternalInterface, the call method and the available property.  ExternalInterface.available &#8220;[i]ndicates whether this player is in a container that offers an external interface.&#8221;  In English, if ExternalInterface.available is true we can use ExternalInterface.  ExternalInterface.call allows us to call Javascript functions in the wrapper.  To use ExternalInterface.call you pass in the name of the Javascript function as a string and then any parameters the Javascript function takes as a comma-separated list.</p>
<p>Earlier I gave the example of logout as a situation where you would probably not want to show a close confirmation popup.  Using ExternalInterface you can disable the confirmation for situations such as this.  To disable the confirmation popup add the following to your logout code in Flex Builder:</p>
<pre>
<pre class="brush: jscript; title: ; notranslate">

// Make sure ExternalInterface is available
if (ExternalInterface.available)
{
   // Set prompt to false, disabling the close confirmation
   ExternalInterface.call(&quot;FlexOnBeforeUnload.setPrompt&quot;, false);
}
else
{
   // Optionally handle the situation where ExternalInterface
   // is not available.
}
</pre>
</pre>
<p>This will set the value of FlexOnBeforeUnload.prompt to false, disabling the close confirmation.  Similarly you can change the prompt text:</p>
<pre>
<pre class="brush: jscript; title: ; notranslate">

// Make sure ExternalInterface is available
if (ExternalInterface.available)
{
   // Set prompt to false, disabling the close confirmation
   ExternalInterface.call(&quot;FlexOnBeforeUnload.setPromptText&quot;,
                                &quot;Different prompt text.&quot;);
}
else
{
   // Optionally handle the situation where ExternalInterface
   // is not available.
}
</pre>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://seanmonahan.org/2009/03/19/preventing-users-from-accidently-navigating-away-from-your-flex-app/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Beer Near Here Desktop Part 3: Setting up the Project in Flex Builder</title>
		<link>http://seanmonahan.org/2009/03/03/beer-near-here-desktop-part-3-setting-up-the-project-in-flex-builder/</link>
		<comments>http://seanmonahan.org/2009/03/03/beer-near-here-desktop-part-3-setting-up-the-project-in-flex-builder/#comments</comments>
		<pubDate>Wed, 04 Mar 2009 02:08:00 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[AIR]]></category>
		<category><![CDATA[Beer Near Here]]></category>
		<category><![CDATA[Flex]]></category>

		<guid isPermaLink="false">http://seanmonahan.org/?p=317</guid>
		<description><![CDATA[After laying down the plan for this project I spent quite a bit of time with the setup in Flex Builder.  If you&#8217;ve read my post, Flex Builder Optimization and You, you&#8217;ll know that I&#8217;ve done a bit of reading on setting up Flex projects for efficient work flows, particularly compile times, and I&#8217;ve come [...]]]></description>
			<content:encoded><![CDATA[<p>After laying down the plan for this project I spent quite a bit of time with the setup in Flex Builder.  If you&#8217;ve read my post, <a href="/2009/01/31/flex-builder-optimization-and-you/">Flex Builder Optimization and You</a>, you&#8217;ll know that I&#8217;ve done a bit of reading on setting up Flex projects for efficient work flows, particularly compile times, and I&#8217;ve come to the conclusion that it&#8217;s super, extra important to setup Flex projects well.  Aside from the usual upshots of enhancing efficiency and the like, it&#8217;s been my experience that a well organized Flex project will load and compile significantly faster that one that&#8217;s been poorly organized.  Trust me, I learned the hard way.</p>
<p><span id="more-317"></span></p>
<p>Beer Near Here Desktop makes use of three libraries, all of which are freely available:</p>
<ol>
<li><a title="as3corelib" href="http://code.google.com/p/as3corelib/">as3corelib</a> for hashing methods</li>
<li><a title="Alex Uhlmann's Flex Distortion Effects" href="http://weblogs.macromedia.com/auhlmann/archives/DistortionEffects.zip">Alex Uhlmann&#8217;s Distortion Effects</a> for distortion effects</li>
<li>My own Flex Library, available <a title="Sean Monahan's Flex Library" href="http://seanmonahan.org/code/flex/library/SeanMonahanLibrary-latest.zip">here</a>.</li>
</ol>
<p>Now there are a number of ways to link these libraries into the BNHD project, but it all pretty much boils down to two methods.  Either I link the project into BNHD&#8217;s build path or I copy the SWC that the library produces into BNHD&#8217;s build path.  My experience has been that neither of these methods is really better than the other and using one over the other largely depends on the nature of the library itself.</p>
<p>Often when people distribute their Flex code it is in the form of a SWC, so there will be plenty of cases where you&#8217;ll simply have to use this method.  In the case of as3corelib, I downloaded the source so that I could disable the packages I didn&#8217;t need, but once I compiled a SWC for the project I included that into BNHD&#8217;s build path rather than the as3corelib project itself.  As3corelib isn&#8217;t going to change beyond my initial modifications and once the SWC is exported I just copy and pasted into into BNHD&#8217;s libs folder.  This allows the BNHD project to be more self-contained as it now no longer relies on the separate as3corelib project.  I then repeated all of this for the distortion effects library.</p>
<p>My own personal Flex library is another matter.  It&#8217;s very immature with few components and effects so as I work on projects like Beer Near Here Desktop I typically pull out a few of the more useful components and put them into my library.  All this is to say, my library changes fairly frequently.  In this case I&#8217;ve found simply adding the project rather than the SWC it produces to be a faster solution.  The downside is that the BNHD project now relies on an external project, so both will have to be present and accounted for in Flex Builder in order to work with BNHD.  Doing this also alleviates the need to continually rebuild my library project during development.</p>
<p>To summarize so far, external libraries, like those listed above should be placed into their own Flex Library Projects.  Any library project that will remain static throughout development should be compiled into a SWC and copied into the main project&#8217;s lib folder.  Any library project that will change frequently should be linked into the main project&#8217;s build path.  Doing all this will prevent you from spending many minutes (or hours) watching your Flex projects compile all while keeping things nicely organized.  I believe this is called a &#8220;win-win&#8221;.</p>
<p>The actual Beer Near Here Desktop project organization is very simple.  In the project&#8217;s source folder I have three files: main.mxml, main-app.xml and style.css.  The first two are the files that define any AIR app and style.css is the master stylesheet for the entire application.  In the source folder I also have an images folder that contains all of the apps images and net.beernearhere folder that contains all the classes and components and whatnots specific to Beer Near Here Desktop.</p>
<p><a href="http://seanmonahan.org/wp-content/uploads/2009/03/bnh-flex-project-layout.png"><img class="alignnone size-medium wp-image-330" title="bnh-flex-project-layout" src="http://seanmonahan.org/wp-content/uploads/2009/03/bnh-flex-project-layout-300x290.png" alt="bnh-flex-project-layout" width="300" height="290" /></a></p>
<p>I really cannot stress enough how much more efficient Flex Builder became once I started shunting things off into libraries and strongly recommend you do the same if you&#8217;re not already.  As for how those projects should be organized, I leave that up to you.  I showed how the BNHD project is setup because I&#8217;ll reference it later, but it&#8217;s pretty standard and really to each his (or her) own.</p>
<p><a title="Read Part 4." href="/2009/03/10/beer-near-here-desktop-part-4-using-custom-chrome/">Read Part 4</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://seanmonahan.org/2009/03/03/beer-near-here-desktop-part-3-setting-up-the-project-in-flex-builder/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Flex Builder Optimization and You</title>
		<link>http://seanmonahan.org/2009/01/31/flex-builder-optimization-and-you/</link>
		<comments>http://seanmonahan.org/2009/01/31/flex-builder-optimization-and-you/#comments</comments>
		<pubDate>Sun, 01 Feb 2009 04:19:59 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[Compile Time]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Flex Builder]]></category>
		<category><![CDATA[Flex Libraries]]></category>
		<category><![CDATA[Flex Library]]></category>
		<category><![CDATA[Flex Modules]]></category>
		<category><![CDATA[Modules]]></category>
		<category><![CDATA[Runtime Shared Library]]></category>

		<guid isPermaLink="false">http://seanmonahan.org/?p=232</guid>
		<description><![CDATA[So you&#8217;ve been working on a Flex project for nearly a year and you have a few dozen modules and even more Actionscript classes and suddenly you realize that the project is taking an eternity to compile.  Three minutes 26 seconds in my case.  This snuck up on me too as compile times gradually became [...]]]></description>
			<content:encoded><![CDATA[<p>So you&#8217;ve been working on a Flex project for nearly a year and you have a few dozen modules and even more Actionscript classes and suddenly you realize that the project is taking an eternity to compile.  Three minutes 26 seconds in my case.  This snuck up on me too as compile times gradually became longer and longer.  It all came to a head yesterday as I was doing some debugging but couldn&#8217;t really accomplish anything as I had to wait three and a half minutes between compiles.  After quite a bit of searching I&#8217;m come up with a two-pronged solution.</p>
<p><span id="more-232"></span></p>
<p>The two prongs are rather simple: move your Actionscript classes and MXML components to a library project, and set Flex Builder to only compile the module or modules you are currently working with rather than all modules for the project.  I know it sounds really simple but it took me about five hours to dig around Adobe&#8217;s documentation and piece everything together.  The payoff has definitely been worth is though with compile times down to about twenty seconds, or about 10% of the previous time.  In the hope of saving others a few gray hairs I&#8217;ve put together a handy little guide.</p>
<p>I started by creating a separate library project for all my classes and reusable components.  The official Adobe docs on this are <a title="Flex 3: Setting up Library Projects" href="http://livedocs.adobe.com/flex/3/html/help.html?content=projects_7.html">here</a> (for Flex 3).  There are a lot of benefits to doing this besides decreased compile times.  Having all your Actionscript classes and MXML components in a library project allows you to easily reuse them in multiple projects.  Taking this a step further you can also add other libraries to a library project.  So rather than link multiple libraries into your Flex project you could set up a library to do this and link that single library into your project.  In the end library projects are quite useful for many things.  Use them.</p>
<p>But how, you ask?  The process is pretty much identical to creating a standard Flex project, just select File &gt; New &gt; Flex Library Project and follow along through the wizard.  Once you have your project setup just add your code and compile it into a .swc file (if you have Build Automatically checked Flex Builder will do this for you automagically).  You then link this swc into your main project&#8217;s build path. To add the swc to your project&#8217;s build path:</p>
<ol>
<li>Right-click your project in the Flex Navigator, choose Properties.</li>
<li>In the Properties window select Flex Build Path.</li>
<li>Click the Library Path tab.</li>
<li>Click the Add SWC&#8230; button.</li>
<li>Browse and find your library&#8217;s swc.  By default this is in the bin folder of your library project.</li>
</ol>
<p>You can now import your classes and components just as if they were physically in the project.  Using a seprate library project decreases compile time because now your classes and components are only being recompiled when you make changes to the library project.</p>
<p>One thing to consider when adding your library to the Build Path of your project is the library&#8217;s Link Type (you can see this by clicking the rollout tab next to you library in the list of build path libraries).  There are three options; I went with Runtime Shared Library (RSL).  As far as I could tell there was no substantial difference in compile times with the three link types, but RSL has the advantage of seriously reducing module file size.  I went from having an average module weigh in around 230KB to around 70KB.  The catch is that the library is exported as a separate swf that needs to be uploaded along with your application and module swfs.  The library swf is then loaded in by the application at start up, making the initial application start up a tad slower but drastically increasing load speeds for modules.</p>
<p>Employing runtime shared libraries offered a nice performance boost both for my application&#8217;s compile times and load times but selectively compiling my app&#8217;s modules is what really decimated compile times.  To select which modules to compile:</p>
<ol>
<li>Right-click your project in the Flex Navigator, choose Properties.</li>
<li>In the Properties window select Flex Modules.</li>
<li>You should see a list of all the modules in your application.  Select the ones you aren&#8217;t working with and click the Remove button.</li>
</ol>
<p>When you need to add a module to compile click the Add&#8230; button in the Flex Modules window and browse for it.  Any new modules you add should automatically be added to the list to compile.  There are two downsides to this.  The first is that anytime you make a change to the list of modules Flex has to rebuild your project&#8217;s workspace which can take some time.  The second is that it&#8217;s a complete pain in the ass to add modules back into the compile list since you can only add one at a time and you have to browse through a few windows to do so.  In practice though, you&#8217;re not likely to be constantly adding and removing all your modules so this second point is probably moot.</p>
<p>And that&#8217;s it.  As I said at the top I saw a 90% decrease in compile times just by doing these two things.  I&#8217;ve also been far more productive as a result since I don&#8217;t have an interminable wait while debugging.</p>
]]></content:encoded>
			<wfw:commentRss>http://seanmonahan.org/2009/01/31/flex-builder-optimization-and-you/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Flex List to List Component</title>
		<link>http://seanmonahan.org/2009/01/09/a-flex-list-to-list-component/</link>
		<comments>http://seanmonahan.org/2009/01/09/a-flex-list-to-list-component/#comments</comments>
		<pubDate>Fri, 09 Jan 2009 23:43:41 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Adobe Flex]]></category>
		<category><![CDATA[Component]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[List to List]]></category>

		<guid isPermaLink="false">http://seanmonahan.org/?p=226</guid>
		<description><![CDATA[On a recent Flex project I needed a component that would allow me to move selected items in one list to another list.  A Google search for &#8220;flex list to list component&#8221; turned up nothing, nor did &#8220;flex enhanced list component&#8221;.  Undaunted I decided to roll my own which I am now sharing with the [...]]]></description>
			<content:encoded><![CDATA[<p>On a recent Flex project I needed a component that would allow me to move selected items in one list to another list.  A Google search for &#8220;flex list to list component&#8221; turned up nothing, nor did &#8220;flex enhanced list component&#8221;.  Undaunted I decided to roll my own which I am now sharing with the world.</p>
<p><span id="more-226"></span>The component has a robust feature set providing multiple move options and customization for nearly everything.  I&#8217;ve created a project page for this component <a href="/projects/flex-list-to-list-component/">here</a>.  There you can find a demo and download the source for use in your own projects.</p>
]]></content:encoded>
			<wfw:commentRss>http://seanmonahan.org/2009/01/09/a-flex-list-to-list-component/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

