<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.3.3" -->
<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/"
	>

<channel>
	<title>{ P I X E L W E L D E R S } &#187; MXML</title>
	<link>http://pixelwelders.com/blog</link>
	<description>Flash + Flex + Game Dev + Grammar?</description>
	<pubDate>Tue, 04 Nov 2008 14:18:05 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.3</generator>
	<language>en</language>
			<item>
		<title>Broadcasting Events from Custom MXML Components - The [Event] Tag</title>
		<link>http://pixelwelders.com/blog/flex/2008/mxml-embed-tag/</link>
		<comments>http://pixelwelders.com/blog/flex/2008/mxml-embed-tag/#comments</comments>
		<pubDate>Thu, 17 Apr 2008 09:05:31 +0000</pubDate>
		<dc:creator>Zack Jordan</dc:creator>
		
		<category><![CDATA[Flex]]></category>

		<category><![CDATA[MXML]]></category>

		<category><![CDATA[events]]></category>

		<category><![CDATA[metatag]]></category>

		<guid isPermaLink="false">http://pixelwelders.com/blog/flex/2008/mxml-embed-tag/</guid>
		<description><![CDATA[Okay, this was hard for me to wrap my brain around, so let me summarize, in case anyone else is stuck on this.  When developing in Flex, all the standard components let you specify a listener when you create them in MXML, like this:

&#60;mx:Button id=&#34;someButton&#34; click=&#34;handleClick( event );&#34;/&#62;

Now when someone clicks on someButton, it [...]]]></description>
			<content:encoded><![CDATA[<p>Okay, this was hard for me to wrap my brain around, so let me summarize, in case anyone else is stuck on this.  When developing in Flex, all the standard components let you specify a listener when you create them in MXML, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;mx:Button</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;someButton&quot;</span> <span style="color: #000066;">click</span>=<span style="color: #ff0000;">&quot;handleClick( event );&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span></pre></div></div>

<p>Now when someone clicks on <code>someButton</code>, it calls the function <code>handleClick()</code>. Although not strictly best practice, it really is a nice little shorthand for adding an event listener to a Flex component.  And since the Adobe components contain this functionality, people are used to it.  If you have any plans of releasing your component into the wild, people will expect it to act like the components they already know and love.</p>
<p>Well, here’s how you do it: with the <code>[Event]</code> metadata tag.</p>
<p>1- First of all, the tag goes before the class declaration because it applies to the entire class.  You can actually have more than one of these tags, for different events that you might broadcast.  In my example, I have two, and they both broadcast <code>ModelEvent</code>s.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript"><span style="color: #808080; font-style: italic;">// create the [Event] tags</span>
<span style="color: #66cc66;">&#91;</span>Event<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">name</span>=<span style="color: #ff0000;">&quot;onLoad&quot;</span>, <span style="color: #0066CC;">type</span>=<span style="color: #ff0000;">&quot;com.pixelwelders.events.ModelEvent&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #66cc66;">&#91;</span>Event<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">name</span>=<span style="color: #ff0000;">&quot;onLoadError&quot;</span>, <span style="color: #0066CC;">type</span>=<span style="color: #ff0000;">&quot;com.pixelwelders.events.ModelEvent&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// declare your class</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DataModel <span style="color: #0066CC;">extends</span> EventDispatcher <span style="color: #0066CC;">implements</span> IModel <span style="color: #66cc66;">&#123;</span> ...</pre></div></div>

<p>2- The <code>type</code> attribute refers to the actual type of event that this class broadcasts, package and all.</p>
<p>3- The <code>name</code> attribute refers to the string you use when you create a new Event of this type.  You have to use an actual literal here; you can’t use a variable like <code>ModelEvent.LOADED</code>.  So if you are dispatching an event like either of these:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript">dispatchEvent<span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> ModelEvent<span style="color: #66cc66;">&#40;</span> ModelEvent.<span style="color: #0066CC;">LOADED</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
dispatchEvent<span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> ModelEvent<span style="color: #66cc66;">&#40;</span> ModelEvent.<span style="color: #006600;">LOAD_ERROR</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>&#8230;you’ll need to check in your Event class to see what the actual value of <code>LOADED</code> or <code>LOAD_ERROR</code> is.  In my ModelEvent class, I have:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript"><span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const <span style="color: #0066CC;">LOADED</span>				: <span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;onLoad&quot;</span>;
<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const LOAD_ERROR			: <span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;onLoadError&quot;</span>;</pre></div></div>

<p>Thus, my complete Event metadata tags are:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript"><span style="color: #66cc66;">&#91;</span>Event<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">name</span>=<span style="color: #ff0000;">&quot;onLoad&quot;</span>, <span style="color: #0066CC;">type</span>=<span style="color: #ff0000;">&quot;com.pixelwelders.events.ModelEvent&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #66cc66;">&#91;</span>Event<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">name</span>=<span style="color: #ff0000;">&quot;onLoadError&quot;</span>, <span style="color: #0066CC;">type</span>=<span style="color: #ff0000;">&quot;com.pixelwelders.events.ModelEvent&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span></pre></div></div>

<p>Of course, that’s only the first part of the equation.  The second part is listening for the event, which thanks to our <code>[Embed]</code> tags, is now a piece of figurative cake.  Here’s my Flex tag:</p>

<div class="wp_syntax"><div class="code"><pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;model:DataModel</span>
		<span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;dataModel&quot;</span>
		<span style="color: #000066;">xmlURL</span>=<span style="color: #ff0000;">&quot;fileName.xml&quot;</span> 
		<span style="color: #000066;">onLoad</span>=<span style="color: #ff0000;">&quot;handleModelLoaded( event );&quot;</span>
		<span style="color: #000066;">onLoadError</span>=”handleModelError<span style="color: #66cc66;">&#40;</span> event <span style="color: #66cc66;">&#41;</span>;”<span style="font-weight: bold; color: black;">/&gt;</span></span></pre></div></div>

<p>That <code>onLoad</code> and <code>onLoadError</code> attributes have the same value as your name attributes that you specified in step 3 above.  Now put your two handlers in your <code>Script</code> tag, and they will be called any time those events are fired.  Voilà.</p>
]]></content:encoded>
			<wfw:commentRss>http://pixelwelders.com/blog/flex/2008/mxml-embed-tag/feed/</wfw:commentRss>
		</item>
		<item>
		<title>MXML Singletons in Flex 3</title>
		<link>http://pixelwelders.com/blog/flex/2008/mxml-singletons-in-flex-3/</link>
		<comments>http://pixelwelders.com/blog/flex/2008/mxml-singletons-in-flex-3/#comments</comments>
		<pubDate>Thu, 10 Apr 2008 09:04:27 +0000</pubDate>
		<dc:creator>Zack Jordan</dc:creator>
		
		<category><![CDATA[ActionScript 3.0]]></category>

		<category><![CDATA[Design Patterns]]></category>

		<category><![CDATA[Flex]]></category>

		<category><![CDATA[MXML]]></category>

		<guid isPermaLink="false">http://pixelwelders.com/blog/flex/2008/mxml-singletons-in-flex-3/</guid>
		<description><![CDATA[Okay, so I've been putting together a little image-viewer component in Flex, and I wanted it to load one data model that all other components would share.  Sounds like a job for a Singleton, right?  However, I also wanted to instantiate said model from MXML, which adds some kinks.  In fact, I've <a href="http://www.onflex.org/ted/2007/01/singleton-in-mxml.php">heard it said</a> that it's not possible at all.  And after some trial and error, I found that apparently it is possible- you just have to hack around a little bit.  ]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re not clear on what a Singleton is, try glancing through my brief yet poignant article here: <a href="http://pixelwelders.com/blog/best-practices/2008/art-and-beauty-of-singletons-in-as3/" >The Art and Beauty of Singletons</a>.</p>
<h3>Creating a Singleton through MXML</h3>
<p>	Okay, so I&#8217;ve been putting together a little image-viewer component in Flex, and I wanted it to load one data model that all other components would share.  Sounds like a job for a Singleton, right?  However, I also wanted to instantiate said model from MXML, which adds some kinks.  In fact, I&#8217;ve <a href="http://www.onflex.org/ted/2007/01/singleton-in-mxml.php" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.onflex.org/ted/2007/01/singleton-in-mxml.php');">heard it said</a> that it&#8217;s not possible at all.  And after some trial and error, I found that apparently it is possible- you just have to hack around a little bit.  </p>
<p>If you instantiate your class (or &#8220;component&#8221;) through MXML, Flex will automatically call its constructor.  Typically, this is a bad thing because you don&#8217;t call constructors directly in Singletons- instead of calling a method that returns a <em>new</em> object (i.e. a constructor), you call a method that returns a reference to an already-created object (<code>getInstance()</code> or <code>instance()</code>).  Flex won&#8217;t do that for you.  However, this is not a deal-breaker.  To make that class a Singleton, you just need to make sure that the constructor is only called once.</p>
<p>So, set up your Singleton like normal:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript"><span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> _instance: DataModel = <span style="color: #000000; font-weight: bold;">null</span>;
<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</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>: DataModel
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">return</span> _instance;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Then, in the constructor of said Singleton, add the following:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript"><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">!</span>_instance <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	_instance = <span style="color: #0066CC;">this</span>;
<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</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;DataModel is a Singleton: only one instance allowed.&quot;</span> <span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>All I&#8217;ve done here is move the creation of the Singleton from the <code>get instance()</code> method to the constructor.  And if the constructor is called more than once, it throws an error.  Now you can instantiate the class through MXML, just like any other component.</p>

<div class="wp_syntax"><div class="code"><pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;model:DataModel</span>
		<span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;dataModel&quot;</span>
		<span style="color: #000066;">xmlURL</span>=<span style="color: #ff0000;">&quot;fileName.xml&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span></pre></div></div>

<p>Meanwhile in Actionscriptland, you can refer to this instance of your class just like it was a normal Singleton:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript">	<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span> DataModel.<span style="color: #006600;">instance</span> <span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// traces &quot;[object DataModel]&quot;</span></pre></div></div>

<p>	Plus, if you try to create another instance, be it through MXML or through ActionScript, you&#8217;ll get a good ol&#8217; runtime error.  You’ll have to wait until runtime to see it, but that’s certainly better than nothing, right?</p>
]]></content:encoded>
			<wfw:commentRss>http://pixelwelders.com/blog/flex/2008/mxml-singletons-in-flex-3/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
