<?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>Vivisecting Media &#187; ActionScript</title>
	<atom:link href="http://blog.vivisectingmedia.com/category/ria/actionscript/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.vivisectingmedia.com</link>
	<description>Looking at the world of media: from music to RIA.</description>
	<lastBuildDate>Fri, 09 Oct 2009 15:29:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Announcing DevelopmentArcâ„¢ Core Release Version 0.5</title>
		<link>http://blog.vivisectingmedia.com/2009/02/announcing-developmentarc%e2%84%a2-core-release-version-05/</link>
		<comments>http://blog.vivisectingmedia.com/2009/02/announcing-developmentarc%e2%84%a2-core-release-version-05/#comments</comments>
		<pubDate>Wed, 11 Feb 2009 18:37:47 +0000</pubDate>
		<dc:creator>James Polanco</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flash Player]]></category>
		<category><![CDATA[Flex Development]]></category>
		<category><![CDATA[Rich Internet Applications]]></category>
		<category><![CDATA[web 2.0]]></category>
		<category><![CDATA[web integration]]></category>

		<guid isPermaLink="false">http://blog.vivisectingmedia.com/?p=169</guid>
		<description><![CDATA[The newest release of DevelopmentArc Core is now available on the Google Code Repository.  This release is the first release of the library under the new Core moniker.  Previously the library was released under the Vivisecting Media Code Library, which has been now been deprecated since Core is publicly available.  All updates [...]]]></description>
			<content:encoded><![CDATA[<p>The newest release of <a title="Read more about DevelopmentArc Core" href="http://www.developmentarc.com/site/software/" target="_self">DevelopmentArc Core</a> is now available on the <a title="Read the Wiki and download the library" href="http://code.google.com/p/developmentarc-core/" target="_blank">Google Code Repository</a>.  This release is the first release of the library under the new Core moniker.  Previously the library was released under the Vivisecting Media Code Library, which has been now been deprecated since Core is publicly available.  All updates and new features will be added to the Core system moving forward.</p>

<p>From the code depot you can download a compiled SWC or a ZIP file that contains all the source and ASDoc generated files.  We have posted the <a title="DevelopmentArc Core ASDoc Files" href="http://www.developmentarc.com/core_asdoc/" target="_blank">ADoc files on-line</a> so that you can easily access them if you do not download them.  Currently, we have 219 unit tests running and passing for the library, which are all available from the source tree if you wish to access them.</p>

<p>The current release of Core contains the following functionality.  We have marked new and updated features that have been changed from the Vivisecting Media Library to the new Core Library:
<ul>
    <li><strong>Task Management System</strong> <span style="color: #ff6600;">new!</span> &#8211; The Task system is a priority queuing system that enables an application to execute specific bundles of business and/or server logic in a specific order leveraging priority and the ability to override previously queued bundles. These bundles are encapsulated inside of Task objects, which can be then bundled Task Group objects. Tasks and task groups have the ability to take priority over other Tasks or Task Groups inside of the queue, as well as replace existing tasks in the queue based on a set of overrides an individual task can define.</li>
    <li><strong>Command Action Delegation System (CAD)</strong> <span style="color: #ff6600;">new!</span> &#8211; Provides a pre-built solution for implementing the Command Pattern within ActionScript/Flex/AIR applications. The command / action system allows for commands to be dispatched to the ActionDelegate which then executes any assigned functionality via an Action.</li>
    <li><strong>Selection Management System</strong><span style="color: #008000;"> updated!</span> &#8211; The Selection Management system enables any kind of object to become selectable and treated as part of a Selection group. In most cases this would be a UI Component that should behave similarly to a radio button group, where only one item can be selected at one time. This system also allows for multi-select and deselection of items that are members of a selection group.</li>
    <li><strong>Local Connection Management</strong> <span style="color: #008000;">updated!</span> &#8211; The Local Connection manager is a helper class to quickly establish and manage local connection instances within an application. The Local Connection manager also supports the ability to maintain strong class types when the data object is passed over the local connection instance.</li>
    <li><strong>Event Broker Utility</strong> &#8211; The Event Broker is a utility class that acts as a router for specified events. The Event Broker is a single point of contact that any object can broadcast to or subscribe to. This functionality is intended to be the focal point for implementing the mediator pattern within the application.</li>
    <li><strong>Instance Factory Utility</strong> <span style="color: #ff6600;">new!</span> &#8211; The Instance Factory allows for the ability to make sure there is only one instance of a specified class within the instance of the factory. This is useful for using in Locators that handle generating a new instance on the first request but then making sure the original instance of the requested item is returned on subsequent requests.</li>
    <li><strong>Singleton Factory Utility</strong> <span style="color: #ff6600;">new!</span> &#8211; Similar to the Instance Factory, the Singleton Factory verifies that there is only one instance of a requested item, but instead of being limited to the instance of the factory, the Singleton Factory verifies there is only one instance of the requested item in the entire application.</li>
    <li><strong>Adjustable Date Utility</strong> <span style="color: #ff6600;">new!</span> &#8211; The adjustable date wraps a Flash Date object and provides a series of helper methods to adjust time and dates quickly and easily. Example uses included the ability to adjust a date by a specific number of days forward or backwards, making sure that the number of days in the month and Leap Years are respected during the adjustment.</li>
    <li><strong>Queue Data Structures </strong><span style="color: #008000;">updated!</span> &#8211; The Queue system allows for items to be stored in either a LIFO or FIFO based queue order or in a priority queue order and then retrieved from the data structure depending on how the items are ordered.</li>
    <li><strong>Hash Table Data Structure</strong> &#8211; The Hash Table is key/value that allows any value or object to be used as a reference key to another value or object item. The Hash Table is similar to Flex&#8217;s ArrayCollection in how items can be referenced but unlike the collection, this is ActionScript independent, is not intended for data pagination and is focused on performance for larger data sets.</li>
    <li><strong>Browser Location Utility</strong> <span style="color: #ff6600;">new!</span> &#8211; Utility class used to query the current url in the application. The class provides static properties for each piece of the application url.</li>
</ul>
I am really proud of all the work DevelopmentArc has put into Core so far.  We are currently working on the version 0.9 release roadmap which includes multiple new systems, helpers and utilities.  <em>FYI</em> &#8211; the 0.5 release number is used to announce the step from the 0.4 release of Vivisecting Medias to the Core name.  The 0.9 release will contain all of the target features for the 1.0 release of Core.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.vivisectingmedia.com/2009/02/announcing-developmentarc%e2%84%a2-core-release-version-05/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adobe MAX 2008: The Future of Flex Builder (Part Two)</title>
		<link>http://blog.vivisectingmedia.com/2008/12/adobe-max-2008-the-future-of-flex-builder-part-two/</link>
		<comments>http://blog.vivisectingmedia.com/2008/12/adobe-max-2008-the-future-of-flex-builder-part-two/#comments</comments>
		<pubDate>Wed, 10 Dec 2008 18:45:53 +0000</pubDate>
		<dc:creator>James Polanco</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flex Development]]></category>
		<category><![CDATA[MAX 2008]]></category>
		<category><![CDATA[Rich Internet Applications]]></category>
		<category><![CDATA[web integration]]></category>
		<category><![CDATA[adobe max]]></category>
		<category><![CDATA[Flex Builder]]></category>
		<category><![CDATA[Gumbo]]></category>
		<category><![CDATA[max]]></category>

		<guid isPermaLink="false">http://blog.vivisectingmedia.com/2008/12/adobe-max-2008-the-future-of-flex-builder-part-two/</guid>
		<description><![CDATA[In my previous post, I introduced the &#8220;Future of Flex Builder&#8221; session I attended at Adobe MAX North America, how the Flex Builder team is approaching new feature development and looked at the two main Design Fidelity features they are adding to Flex Builder.

Adobe MAX 2008: The Future of Flex Builder (Part One)

In this post [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous post, I introduced the &#8220;Future of Flex Builder&#8221; session I attended at Adobe MAX North America, how the Flex Builder team is approaching new feature development and looked at the two main Design Fidelity features they are adding to Flex Builder.</p>

<p><a href="http://blog.vivisectingmedia.com/2008/12/adobe-max-2008-the-future-of-flex-builder-part-one/" title="Adobe MAX 2008: The Future of Flex Builder (Part One)">Adobe MAX 2008: The Future of Flex Builder (Part One)</a></p>

<p>In this post I will be delving deeper into the new/updated feature set that focuses on the aspect of the &#8220;Test &amp; Tune&#8221; workflow.  Test &amp; tune is the day-to-day development workflow that we are all used to: Navigating our projects, creating new code, documenting, re-factoring, debugging and testing.  A lot of these features may seem small but they provide a lot of power and can drastically improve development time and ease frustration that we experience during the development cycle.</p>

<p><span id="more-168"></span>
<strong>Package Explorer</strong></p>

<p>The first test &amp; tune feature I want to examine is the new Package Explorer.  Some of you may be thinking to yourself that Flex Builder already has a Package Explorer and wondering why this is a new feature.  Well, to be exact, Flex Builder 3 does not have a true Package Explorer, what it has is the &#8220;Flex Navigator&#8221; which is really just a folder browser.  When we use the Flex Navigator we often treat it like a package explorer since ActionScript packages are just folder hierarchies and this is what is represented within the Flex Navigator.  The new Package Explorer merges the functionality of the Flex Code Outline panel and the Flex Navigator into one view.</p>

<p><img src="http://blog.vivisectingmedia.com/wp-content/uploads/2008/12/package-explorer.png" alt="Package Explorer" /></p>

<p>Take a look at the screen cap, first you will notice that the MXML root file is now in a <em>(default package)</em> node within the explorer.  From there you can then expand the MXML file data to look at the Classes contained and the properties of the class.  Next, you can see that the default view makes the hierarchy a flat layout.  For example, the package &#8220;com.developmentarc.test&#8221; is listed all in one line which means you don&#8217;t have to expand to view the structure to access a deep package like you would in the Flex Navigator.  If you don&#8217;t like the flat layout you can change the settings of the Package Explorer to create the more traditional parent/child structure.  </p>

<p>Finally, look at the Flex 4 SWC package in the screen cap.  The Flex SDK version for the project is now shown in the Package Explorer.  Not only is it shown but you can expand the SWCs to see the content structure, classes and properties contained within.  In the screen cap I am exploring the Array in the playerglobal.swc file.  This SWC exploration also works with 3rd party SWCs, not just the Flex 4 SDK. These all may seem like small changes, but it makes exploration, creation and modification of existing projects much easier. This also allows for Java Eclipse users to experience a much more familiar view when developing Flex Applications.  Good stuff.</p>

<p><strong>Event Auto-generation</strong></p>

<p>During development, creating event bindings and method handlers such as the Mouse click event, is a common task.  The event auto-generation is a short cut feature in Design View that allows a developer to quickly generate an event handler for a component.  The example they used in the session was adding an event handler for a Button&#8217;s click event.  From design view the developer would click on the generate handler button next to the on click event field:</p>

<p><img src="http://blog.vivisectingmedia.com/wp-content/uploads/2008/12/event-creator.png" alt="Event Creator" /></p>

<p>When the user clicks this button, the handler function is generated in an &lt;Script&gt; block within the MXML file and the method is then assigned to the Button&#8217;s click property.  My personal feelings about this feature is that it is helpful for initial development and/or learning of the Flex event handling model, but for complex development I am hesitant to see this as a go to feature.  For one, the feature automatically adds the script to the current MXML file.  On larger projects I typically avoid having scripts in MXML, this is especially true when <a href="http://blog.vivisectingmedia.com/2008/04/the-flex-code-behind-pattern/" title="The Code Behind Pattern">following the code-behind pattern</a>.  I also feel this is going against the proposed workflow model that Adobe is pushing for Flash Catalyst and Flex Builder integration since your MXML is becoming more layout-centric with Classes for logic.</p>

<p>Overall, its a helpful feature and it makes prototyping, initial development and demoing a lot easier.  We will see how this feature matures and how it is adopted by developers in the future.</p>

<p><strong>ASDoc</strong></p>

<p>ASDoc-ing is the process of adding specially formatted comments to your code that enables the ASDoc tool to generate documentation from the code and formatted comments.  ASDoc is just like JavaDoc and has been evolving and maturing the the first release of Flex. One feature that has been part of Eclipse&#8217;s Java development suite is the ability to see inline contextual JavaDoc notes for specific Classes, properties and/or methods.  The next version of Flex Builder supports the same kind of in-context doc hinting for code that has been properly ASDoc-ed.</p>

<p><img src="http://blog.vivisectingmedia.com/wp-content/uploads/2008/12/asdoc-example.png" alt="Asdoc Example" /></p>

<p>When a user hovers over a method, property or class in either ActionScript or MXML the ASDoc tooltip will display showing the contextual docs for the specified attribute.  In the screenshot above, I am hovering over the &#8220;left&#8221; attribute of an &lt;FxButton&gt; tag.  Notice that the displayed doc notes are only for the &#8220;left&#8221; property.  If I rolled over &lt;FxButton portion of the tag you would see the ASDoc content for just FxButton. Not only can you view content inline, there is now a ASDoc panel that can be added to you workspace layout.  This panel automatically updates when you click on a property, method, class, tag or attribute.  This way you can have the view up and accessing ASDoc content without having to wait for the rollover event.</p>

<p>The ASDoc feature also works within your own code.  For example, if you are developing a custom Class and provide properly formatted ASDocs in the Class, the ASDoc window/tooltip will show your ASDoc notes whenever you reference your Class in the application.  The Flex team is also working on a new SWC format nicknamed &#8220;Fat SWC&#8221;.  This Fat SWC will now include all ASDoc notes that are provided in the source code for the SWC compilation.  This means that when you use a &#8220;Fat SWC&#8221; in your project, you will get ASDoc hints for any of the Classes, properties, methods, tags or tag attributes that are contained within the SWC.</p>

<p><strong>New Re-factoring Features</strong></p>

<p>There are two new re-factoring features that were demonstrated during the session, both of which help automate tasks that developers do on a weekly, if not daily basis.  The first feature is the ability to auto-generate a getter/setter pair from an existing property.</p>

<p><img src="http://blog.vivisectingmedia.com/wp-content/uploads/2008/12/get-set-dialog.png" alt="Get Set Dialog" /></p>

<p>To use this feature you first select, or apply cursor focus, the property that you want to convert to a getter/setter pair.  You can then either <em>right click &gt; source &gt; Generate Getter Setter&#8230;</em> or you can choose the <em>Source &gt; Generate Getter Setter&#8230;</em> menu option.  This brings up the above options dialog where you can decide if the existing property should become private, what the new name of the property should become (it defaults to a single underscore prefix), the name of the exposed get / set method, should it create both the get and set method, or just one or the other and finally where the new methods should be added.</p>

<p>This is one of those at first glance small yet super powerful features.  During development there are many times where you need to convert a basic public property to a get/set pair and this helps write the code quickly and elegantly. One other cool thing to note about this feature is its ability to remember your preferred prefix.  As I mentioned earlier, the default is a single underscore prefix for the private method.  I personally prefer a double underscore prefix for my get/set value properties and when I added that additional underscore Flex Builder remembered this and kept the double underscore when I used the re-factor function again.  Very, very cool.</p>

<p>The second re-factoring feature, isn&#8217;t really new, its just a lot more complete.  When re-factoring the code-base, one of the biggest challenges is the process of moving Classes around to new packages.  This may occur for all kinds of different reasons: better project organization, company name changes, project changes, etc. etc.  Eclipse (which Flex Builder is built upon) supports the move option, yet in the current release this would just move the resource to the new folder and not update the Class&#8217; package structure and linkage throughout the workspace.  If you used the rename function, Flex Builder would handle all the updating for you, so it always felt as if the functionality was half-complete.</p>

<p>Well, now move is finally fully supported.  This is great news for those of us that have had to spend five minutes moving resources and then the next hour fixing the compiler errors.  Typically the process was move the resource, build the project, look for errors, updating bad references, re-build the project, looking for bad references, rinse and repeat.  This can get really ugly when you have 800+ classes (which I have had in the past) and you are doing a massive project re-structure.  Not fun, let me tell ya.  When you choose to move an asset you get to choose what package to move it to and Flex Builder handles updating all the linkage references and package declarations.  </p>

<p><img src="http://blog.vivisectingmedia.com/wp-content/uploads/2008/12/move-example.png" alt="Move Example" /></p>

<p>One thing to note, in the current preview build of Flex Builder the package has to exist before you move the resource to it.  If I recall, David Zuckerman mentioned that this will change in the future.  Even if it doesn&#8217;t this is a great feature, one that I want NOW. It would have been soooo nice yesterday when I had to move a ton of MXML classes around and re-link them throughout the project, oh well.</p>

<p><strong>Code Templates</strong></p>

<p>Code templates are more or less self exactly what they sound like.  A code template is similar to an email signature in that it provides the ability to auto-fill predefined content when a new MXML, ActionScript or CSS file is created.  In a way, we have already been using Code Templates when we generate a new ActionScript class.  The provided package structure, class declaration and constructor are all generated by the Code Template engine.  The difference is that in the next version of Flex Builder this will all be exposed to you and fully modifiable.</p>

<p><img src="http://blog.vivisectingmedia.com/wp-content/uploads/2008/12/code-templates.png" alt="Code Templates" /></p>

<p>Are you working on a MIT or other licensed project? You can now add the license block to the template code and any new file will have the proper structure.  You can predefine code blocks, ASDoc blocks, code layout and preferred structures all within the code templates.  You can also use variables that set time, file path, names, etc. into the template.  On top of that you can export your created templates, pass them around and have others import the template into their Flex Builder.  This is great if you work with multiple developers and want to create better coding standards or if you want to create company or open source project specific structures.  Again, a simple yet very powerful feature that is being exposed to us.</p>

<p><strong>Code Navigation</strong></p>

<p>The last Test &amp; Tune feature I am going to talk about in this post is code navigation.  Code navigation shows a developer in code view what methods are overrides and implementations of interfaces. It also allows for quick navigation up to the parent reference when the triangle icon is clicked.</p>

<p><img src="http://blog.vivisectingmedia.com/wp-content/uploads/2008/12/code-navigation.png" alt="Code Navigation" /></p>

<p>This is a nice feature because in the past you had to look at the Interface itself to determine what methods are implemented in the current class related.  Overriding was generally easy to see at a glance because of the override keyword but getting to the parent method instance was not so easy.  This feature enables both code readability and better navigation within the application.</p>

<p>Well, that is all for now.  I have three more topics to write about, two more test &amp; tune and then a look at the new Data service support.  I will save those for the final post.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.vivisectingmedia.com/2008/12/adobe-max-2008-the-future-of-flex-builder-part-two/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Turkey Day Travels</title>
		<link>http://blog.vivisectingmedia.com/2008/11/turkey-day-travels/</link>
		<comments>http://blog.vivisectingmedia.com/2008/11/turkey-day-travels/#comments</comments>
		<pubDate>Wed, 26 Nov 2008 15:49:52 +0000</pubDate>
		<dc:creator>James Polanco</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flash Player]]></category>
		<category><![CDATA[Flex Development]]></category>
		<category><![CDATA[General Media / Stuff]]></category>
		<category><![CDATA[MAX 2008]]></category>
		<category><![CDATA[Rich Internet Applications]]></category>
		<category><![CDATA[Adobe]]></category>
		<category><![CDATA[degrafa]]></category>
		<category><![CDATA[max]]></category>
		<category><![CDATA[milan]]></category>
		<category><![CDATA[travel]]></category>

		<guid isPermaLink="false">http://blog.vivisectingmedia.com/2008/11/turkey-day-travels/</guid>
		<description><![CDATA[Its the day before Thanksgiving, Turkey&#8217;s eve I guess, the most glorious of American gluttony-days, uh I mean holidays. A wonderful day full of family, football and of course food. Oh the food!  I am only being semi-facetious, I really do love the three F&#8217;s of the holiday.  This year, the travel is [...]]]></description>
			<content:encoded><![CDATA[<p>Its the day before Thanksgiving, Turkey&#8217;s eve I guess, the most glorious of American gluttony-days, uh I mean holidays. A wonderful day full of family, football and of course food. Oh the food!  I am only being semi-facetious, I really do love the three F&#8217;s of the holiday.  This year, the travel is crazy.  I am flying down to Southern California to spend the holiday with my fiancÃ©e&#8217;s family and then I fly back home Friday night to pack and then Aaron and I head out to Milan for the Adobe MAX EU the next evening.</p>

<p>Aaron and I have a TON of notes and thoughts that are being worked on, all thanks to MAX North America.  I am sure we will have even more coming from MAX EU. We will both be working on a ton of new posts over the coming few weeks and at the same time we are in the process of getting our <a href="http://www.developmentarc.com/">DevelopmentArc site</a> proper (don&#8217;t forget client work and other assorted projects we are working on too!).  More to come on that later.  So, for those of you in/from the States that celebrate T-day, enjoy!  For those of you that don&#8217;t, no worries, you can laugh at us as we Americans stumble around in a triptiphane induced food coma.</p>

<p>Oh, and for those of you that read this blog for Flex/Flash/Technology info, read up on my newest obsession while I am away: <a href="http://www.degrafa.org/" title="Degrafa Framework Website: Download, Docs and Examples">Degrafa</a>. Degrafa is short for &#8220;<strong>De</strong>clarative <strong>Gra</strong>phics <strong>F</strong>r<strong>a</strong>mework&#8221; and it is a set of Classes that enable you to create simple to complex graphical content using MXML instead of having to write a bunch of ActionScript graphics calls.  But that is just the tip of the iceberg, you can actually make <a href="http://www.jamesward.com/wordpress/2008/04/15/flex-3-skin-transitions-with-degrafa-and-animatecolor/" title="James Ward's post on skinning a component, best example using states">component skins (states and all!)</a> using their framework.  Its rare for me to get this excited about a new framework, I am usually super hesitant about jumping in, but after a demo at the MAX Birds of a Feather, and now getting my hands dirty in it, I am super impressed.  Good work guys!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.vivisectingmedia.com/2008/11/turkey-day-travels/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>VivMedia Code Library: Version 0.03 released</title>
		<link>http://blog.vivisectingmedia.com/2008/07/vivmedia-code-library-version-003-released/</link>
		<comments>http://blog.vivisectingmedia.com/2008/07/vivmedia-code-library-version-003-released/#comments</comments>
		<pubDate>Sat, 19 Jul 2008 18:13:25 +0000</pubDate>
		<dc:creator>James Polanco</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Distribution]]></category>
		<category><![CDATA[Flash Player]]></category>
		<category><![CDATA[Flex Development]]></category>
		<category><![CDATA[Rich Internet Applications]]></category>
		<category><![CDATA[Self Reference]]></category>
		<category><![CDATA[web 2.0]]></category>
		<category><![CDATA[web integration]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[unittest]]></category>
		<category><![CDATA[vivmedia code depot]]></category>

		<guid isPermaLink="false">http://blog.vivisectingmedia.com/2008/07/vivmedia-code-library-version-003-released/</guid>
		<description><![CDATA[I pushed a new version of the library yesterday to Google Code.  This is a relatively small release, which was not really my intention for version 0.03.  Originally, I was planning on adding the first of a series of really powerful features but due to unit testing the new Queue (48 new tests), [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://blog.vivisectingmedia.com/wp-content/uploads/2007/10/rune_flash.gif" alt="rune_flash.gif" align="left" border="0" hspace="10" vspace="5" />I pushed a <a href="http://code.google.com/p/vivisectingmedia-as3/downloads/list" title="Vivisecting Media Code Library version 0.03">new version of the library yesterday to Google Code</a>.  This is a relatively small release, which was not really my intention for version 0.03.  Originally, I was planning on adding the first of a series of really powerful features but due to unit testing the new Queue (48 new tests), this kind of slowed down progress.  Oh, that and a TON of client deadlines.  Anyway, there are two updates in this version: The Queue and new source code licensing.</p>

<p>I read a great <a href="http://www.gskinner.com/blog/archives/2008/07/source_code_lic_1.html" title="gSkinner: Source Code Licenses">analysis article by Grant Skinner, talking about the different Open Source licenses</a> and how they can affect you and your client.  I originally licensed the code library under the Mozilla Public License (MPL) due to its popularity, its freedom to allow developers to build on top of the code base for any purpose (free or for charge) and only requires code modification submission if the developer changed the original code in the library.  My goal with VivMedia is to help developers build great applications, and the last thing I wanted to do is limit them, yet I do want to encourage people to submit changes and updates.  That is why I went with MPL at first.</p>

<p>The issue with MPL (as Grant points out) is that when you use MPL code there are certain steps you need to take to release your project.  In most cases this is not a big deal and it very rarely impacts the project but it may cause some developers or their clients a bit of grief.  In all honesty, I just want to create a library that myself and others can use without having to jump through hoops with our clients.  This lead me to change all the code license to MIT, which is very permissive of all forms of development. All you need to do is keep the MIT license headers in the source.  You don&#8217;t have to submit changes (all thought I would be very happy if you did) and you don&#8217;t have to jump through any other legal hoops when deploying. So, if you are using the library already, update to the new version and know that the door is now wide open and you don&#8217;t have to fret about how you or your clients release the work.</p>

<p>The other addition to this version is the new Queue data structure.  The Queue allows you to add and remove items is in a linear fashion.  You can define the Queue as either Last in First Out (LIFO) or First in First Out (FIFO).  What that means is when you add() item A and item B to the Queue and then call next(), the item returned is dependent if the queue is set to LIFO or FIFO.  A LIFO Queue returns item B, since it was the last item added.  A FIFO Queue returns item A since it was the first item added.  As always, a full tutorial will be posted soon on how to use the Queue in your code.</p>

<p>The Queue is a building block for a lot of new functionality that I am starting to plan out.  There will probably be variations of the Queue going forward too.  For example a version that is designed to help solve Asynchronous issues in a Synchronous fashion, without you having to write a lot of logic around the actions.  Right now, its all about what time permits&#8230; and as I said in the last post, vacation is nearly here!  So, update/download the library, check out the ASDocs in the source (I always update them for each release) and let me know what you all think.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.vivisectingmedia.com/2008/07/vivmedia-code-library-version-003-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>VivMedia Code: Selection Controller</title>
		<link>http://blog.vivisectingmedia.com/2008/07/vivmedia-code-selection-controller/</link>
		<comments>http://blog.vivisectingmedia.com/2008/07/vivmedia-code-selection-controller/#comments</comments>
		<pubDate>Thu, 03 Jul 2008 21:34:02 +0000</pubDate>
		<dc:creator>James Polanco</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flash Player]]></category>
		<category><![CDATA[Flex Development]]></category>
		<category><![CDATA[Rich Internet Applications]]></category>
		<category><![CDATA[documentation]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[ISelectable]]></category>
		<category><![CDATA[multiselect]]></category>
		<category><![CDATA[SelectionController]]></category>
		<category><![CDATA[tools]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[vivisecting media code depot]]></category>

		<guid isPermaLink="false">http://blog.vivisectingmedia.com/2008/07/vivmedia-code-selection-controller/</guid>
		<description><![CDATA[With the previous release of version 0.02 of the code base I added a new class called the SelectionController that enables you to treat items that implement the ISelectable interface as a selection group.  As promised, this is the tutorial/indepth examples of the SelectionController and how to use it within your applications.



Overview
The SelectionController allows [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://blog.vivisectingmedia.com/wp-content/uploads/2007/10/rune_flash.gif" alt="rune_flash.gif" align="left" border="0" hspace="10" vspace="5" />With the previous <a href="http://blog.vivisectingmedia.com/2008/06/vivmedia-code-library-version-002-released/" title="VivMedia Code update">release of version 0.02 of the code base</a> I added a new class called the SelectionController that enables you to treat items that implement the ISelectable interface as a selection group.  As promised, this is the tutorial/indepth examples of the SelectionController and how to use it within your applications.</p>

<p><span id="more-132"></span></p>

<p><strong>Overview</strong><br/>
The SelectionController allows you to group items similarly to how a radio group works in both HTML and Flex.  The general principle with a radio group is that when one radio button is selected, any of the other radio buttons in the group are then deselected.  This kind of selection occurs in Lists, DataGrids, etc.</p>

<p>The challenge is that in a highly stylized Flash/Flex applications we often need to treat custom components as a group.  One example is my <a href="http://blog.vivisectingmedia.com/2008/03/sxsw-2008-hot-freaks-party-and-dr-dog-photos/" title="PhotoSlider">Wordpress PhotoSlider</a>.  When a user selects a thumbnail, the selected thumbnail is highlighted and the previously highlighted thumbnail is deselected.</p>

<p>A typical solution is to make the parent container smart enough to understand when an item is selected and then let the parent change the highlight state.  This approach can get tricky when you have items that are deeply nested and don&#8217;t share an easily accessible common parent or if you have complex selection/deselection requirements that may span across multiple containers or even have selected children that are currently off the display stack.</p>

<p>To solve the easiest issues to the most complex selection challenges I developed a SelectionController that is a Singleton instance that manages selection of content for you.  Items first need to implement the ISelectable interface and then they can be added to a selection group.  When you want to select or deselect a group you can either pass in the group ID or you can pass in one of the items in the group to handle selection.</p>

<p><strong>Examples</strong><br/>
First, lets look at our item that we want to be selectable and then we will look at how you can select/deselect the item.</p>


<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package com.<span style="color: #006600;">vivisectingmedia</span>.<span style="color: #006600;">tutorial</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> com.<span style="color: #006600;">vivisectingmedia</span>.<span style="color: #006600;">framework</span>.<span style="color: #006600;">controllers</span>.<span style="color: #006600;">SelectionController</span>;
	<span style="color: #0066CC;">import</span> com.<span style="color: #006600;">vivisectingmedia</span>.<span style="color: #006600;">framework</span>.<span style="color: #006600;">controllers</span>.<span style="color: #006600;">interfaces</span>.<span style="color: #006600;">ISelectable</span>;
&nbsp;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
&nbsp;
	<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">controls</span>.<span style="color: #0066CC;">Button</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MySelectionButton <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">Button</span> <span style="color: #0066CC;">implements</span> ISelectable
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">// constructor</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> MySelectionButton<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * The default method for when a Button is clicked.
		 * We override this method so that when a user clicks
		 * on this Button we select this item and deselect
		 * all the other Buttons in the group.
		 * 
		 * @param event Click event.
		 * 
		 */</span>
		override protected <span style="color: #000000; font-weight: bold;">function</span> clickHandler<span style="color: #66cc66;">&#40;</span>event:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">super</span>.<span style="color: #006600;">clickHandler</span><span style="color: #66cc66;">&#40;</span>event<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// this item is now selected</span>
			SelectionController.<span style="color: #006600;">selectItem</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * This setter is called when the SeletionController
		 * changes the Button's selection state.  Technically,
		 * we do not have to override this method because the Button
		 * Class already supports the selected getter/setter signature
		 * required by the ISelectable interface.
		 *  
		 * @param value True is selected, false is deselected.
		 * 
		 */</span>		
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> selected<span style="color: #66cc66;">&#40;</span>value:<span style="color: #0066CC;">Boolean</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// store the private reference</span>
			<span style="color: #0066CC;">super</span>.<span style="color: #006600;">selected</span> = value;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> selected<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Boolean</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">super</span>.<span style="color: #006600;">selected</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>


<p>A couple of notes about the above code.  As I mentioned in the comments, we don&#8217;t really need to override the get/set method for selected.  The Button Class already meets the ISelectable interface requirement, but you will still need to extend the Button class to add the ISelectable interface so that it works with the SelectionController.  I overrode the code just to show you how you would need to implement this in a component that does not already support selection states.  What I really want you to focus on is <em>implements ISelectable</em> and the <em>clickHandler()</em> method.</p>

<p>In the clickHandler() call we pass up the super.clickHandler() and then we call the SelectionController.selectItem() passing the component instance reference in as the argument.  What this is telling the SelectionController to do is first, see if the passed item is in a selection group, if so, select the item by setting selected to true, and then set selected value to false on any other item in the selection group.  The SelectionController is friendly enough that if the item passed in is not in a selection group, then the item is still selected and nothing else happens.</p>

<p>So how do you leverage the selection object?  Let&#8217;s look at some examples:</p>


<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// create some selectable items</span>
<span style="color: #000000; font-weight: bold;">var</span> buttonOne:MySelectionButton = <span style="color: #000000; font-weight: bold;">new</span> MySelectionButton<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> buttonTwo:MySelectionButton = <span style="color: #000000; font-weight: bold;">new</span> MySelectionButton<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> buttonThree:MySelectionButton = <span style="color: #000000; font-weight: bold;">new</span> MySelectionButton<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// generate an group id to assign them to</span>
<span style="color: #000000; font-weight: bold;">var</span> groupOne:<span style="color: #0066CC;">int</span> = SelectionController.<span style="color: #006600;">generateNewId</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// assign the items to the group</span>
SelectionController.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span>buttonOne, groupOne<span style="color: #66cc66;">&#41;</span>;
SelectionController.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span>buttonTwo, groupOne<span style="color: #66cc66;">&#41;</span>;
SelectionController.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span>buttonThree, groupOne<span style="color: #66cc66;">&#41;</span>;</pre></div></div>


<p>This is the basic use case for grouping items.  First, we use a helper method to generate a new group ID.  The ID is an int and it must be unique for each group.  The generateNewId() returns the next ID number not in use.  Once you have an ID you can then add items to the SelectionController using the addItem() method.  This stores the reference of the item and pairs it with the group.</p>

<p>Because the MySelectionButton is built with the SelectionController in mind we don&#8217;t have to do anything else if we want these three buttons to behave as a group.  When a user clicks one of the buttons in the group, the clickHandler calls the SelectionController and the SelectionController will then select the clicked button and deselect the rest.</p>

<p>What happens when you want to programatically select one item, or all of the items in the group or deselect all the items in the group? The SelectionController provides methods to handle these tasks:</p>


<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// select one item</span>
SelectionController.<span style="color: #006600;">selectItem</span><span style="color: #66cc66;">&#40;</span>buttonTwo<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// select all the items, first get the group if</span>
<span style="color: #808080; font-style: italic;">// you don't know it</span>
<span style="color: #000000; font-weight: bold;">var</span> currentGroup:<span style="color: #0066CC;">int</span> = SelectionController.<span style="color: #006600;">getItemGroup</span><span style="color: #66cc66;">&#40;</span>buttonOne<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// now select the group</span>
SelectionController.<span style="color: #006600;">selectAll</span><span style="color: #66cc66;">&#40;</span>currentGroup<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// or you can deselect them all</span>
SelectionController.<span style="color: #006600;">deselectAll</span><span style="color: #66cc66;">&#40;</span>currentGroup<span style="color: #66cc66;">&#41;</span>;</pre></div></div>


<p><strong>Considerations and Gotchas</strong><br/>
As with all my tutorials, lets talk about things you should consider/know about the SelectionController that may cause you some trouble if you attempt to do certain things.</p>

<p><em>One group to an item.</em>  Selectable items can not be members of multiple groups and when a new group is applied the item is removed from the original group. For example:</p>


<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// create a new group</span>
<span style="color: #000000; font-weight: bold;">var</span> groupTwo:<span style="color: #0066CC;">int</span> = SelectionController.<span style="color: #006600;">generateNewId</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// apply the group to ButtonTwo</span>
SelectionController.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span>buttonTwo, groupTwo<span style="color: #66cc66;">&#41;</span>;</pre></div></div>


<p>Doing the above would remove ButtonTwo from groupOne and when you call selectAll() on groupOne only buttonOne and buttonThree would be selected.</p>

<p><em>Items are Strong referenced.</em>  When an item is added to a group it is strong referenced in memory.  This means that if you want to delete the item so that Garbage Collection can be applied you need to remove the item form the SelectionController by calling removeItem().  This removes the item from the SelectionController and Garbage Collection can be applied when the next pass occurs.</p>

<p><em>Multi-Select is supported by not built in by default.</em>  If you want to create a multi-selectable grouping you can use the SelectionController for this kind of functionality, but you will need to add more logic to you selectable items.  For example, we want our MySelectionButton&#8217;s to be multi-selectable when a user has the Control/Command key down.  To do this in our example you would modify the clickHandler() like so:</p>


<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">override protected <span style="color: #000000; font-weight: bold;">function</span> clickHandler<span style="color: #66cc66;">&#40;</span>event:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">super</span>.<span style="color: #006600;">clickHandler</span><span style="color: #66cc66;">&#40;</span>event<span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>event.<span style="color: #006600;">ctrlKey</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">// do not select using the controller</span>
		<span style="color: #0066CC;">this</span>.<span style="color: #006600;">selected</span> = <span style="color: #000000; font-weight: bold;">true</span>;
	<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">// do select using the controller</span>
		SelectionController.<span style="color: #006600;">selectItem</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>


<p>The above example determines if the user is multi-selecting by checking the ctrlKey value in the MouseEvent.  The MouseEvent passes true if the key is down and false if the key is up. In the case when the key is down we want the object selected but we don&#8217;t want the Controller to deselect the rest of the items in the group.  Otherwise, we do want the rest of the items to be deselected, so use then use the Controller.</p>

<p>That is the basics of the SelectionController, play around with it and let me know if you need more examples, find any issues or have any questions with the Controller.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.vivisectingmedia.com/2008/07/vivmedia-code-selection-controller/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Adobe Edge Article: Coding Standards</title>
		<link>http://blog.vivisectingmedia.com/2008/06/adobe-edge-article-coding-standards/</link>
		<comments>http://blog.vivisectingmedia.com/2008/06/adobe-edge-article-coding-standards/#comments</comments>
		<pubDate>Sat, 14 Jun 2008 20:50:41 +0000</pubDate>
		<dc:creator>James Polanco</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flex Development]]></category>
		<category><![CDATA[General Media / Stuff]]></category>
		<category><![CDATA[Rich Internet Applications]]></category>
		<category><![CDATA[Self Reference]]></category>
		<category><![CDATA[web 2.0]]></category>
		<category><![CDATA[web integration]]></category>
		<category><![CDATA[adobe edge]]></category>
		<category><![CDATA[article]]></category>
		<category><![CDATA[code conventions]]></category>
		<category><![CDATA[code standards]]></category>

		<guid isPermaLink="false">http://blog.vivisectingmedia.com/2008/06/adobe-edge-article-coding-standards/</guid>
		<description><![CDATA[A few months ago I was asked to write an article for the Adobe Edge Newsletter on a topic of my choosing.  After spending a bit of time thinking about what moved me at the moment, I decided to sit down and write about the need for RIA and web app developers to invest [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://blog.vivisectingmedia.com/wp-content/uploads/2008/06/adobe-edge.png" alt="Adobe Edge" align="left" border="0" hspace="10" vspace="5" />A few months ago I was asked to write an article for the <a href="http://www.adobe.com/newsletters/edge/" title="Adobe Edge Newsletter">Adobe Edge Newsletter</a> on a topic of my choosing.  After spending a bit of time thinking about what moved me at the moment, I decided to sit down and write about the need for RIA and web app developers to invest time into research, development and adoption of code standards. This week, the article &#8220;<a href="http://www.adobe.com/newsletters/edge/june2008/articles/article6/index.html?trackingid=DEKYJ" title="Adobe Edge Article...">Coding standards: What are they and why should you use them?</a>&#8221; went live and I want to share the link with everyone.</p>

<p>One of the challenges of writing an article like this is first defining your audience and then determining how to cover the topic in the alloted text.  The Edge sets a word count guideline and this limits how deep I could or in this case could not go.  Personally, I fully support the word count guide because this both focuses the topic and generates more digestible articles.  Yet, on a topic as broad and as deep as this one I had to focus more on an introductory guide to coding standards and adoption.</p>

<p>The second challenge is determining your audience.  My goal was to create an article that worked for both readers who have never heard of coding standards, and readers that have been using them for years.  I always try to create a broad stripe across my reader base because I want to inform as many people as possible.  This thought process was kind of beaten into me during my years as a Technical Support rep and team-lead at Macromedia.  There is nothing like having to write Tech-notes and email support to teach you how to write to a broad audience.</p>

<p>The main point I wanted to get across with this article is that coding standards are actually much more then just code formating.  Code formatting, or code conventions, really focus on how you name variables, methods, classes, etc. Yet, this is just the tip of the coding standards iceberg.  One of the least discussed aspects of coding standards is the adoption and implementation practice of code design patterns.  As developers, we need to better understand how adoption of patterns, frameworks and micro-architectures help structure our code.  Not only do they provided rapid development but they also help guide us into a more structured development process.</p>

<p>As RIA and web application development expands we are seeing a demand for much more complex and scalable applications. Scalability is not just about user load but includes further expansion of features <em>and</em> performance over time.  More and more developers are being asked to create applications that have a lifespan longer then just a few months.  Some people may gaffe at that comment, but if you look at a lot of &#8220;rich&#8221; applications up to this point, most have been for marketing campaigns and advertising.  The nature of these kinds of projects do not focus on lifespan and version updates. Their focus is on making them work in a short time to meet the marketing deadline and then get them out the door.</p>

<p>With the trend of Web 2.0 (which I <em>hate</em> as a term), the web world is quickly being pushed into the long standing Desktop Software model. This is forcing a lot of us to re-evaluate how we design and develop our online enabled applications.  This is a scary yet wonderful time in our industry as we are rapidly adopting and developing new ways of web application design and construction. Due to this push I feel its time to help expose more of us to coding standards. My article is more a call to arms then a definitive guide to coding standards implementation.  If you are interested, take a read and add your thoughts to the comments&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.vivisectingmedia.com/2008/06/adobe-edge-article-coding-standards/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>VivMedia Code Library: Version 0.02 Released</title>
		<link>http://blog.vivisectingmedia.com/2008/06/vivmedia-code-library-version-002-released/</link>
		<comments>http://blog.vivisectingmedia.com/2008/06/vivmedia-code-library-version-002-released/#comments</comments>
		<pubDate>Sat, 07 Jun 2008 00:15:56 +0000</pubDate>
		<dc:creator>James Polanco</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flash Player]]></category>
		<category><![CDATA[Flex Development]]></category>
		<category><![CDATA[Rich Internet Applications]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[unittest]]></category>
		<category><![CDATA[vivmedia code depot]]></category>

		<guid isPermaLink="false">http://blog.vivisectingmedia.com/2008/06/vivmedia-code-library-version-002-released/</guid>
		<description><![CDATA[Its been a busy few weeks for me (with work and personal projects) but I am happy to say that version 0.02 of the VivMedia library is now available.  This update includes a fully automated Unit Test suite for all the code except the Local Connection Manager.  Trying to automate the LCM is [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://blog.vivisectingmedia.com/wp-content/uploads/2007/10/rune_flash.gif" alt="rune_flash.gif" align="left" border="0" hspace="10" vspace="5" />Its been a busy few weeks for me (with work and personal projects) but I am happy to say that <a href="http://code.google.com/p/vivisectingmedia-as3/downloads/list" title="Vivisecting Media Library version 0.02">version 0.02 of the VivMedia library</a> is now available.  This update includes a fully automated Unit Test suite for all the code except the Local Connection Manager.  Trying to automate the LCM is going to be coding feat in itself, so right now it is being tested the old fashioned way a.k.a. by hand. There are a few minor updates to the existing code such as streamlining how the HashTable clones the internal Array and some other minor updates to support automated testing. As usual the code is fully ASDoced and generated docs are available with the ZIP download.</p>

<p>The big change is this version is the addition of the new SelectionController and ISelectable interface. The SelectionController is a manager that allows you to group any number of items that implement the ISelectable interface and then treat them as a Radio Group or multi-selectable group.  With the controller you assign items to groups and then you can use the controller to select and deselect the items.</p>

<p>A great example of this code is how I handle image highlighting in the <a href="http://blog.vivisectingmedia.com/2008/03/sxsw-2008-hot-freaks-party-and-dr-dog-photos/" title="Photoslider example">Photoslider</a>.  Each of my image thumbnails implement ISelectable and then when a user clicks a thumb it calls the SelectionController.selectItem() and passes itself as an argument.  The SelectionController then sets selected on all the thumbnails in the group, setting true for the passed in item and false for the rest.  I plan on doing a full tutorial in the near future so keep an eye out for it.  Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.vivisectingmedia.com/2008/06/vivmedia-code-library-version-002-released/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The Extendable Singleton Pattern</title>
		<link>http://blog.vivisectingmedia.com/2008/06/the-extendable-singleton-pattern/</link>
		<comments>http://blog.vivisectingmedia.com/2008/06/the-extendable-singleton-pattern/#comments</comments>
		<pubDate>Thu, 05 Jun 2008 20:42:43 +0000</pubDate>
		<dc:creator>James Polanco</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flex Development]]></category>
		<category><![CDATA[Rich Internet Applications]]></category>
		<category><![CDATA[patter]]></category>
		<category><![CDATA[singleton]]></category>
		<category><![CDATA[singleton lock]]></category>

		<guid isPermaLink="false">http://blog.vivisectingmedia.com/2008/06/the-extendable-singleton-pattern/</guid>
		<description><![CDATA[The Singleton pattern is one of my favorite little patterns that can be used in any language that provides static typed properties/methods.  There are many different uses of the Singleton and just as many ways to implement them within code.  In this post I am going to first introduce the basic Singleton pattern, [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://blog.vivisectingmedia.com/wp-content/uploads/2007/10/rune_flash.gif" alt="rune_flash.gif" align="left" border="0" hspace="10" vspace="5" />The Singleton pattern is one of my favorite little patterns that can be used in any language that provides static typed properties/methods.  There are many different uses of the Singleton and just as many ways to implement them within code.  In this post I am going to first introduce the basic Singleton pattern, explore how it is most often defined in ActionScript (including tips on protecting the Singleton instance), then talk about how to create an extendable Singleton and why on earth you would want to.  If you already know the principles of Singletons, skip ahead to the second section of this post &#8220;Defining Singletons in ActionScript&#8221;.</p>

<p><span id="more-119"></span></p>

<p><strong>What Is a Singleton?</strong><br/>
If you have never heard of the Singleton Pattern you are not alone, I know many senior Java and ActionScript developers that looked at me blankly when I brought up the pattern in passing.  The core idea of the pattern is to solve the issue of when you need only once instance of an object that can be accessed globally in you application.  </p>

<p>For example, you have a User object that stores the user information once they are logged in.  Throughout your code you want to use this information and you also want to make sure that there is only once instance of this user data.   One solution is to pass around a value that represents the user, yet there are many issues with this kind of approach.  First, the code needs to understand this data and expect it when called.  Second, what happens if the user logs out or changes the data, such as their address?  The code needs to know this the next time its called.  Passing it around gets ugly very very fast.</p>

<p>Another solution is to save the values in a global variable.  But where should this live and how should it be accessed?  Does it live in _root (really, really bad idea) or the top Application?  What happens when your code gets nested in another application or is modularized?  How can you find the data?  This is where the singleton comes in. The Singleton pattern leverages the power of static properties and methods which are bound to the Class type itself.  What!?!  Let me show you:</p>


<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package com.<span style="color: #006600;">vivisectingmedia</span>.<span style="color: #006600;">example</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> User
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">name</span>:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> address:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> birthDate:<span style="color: #0066CC;">Date</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>


<p>So there is our basic user class.  Its just a simple data object class that stores our information.  In most cases we create a new instance of it and then start manipulating it, such as:</p>


<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> loggedInUser:User = <span style="color: #000000; font-weight: bold;">new</span> User<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
loggedInUser.<span style="color: #0066CC;">name</span> = <span style="color: #ff0000;">&quot;Bob&quot;</span>;
loggedInUser.<span style="color: #006600;">address</span> = <span style="color: #ff0000;">&quot;123 Market St.&quot;</span>;
loggedInUser.<span style="color: #006600;">birthData</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Date</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1976</span>, <span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>


<p>In the above example we are working with an instance of a class.  We used the constructor to generate the instance and we are manipulating the properties of that instance.  In ActionScript 3.0 we can leverage the static keyword to define properties on the class itself:</p>


<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package com.<span style="color: #006600;">vivisectingmedia</span>.<span style="color: #006600;">example</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> User
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">static</span> <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">name</span>:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">static</span> <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> address:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">static</span> <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> birthDate:<span style="color: #0066CC;">Date</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
....
&nbsp;
<span style="color: #808080; font-style: italic;">// we now access the name value through the Class, not an instance</span>
User.<span style="color: #0066CC;">name</span> = <span style="color: #ff0000;">&quot;Bob&quot;</span>;
User.<span style="color: #006600;">address</span> = <span style="color: #ff0000;">&quot;123 Fake St.&quot;</span>;
User.<span style="color: #006600;">birthData</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Date</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1976</span>, <span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>


<p>Those values are being set on the Class not on an instance.  This means that when you change values in one part of the code and then access the class in other parts of the code the stored values are the same.  In a way we have just defined a global data object that stores our user data.  The Singleton pattern takes the ability to store values on the Class and then steps it up a notch.</p>

<p>In theory we could go in and mark EVERYTHING static but this is not ideal and can lead to complications.  The better way is to create a hybrid of the first User class example and the second.  We do this by exposing a static method on User that checks if an instance of User is created and if not creates one and then returns it.  This part of the Singleton is kind of a mind-bender so lets just jump into code.</p>

<p><strong>Defining Singletons in ActionScript</strong><br/>
The core idea of the Singleton is to have static helper methods that manages our object instance by allowing only one instance to exist and also provides access to this instance.  Using the User example (above) here is a complete Singleton which also follows the extendable pattern.  Don&#8217;t worry if you don&#8217;t get all of it on the first pass, I will break it down step by step later.</p>


<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package com.<span style="color: #006600;">vivisectingmedia</span>.<span style="color: #006600;">example</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> User
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">// stores our instance</span>
		<span style="color: #0066CC;">static</span> <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> __inst:User;
&nbsp;
		<span style="color: #808080; font-style: italic;">// standard public properties</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">name</span>:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> address:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> birthDate:<span style="color: #0066CC;">Date</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">// accessor for the instance</span>
		<span style="color: #0066CC;">static</span> <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> instance<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:User
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span>__inst<span style="color: #66cc66;">&#41;</span> __inst = <span style="color: #000000; font-weight: bold;">new</span> User<span style="color: #66cc66;">&#40;</span>generateLock<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> __inst;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// accessor for our SingletonLock</span>
		<span style="color: #0066CC;">static</span> protected <span style="color: #000000; font-weight: bold;">function</span> generateLock<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:SingletonLock
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> SingletonLock;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// our constructor, must be public in AS3</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> User<span style="color: #66cc66;">&#40;</span>lock:<span style="color: #000000; font-weight: bold;">Class</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span>lock is SingletonLock<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Invalid use of singleton.&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// add public methods and such here</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> age<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">int</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// calculate age and return it</span>
			...
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		...
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> SingletonLock
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> SingletonLock<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>


<p>Let&#8217;s break this down step by step.  First of all lets look at the static property __inst and the static method instance().  These two parts are the most important aspect of the Singleton pattern.  So what are we doing in instance()?  The first thing to notice is that the instance() method is both static and a getter with no setter which makes it read-only.  We would use it like this:</p>


<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// set the user's info and call the age() method</span>
User.<span style="color: #006600;">instance</span>.<span style="color: #0066CC;">name</span> = <span style="color: #ff0000;">&quot;Fred&quot;</span>;
User.<span style="color: #006600;">instance</span>.<span style="color: #006600;">address</span> = <span style="color: #ff0000;">&quot;123 Mission St.&quot;</span>
User.<span style="color: #006600;">instance</span>.<span style="color: #006600;">birthData</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Date</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1978</span>, <span style="color: #cc66cc;">5</span>, <span style="color: #cc66cc;">14</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> age:<span style="color: #0066CC;">int</span> = User.<span style="color: #006600;">instance</span>.<span style="color: #006600;">age</span>;</pre></div></div>


<p>What happens is we call the instance() method through the class reference of User, the instance() method then looks and sees if the __inst property is currently storing an instance of User, if not then we create a new instance of User, save it in __inst and then return the reference.  This means the the second time we call instance on the User class, we have already created the instance of User and we just hand back the reference. That&#8217;s the magic of the Singleton right there, we now only have one instance of the class that is created exactly once and is created at the time you need it.  You don&#8217;t need to worry about creating it in onCreationComplete() or in the Document class.  The first time you access it the item creates itself.  Pretty nifty, eh?</p>

<p>So what is all the other crap that is in this class?  The rest of the code is both designed to help enforce Singleton usage in ActionScript and allow extending of the Singleton Class.</p>

<p><strong>Protecting Singletons in ActionScript</strong><br/>
One of the current limitations of AS3 is that constructors have to be public.  In languages like Java they can be marked protected but not in AS3.  This is being resolved in the ECMA 4 standard so when the new version of AS comes out we will not longer have to put all this enforcement code in.</p>

<p>Because constructors are always public there is nothing stopping a developer from ignoring the static instance getter and just creating an instance of the User class locally.  This is a bad thing because we now have multiple instances of the object and this defeats the Singleton pattern.  To help enforce this we use the SingletonLock as a special key that prevents the constructor from being used EXCEPT in instance() call and/or the extension process (more on extending later). Let&#8217;s look at the constructor again:</p>


<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// our constructor, must be public in AS3</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> User<span style="color: #66cc66;">&#40;</span>lock:<span style="color: #000000; font-weight: bold;">Class</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span>lock is SingletonLock<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;invalid use of singleton.&quot;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>


<p>You will notice that the constructor expects one argument of type Class and then checks to make sure the passed in Class is of type SingletonLock, if not we throw an Error.  SingletonLock is an internal class that is ONLY available to the User Class.  We expose this class through a method called generateLock() which hands back the Class reference to the internal Class.  The instance() method uses generateLock() to create the lock and pass it on to the constructor.  By setting this structure up we have created an enforcement policy that only allows Classes with access to generateLock() to create an instance of the class.  Take a moment to go back and walk through the entire User Class to make sure you understand this creation flow.</p>

<p>Its seems like overkill, but this kind of code is very important when working with open source projects or large teams.  It forces developers to follow the pattern and prevents improper use of a Singleton based class.  As I mentioned earlier this is only because AS3 does not allow protected or private constructors.  In the future we can throw all this enforcement code away, but for now we should use it.</p>

<p><strong>Extending A Singleton</strong><br/>
Now that we have the Singleton how can we extended it and why would we ever want to?  Very rarely would you want to extend a Singleton but it does come up.  For me, the number one reason I want to extend Singletons is for automated unit testing.  </p>

<p>Here is the situation: I have a series of unit tests that I want to run and each of the tests need to assume a clean instance of the Singleton exists.  The challenge is that the tests all run in the same instance of the application (FlexUnit) and therefor the Singleton is created on the first test and the second test is now manipulating the previously created Singleton.  Very bad for a testing environment.</p>

<p>One solution is that we expose a reset() method on the Singleton that enables the unit test to reset the Singleton instance.  This is a bad approach because we really don&#8217;t want to expose this public method in the live version because we want to prevent users for resetting the Singleton, this reset() method is only for testing purposes.</p>

<p>The ideal solution is to extend the Singleton in the Test Suite and add the reset() method which is called on tearDown().  The test would use the extended instance so we are now able to verify the core Singleton functionality and still get reset() without exposing it for the deployed version.  Here is how we would create a TestUser that extends our User example:</p>


<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package com.<span style="color: #006600;">vivisectingmedia</span>.<span style="color: #006600;">example</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TestUser <span style="color: #0066CC;">extends</span> User
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">// stores our instance</span>
		<span style="color: #0066CC;">static</span> <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> __inst:TestUser;
&nbsp;
		<span style="color: #808080; font-style: italic;">// accessor for the instance</span>
		<span style="color: #0066CC;">static</span> <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> instance<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:TestUser
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span>__inst<span style="color: #66cc66;">&#41;</span> __inst = <span style="color: #000000; font-weight: bold;">new</span> TestUser<span style="color: #66cc66;">&#40;</span>generateLock<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> __inst;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">static</span> <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> reset<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			__inst = <span style="color: #000000; font-weight: bold;">new</span> TestUser<span style="color: #66cc66;">&#40;</span>generateLock<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// our constructor, must be public in AS3</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> TestUser<span style="color: #66cc66;">&#40;</span>lock:<span style="color: #000000; font-weight: bold;">Class</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span>lock<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>


<p>What changed?  First we still define the static private instance but we type it as TestUser.  The value has to be private because the __inst has to be specific to the Class its defined in. Remember that static properties and methods are bound to the Class itself.  Things get a little tricky here so let me see if I can explain it.</p>

<p>We could try and set __inst as protected property on the User class.  When the user calls instance() on TestUser we could create the TestUser instance, store it in the the protected __inst version on User since TestUser is a User.  We could then cast __inst as a TestUser for the return call.</p>


<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// accessor for the instance</span>
<span style="color: #0066CC;">static</span> <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> instance<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:TestUser
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span>__inst<span style="color: #66cc66;">&#41;</span> __inst = <span style="color: #000000; font-weight: bold;">new</span> TestUser<span style="color: #66cc66;">&#40;</span>generateLock<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #b1b100;">return</span> TestUser<span style="color: #66cc66;">&#40;</span>__inst<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>


<p>The problem with this approach is that if you called User.instance first then tried TestUser.instance you will get an error because __inst is holding the User instance and User can not be upcasted to TestUser.  The solution is to keep the __inst separate in each Class to prevent this possible issue.  The added benefit is that you can have a User instance and TestUser instance living side by side which is awesome if you are extending the Singleton for other purposes.</p>

<p>The next thing we did is redefined instance().  Notice how I did not have to use override?  Static methods are bound only to the Class and not any children that extend the Class.  We have to redefine it to expose it on TestUser.  Also, notice that the code is creating a TestUser and passing in the parents protected generateLock() call.  Make sure to look at how we changed the constructor.  We still expect the lock and we pass the generated lock on the to the super call.  </p>

<p>This is exactly why we use a generateLock() method and pass a Class reference around. Up to this point a lot of developers (myself included) used the following style for defining the constructor:</p>


<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// accessor for the instance</span>
<span style="color: #0066CC;">static</span> <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> instance<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:User
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span>__inst<span style="color: #66cc66;">&#41;</span> __inst = <span style="color: #000000; font-weight: bold;">new</span> User<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> SingletonLock<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #b1b100;">return</span> __inst;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// our constructor, must be public in AS3</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> User<span style="color: #66cc66;">&#40;</span>lock:SingletonLock<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	...
<span style="color: #66cc66;">&#125;</span></pre></div></div>


<p>The issue with this structure is that you can not extend User because the constructor signature requires knowledge of the SingletonLock class which is an internal class of User and therefore only User can access it.  By replacing the lock as a Class we can still secure the Singleton by validating the passed lock but we can easily extend it since we are expecting type Class in place of type SingletonLock.  </p>

<p>Now that we have fully extended out Class we can add our reset() method which then overwrites the __inst.  That&#8217;s how you can build and extend singletons in ActionScript.  As always please let me know if you see any errors or have any questions about how this all works.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.vivisectingmedia.com/2008/06/the-extendable-singleton-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>VivMedia Code: the HashTable</title>
		<link>http://blog.vivisectingmedia.com/2008/05/vivmedia-code-the-hashtable/</link>
		<comments>http://blog.vivisectingmedia.com/2008/05/vivmedia-code-the-hashtable/#comments</comments>
		<pubDate>Thu, 29 May 2008 19:00:47 +0000</pubDate>
		<dc:creator>James Polanco</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flash Player]]></category>
		<category><![CDATA[Flex Development]]></category>
		<category><![CDATA[Rich Internet Applications]]></category>
		<category><![CDATA[documentation]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[tools]]></category>
		<category><![CDATA[vivisecting media code depot]]></category>

		<guid isPermaLink="false">http://blog.vivisectingmedia.com/2008/05/vivmedia-code-the-hashtable/</guid>
		<description><![CDATA[One of my favorite, and until recently, most used classes in Flex was the ArrayCollection.  The ArrayCollection is a powerful data-structure that acts similarly to an Array but provides an extended API that provide shortcuts to data access. A few of the more used accessors are contains(), which tells you if the item is [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://blog.vivisectingmedia.com/wp-content/uploads/2007/10/rune_flash.gif" alt="rune_flash.gif" align="left" border="0" hspace="10" vspace="5" />One of my favorite, and until recently, most used classes in Flex was the ArrayCollection.  The ArrayCollection is a powerful data-structure that acts similarly to an Array but provides an extended API that provide shortcuts to data access. A few of the more used accessors are contains(), which tells you if the item is in the ArrayCollection and getItemIndex() which tells you where the item is in the ArrayCollection.</p>

<p>The ArrayCollection is much more then this too.  It also has the ability to tie directly into FlexData services and then dynamically page the data into the ArrayCollection instance as data is requested.  The ArrayCollection is also the complex data structure that is generated by the HTTPService (when parse to Object is set) if you have more then one child node in an XML structure.  If you haven&#8217;t used it yet, the ArrayCollection is a very powerful data-structure.</p>

<p>Of course, with this robustness comes limitations.  First off, its Flex only.  This means that if you are used to Flex development but find yourself on an AS3 only project, you can&#8217;t use the ArrayCollection.  Another big one is performance.  As you begin filling the ArrayCollection up, queries into the data structure start to slow down, and I mean significantly slow down.  If you have hundreds of objects in the ArrayCollection, the performance for lookup and access are way slower then just looping over an array and finding it the old fashioned way.  A more minor issue is when trying to manipulate data dynamically you start having to write some pretty ugly code such as to prevent null reference errors:</p>


<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// remove the item if it exists</span>
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>myCollection.<span style="color: #006600;">contains</span><span style="color: #66cc66;">&#40;</span>objectNeeded<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	myCollection.<span style="color: #006600;">removedItemAt</span><span style="color: #66cc66;">&#40;</span>myCollection.<span style="color: #006600;">getItemIndex</span><span style="color: #66cc66;">&#40;</span>objectNeeded<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>


<p>Its not that bad, but as you get more and more complex accessing structures you start longing for the simplicity of an Array.  This is where the <a href="http://code.google.com/p/vivisectingmedia-as3/" title="Vivisecting Media Code Library depot">HashTable came into the picture</a>.  I wanted something that performs like an Array, yet has the API data accessors that can simplify development. Yet, there was one more thing that I wanted, the ability to map key&#8217;s to values like in an object or Dictionary but still maintain Array like access.</p>

<p>From this need came the HashTable.  In the past I had built multiple solutions similar to the HashTable but they typically where one-offs that lived in the code the functionality was needed. I never got around to formalizing it into a proper data-structure.  I finally got the chance and built it from the ground up for my Photoslider app.  So, let&#8217;s talk about the HashTable&#8230;</p>

<p><span id="more-117"></span></p>

<p><strong>Using The HashTable</strong><br/>
The HashTable is an ActionScript 3.0 data structure that is not dependent on any of the Flex SDK, this means you can use it freely in both Flash and Flex projects.  The HashTable, simply put, is two Arrays which are wrapped in logic to provide a simplified data access API for development.  Its not complex, but it can cut out a ton of code that you would typically write over and over.</p>

<p>The main thing to know about the HashTable is that everything revolves around a key/value pair.  The key can be anything, from a number to a complex object.  When you add an item to the HashTable you define this pairing:</p>


<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// create a key (person) and item (friends)</span>
<span style="color: #000000; font-weight: bold;">var</span> person:<span style="color: #0066CC;">Object</span> = <span style="color: #66cc66;">&#123;</span><span style="color: #0066CC;">name</span>: <span style="color: #ff0000;">&quot;James&quot;</span><span style="color: #66cc66;">&#125;</span>;
<span style="color: #000000; font-weight: bold;">var</span> friends:<span style="color: #0066CC;">Array</span> = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#123;</span><span style="color: #0066CC;">name</span>: <span style="color: #ff0000;">&quot;Eno&quot;</span><span style="color: #66cc66;">&#125;</span>, <span style="color: #66cc66;">&#123;</span><span style="color: #0066CC;">name</span>: <span style="color: #ff0000;">&quot;Aaron&quot;</span><span style="color: #66cc66;">&#125;</span>, <span style="color: #66cc66;">&#123;</span><span style="color: #0066CC;">name</span>: <span style="color: #ff0000;">&quot;Doug&quot;</span><span style="color: #66cc66;">&#125;</span>, <span style="color: #66cc66;">&#123;</span><span style="color: #0066CC;">name</span>: <span style="color: #ff0000;">&quot;Dave&quot;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#93;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// create the table</span>
<span style="color: #000000; font-weight: bold;">var</span> hash:HashTable = <span style="color: #000000; font-weight: bold;">new</span> HashTable<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
hash.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span>person, friends<span style="color: #66cc66;">&#41;</span>;
&nbsp;
...
&nbsp;
<span style="color: #808080; font-style: italic;">// get my friends</span>
<span style="color: #000000; font-weight: bold;">var</span> myFriends:<span style="color: #0066CC;">Array</span> = hash.<span style="color: #006600;">getItem</span><span style="color: #66cc66;">&#40;</span>person<span style="color: #66cc66;">&#41;</span> as <span style="color: #0066CC;">Array</span>;</pre></div></div>


<p>As you can see from the example, we can use an Object as a key and then bind another complex object to the key.  Later on we can retrieve the item by simply requesting the item by providing the key.  Not only can we access via the key, but we can also get all the keys and then retrieve their data.  For example, we now have multiple people in our HashTable and we want to get all their friends:</p>


<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// get all the people and loop on them</span>
<span style="color: #000000; font-weight: bold;">var</span> people:<span style="color: #0066CC;">Array</span> = hash.<span style="color: #006600;">getAllKeys</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> len:<span style="color: #0066CC;">int</span> = people.<span style="color: #0066CC;">length</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i:uint = <span style="color: #cc66cc;">0</span>; i <span style="color: #66cc66;">&lt;</span> len; i++<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> friends:<span style="color: #0066CC;">Array</span> = hash.<span style="color: #006600;">getItem</span><span style="color: #66cc66;">&#40;</span>people<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> as <span style="color: #0066CC;">Array</span>;
	...
<span style="color: #66cc66;">&#125;</span></pre></div></div>


<p>Not only can you get all the keys but you can also get all the items by using getAllItems().  There are also other accessors that can help you along the way.  Such as:</p>


<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// how many key/items are in the HashTable</span>
<span style="color: #000000; font-weight: bold;">var</span> numberOfItems:<span style="color: #0066CC;">int</span> = hash.<span style="color: #0066CC;">length</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// do we have any items</span>
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span>hash.<span style="color: #006600;">isEmpty</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">// do something</span>
	...
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// is the key or the item in the table?</span>
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>hash.<span style="color: #006600;">containsKey</span><span style="color: #66cc66;">&#40;</span>person<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">||</span> hash.<span style="color: #006600;">containsItem</span><span style="color: #66cc66;">&#40;</span>friends<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">// do something</span>
	...
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// we can also remove key pairs</span>
hash.<span style="color: #006600;">remove</span><span style="color: #66cc66;">&#40;</span>person<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// or clear the entire table</span>
hash.<span style="color: #006600;">removeAll</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>


<p><strong>Things to Consider When Using the HashTable</strong><br/>
If you read my <a href="http://blog.vivisectingmedia.com/2008/05/vivmedia-code-the-eventbroker/" title="Event Broker Tutorial">EventBroker post</a> or my <a href="http://blog.vivisectingmedia.com/2008/04/the-flex-code-behind-pattern/" title="Flex Code Behind Pattern">Code Behind Pattern post</a> then you know I like to point out any possible gotcha&#8217;s when using code.  I feel this is very important because if the issues are raised in the beginning it makes the life of the developer a hell of a lot easier.  Here are a few nuggets to consider when using the HashTable:</p>

<ul>
<li>The HashTable uses an Array under the hood so this means items are strong referenced.  Make sure to remove the items from the HashTable before you decide you no longer need the items in you application and want the Garbage Collection to clean them up.  </li>
<li>When using the getAllKeys() or getAllItems() you are being handed back a clone of the internal Array.  If you want to change the value in the HashTable you will need to overwrite them by updating the HashTable.  <strong>Note</strong>: The current version (0.01) of the Framework has a brute force clone in the HashTable.  I was a little for loop happy and this has been fixed locally and will be available in the SVN soon.  Version 0.02 of the framework will have this fix.  Its nothing to worry about, I just prefer more elegant code and wanted to note this for the few of you that will poor over my code (Aaron, Eno and Dave!)</li>
<li>You can have the same object multiple times in the HashTable if it is an item, but only one instance as a key.  This means that if you have the object &#8220;person&#8221; as the key to item &#8220;friends&#8221;, and then try to addItem(person, friendsTwo) then &#8220;friends&#8221; will be replaced with &#8220;friendsTwo&#8221; within the table.  This is silent and allowed.  Key&#8217;s must be unique otherwise you overwrite.</li>
</ul>

<p>That should be all&#8230; if you find anything, want more examples or event want new features let me know!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.vivisectingmedia.com/2008/05/vivmedia-code-the-hashtable/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>VivMedia Code: The EventBroker</title>
		<link>http://blog.vivisectingmedia.com/2008/05/vivmedia-code-the-eventbroker/</link>
		<comments>http://blog.vivisectingmedia.com/2008/05/vivmedia-code-the-eventbroker/#comments</comments>
		<pubDate>Mon, 26 May 2008 20:04:08 +0000</pubDate>
		<dc:creator>James Polanco</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flex Development]]></category>
		<category><![CDATA[Rich Internet Applications]]></category>
		<category><![CDATA[documentation]]></category>
		<category><![CDATA[event broker]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[tools]]></category>
		<category><![CDATA[vivisecting media code depot]]></category>

		<guid isPermaLink="false">http://blog.vivisectingmedia.com/2008/05/vivmedia-code-the-eventbroker/</guid>
		<description><![CDATA[The EventBroker, in the Vivisecting Media Library, is an Flash Event routing utility that can be used in any ActionScript 3.0 or a Flex Project.  The code was developed to be SDK independent so that it does not rely on any Flex classes.  The EventBroker is designed to solve the challenge of passing [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://blog.vivisectingmedia.com/wp-content/uploads/2007/10/rune_flash.gif" alt="rune_flash.gif" align="left" border="0" hspace="10" vspace="5" />The EventBroker, <a href="http://code.google.com/p/vivisectingmedia-as3/" title="Vivisecting Media Code Library depot">in the Vivisecting Media Library</a>, is an Flash Event routing utility that can be used in any ActionScript 3.0 or a Flex Project.  The code was developed to be SDK independent so that it does not rely on any Flex classes.  The EventBroker is designed to solve the challenge of passing Events to objects that may not be aware of the dispatching item or are not within the parent/child hierarchy of the Event bubble system.  In this post I will discuss the Flash Event Model, what limitations are part of the Event Model, how the EventBroker resolves these issues and considerations when using the EventBroker.</p>

<p><span id="more-116"></span></p>

<p><strong>Events In the Flash Player</strong><br/>
Before I give some examples for when to use the EventBroker, let me take a minute to talk about the Event model within the Flash player and some of the possible limitations that can occur within this model.  The Flash Event Model is based around the classic broadcaster/listener pattern.  A broadcaster is responsible for dispatching an Event object when a certain action occurs.  A basic example is a Flex button and the button&#8217;s click Event.  When the user clicks on the button in a running Flex application the button dispatches a click Event.  This Event is designed to inform other objects that the click action has occurred.</p>

<p>For an item to receive the click Event notification the listening object registers to the Event via the addEventListener() method which is a public method on the button.  When the object that wants to listen to the click event, the object calls addEventListener(), passes in the type of Event (ex: MouseEvent.CLICK) and what method to call on the listener when the Event is dispatched (ex: handleClickEvent()).  </p>

<p>Going back to our example, let&#8217;s say our button is the play button for a video player and we want the video object to &#8220;listen&#8221; for the click Event.  During setup, the video object would register to the play button using addEventListener(). When the user clicks the play button the button looks up all the items that have registered for the click Event and then calls the methods that were provided by the addEventListener() call.</p>


<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// setup code for our video object</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> videoInit<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	playButton.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, handleClickEvent<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
...</pre></div></div>


<p>The issue with the above example is that the Flash Event model assumes we know about or have access to the Play Button.  In 7-out-of-10 cases this is true, but there are situations where we don&#8217;t know where the Event broadcasting object is.  The Flash Event model does provide another solution called Event Bubbling.  When an object dispatches an Event and the Event is marked to Bubble then the Event will be passed up the parent/child hierarchy until it is either captured or reaches the top of the stack. Let&#8217;s say we have a UI where the Play Button is a child of an HBox.  If the Video element is outside of the HBox but is within the parent wrapper of the HBox then the video object can register to the HBox for the click Event if the event is set to bubble.  When the Click event is dispatched it will be passed to the parent and the parent will then dispatch the event which our video element would receive.</p>


<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mycustomui:VideoWidget</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;my_video_widget&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:HBox<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:Button</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;playButton&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mx:HBox<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>


<p><strong>Challenges with the Flash Event Model</strong><br/>
Unfortunately, the click event does not bubble by default and this puts the developer into a conundrum. Does the developer need to add a router method to the parent class that knows of the existence of the play button and therefore can route the play button&#8217;s events?  Does the developer expose the play button and allow the Video Widget to know where it exists and therefore register to it?  In most cases, exposing the play Button is not a bad thing and is the desired solution but there are exceptions.</p>

<p>What happens if the play Button is nested deep with in multiple components?  What happens if the play Button is within a Module that may not be loaded at the time of the Video Widget initialization?  What if you have <em>n</em> number of modules that all need to know when the video starts playing?  What happens if the button is dynamically created and the location is not known until creation and the listener is in a different module or location that may or may not know of the generated button?  This is were things start getting really interesting and why I created the Event Broker.</p>

<p>A great solution, one that is used in Cairngorm, is to create a controller that is designed to route events to the proper listener.  This solution is called the Observer pattern, where the controller &#8220;observers&#8221; a dispatcher and then routes the event to the proper location.  This is a great pattern but the last examples I saw in Cairngorm required the developer to keep adding events to the controller as they are required. <em>Note:</em> Its been a while since I have worked with Cairngorm so this process may be automated now, it if is let me know. The EventBroker takes the Observer pattern and moves further into a more flexible solution by automating how objects can register and dispatch events.</p>

<p><strong>What is the Event Broker</strong><br/>
The EventBroker is a Singleton Facade that enables any object located in the application to register to any other object&#8217;s events that are dispatched through the EventBroker.  The EventBroker is a completely duel opt-in system which means that both dispatcher and listener must use the EventBroker as the Event routing system for it to work.  Also, because the EventBroker is a singleton, you do not instantiate the EventBroker.  You just call the static methods subscribe(), broadcast() and unsubscribe() directly off the EventBroker class.</p>

<p>Using our play Button example, let&#8217;s walk through how to enable the EventBroker:</p>


<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// the playButton's parent init() located in</span>
<span style="color: #808080; font-style: italic;">// one part of the application</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> videoControlInit<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	playButton.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, handleClickEvent<span style="color: #66cc66;">&#41;</span>;
	...
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// used to re-dispatch the click event through the EventBroker</span>
protected <span style="color: #000000; font-weight: bold;">function</span> handleClickEvent<span style="color: #66cc66;">&#40;</span>event:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">// re-casting the event so that it is specific to the action</span>
	<span style="color: #000000; font-weight: bold;">var</span> playEvent:Event = <span style="color: #000000; font-weight: bold;">new</span> Event<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;PLAY&quot;</span><span style="color: #66cc66;">&#41;</span>;
	EventBroker.<span style="color: #006600;">broadcast</span><span style="color: #66cc66;">&#40;</span>playEvent<span style="color: #66cc66;">&#41;</span>;
&nbsp;
	doSomethingElse<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
...
&nbsp;
<span style="color: #808080; font-style: italic;">// this code is in another component/module that is completely unaware</span>
<span style="color: #808080; font-style: italic;">// of the playButton</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> widgetInit<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	EventBroker.<span style="color: #006600;">subscribe</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;PLAY&quot;</span>, handlePlayEvent<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
protected <span style="color: #000000; font-weight: bold;">function</span> handlePlayEvent<span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">// do something</span>
	...
<span style="color: #66cc66;">&#125;</span></pre></div></div>


<p>For our example, we first need to register to the play button&#8217;s click Event to call the handleClickEvent() method because this is a built-in event within the button type.  If we created a custom component or object we could have the component cut out the middle man and directly jump to the EventBroker, but this example shows how to handle pre-existing events.</p>

<p>In the handleClickEvent() we re-cast the event to a new &#8220;PLAY&#8221; type.  We want to do this because the user clicking on the playButton is really calling an action.  We could, in theory, re-dispatch the click event to the EventBroker but the listeners would need to know that when a click event is broadcasted the code needs to look at the currentTarget and determine if it is set to &#8220;playButton&#8221; which would call the play action defined in the listener.  Its just easier to define a new Event Type the represents the global action and use this as the Event.  To broadcast the event we just call broadcast, very similar to the EventDispatcher&#8217;s dispatchEvent() call.</p>

<p>Now, in the Widget&#8217;s init() code we call the EventBroker&#8217;s subscribe() method and pass in the event type we want to listen to and what method to call.  The method provided should always take one argument with the type of Event.</p>

<p>So in our example, this is how the EventBroker works.  When the wigetInit() is called it subscribes to the &#8220;PLAY&#8221; and registers the handlePlayEvent method.  The EventBroker stores the type and method closure in a lookup table.  When the playButton dispatches the click event the handle method calls EventBroker.broadcast() passing the &#8220;PLAY&#8221;.  The EventBroker looks up any objects that are currently registered to this type and then passes the event to the provided method closure, in effect calling our handlePlayEvent() on the Widget.</p>

<p>By using this process, we have virtually removed all cross-dependancies of the Widget and the playButton and they can be moved around, loaded independently, etc. without breaking our application&#8217;s functionality.</p>

<p>When you no longer want to listen to an Event, call the EventBroker&#8217;s unsubscribe() method passing in the Event type and the method closure.  This then removes the paring from the EventBroker&#8217;s table and any further call to broadcast() will be ignored for the unsubscribed object.</p>

<p><strong>Things to consider</strong><br/>
The EventBroker is a very helpful tool but should be used sparingly due to some potentially challenging issues.  First off, when an Event is broadcasted all subscribers are called. This doesn&#8217;t sound harmful unless you consider the following use case.  Let&#8217;s say that you have four widgets.  Each Widget broadcasts a &#8220;DATA<em>CHANGE&#8221; Event when the user changes something within the Widget.  All the Widgets subscribe to the &#8220;DATA</em>CHANGE&#8221; event so that they can update their content based on the user&#8217;s action.  We want all the widgets to be independent and have no direct linking to each other so the EventBroker is used.</p>

<p>Here is the challenge, when Widget Foo dispatches &#8220;DATA<em>CHANGE&#8221;, Widget Foo will also get its own &#8220;DATA</em>CHANGE&#8221; event.  This means that the Widgets has to be self-aware enough to know when it is dispatching an event that it is also will receive the event.  This means that code has to be added to ignore the incoming event for that specific case.  Otherwise you may have some really nasty recursion.</p>

<p>I have looked into having the EventBroker try to ignore self calls but there is not a way for ActionScript to determine the callee of the method is the same instances as the closure in the registered subscribers.  Also, there are cases where you may want the callee to call itself. So, just be aware of this issue.</p>

<p>The other thing to consider is that the EventBroker is NOT using the Flash Event model and there for is considered a synchronous action.  This means that when the broadcast() method is called Flash calls all the subscribed methods before calling the next line after the original broadcast() call.  In our example code this means that all the code in handlePlayEvent() on the Widget will be completed before the doSomethingElse() call is made in the handleClickEvent() for the button.</p>

<p>This is an import difference then the dispatchEvent() call because the Event propagation system in Flash happens after the current synchronous functionality is completed. In 99.9% of cases this is not an issue but there are edge cases that may arise due to this.  So keep this in-mind during the debugging process.</p>

<p>Finally, the EventBroker is strong referenced and therefore you should ALWAYS unsubscribe listeners before you delete them.  If you do not unsubscribe you will have a strong reference and the Garbage Collection will not clear them from memory.  This is very, very important to remember for larger applications that are adding / removing objects dynamically.</p>

<p>So that&#8217;s the EventBroker&#8230; let me know of you like it or see any issues when using it!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.vivisectingmedia.com/2008/05/vivmedia-code-the-eventbroker/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
