<?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>Jacob Wright - Flex, AIR, PHP, etc. &#187; Flash</title>
	<atom:link href="http://jacwright.com/blog/category/flash/feed/" rel="self" type="application/rss+xml" />
	<link>http://jacwright.com/blog</link>
	<description>Flex, AIR, PHP, etc.</description>
	<lastBuildDate>Thu, 29 Jul 2010 15:05:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>AS3 Property Observer Implementation</title>
		<link>http://jacwright.com/blog/373/as3-property-observer-implementation/</link>
		<comments>http://jacwright.com/blog/373/as3-property-observer-implementation/#comments</comments>
		<pubDate>Wed, 17 Feb 2010 14:00:05 +0000</pubDate>
		<dc:creator>Jacob Wright</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Reflex]]></category>

		<guid isPermaLink="false">http://jacwright.com/blog/?p=373</guid>
		<description><![CDATA[Update: The final implementation. Monday I wrote about how I would use the observer pattern to alter the functionality of getters/setters and respond to changes without using Flash's events. Tyler and I brainstormed more about the solution and as I knew would happen we came up with a better implementation. Changes One thing I didn't [...]]]></description>
			<content:encoded><![CDATA[<p><b>Update:</b> The <a href="http://github.com/jacwright/flight-framework/blob/master/project/src/flight/observers/Observe.as">final implementation</a>.</p>
<p>Monday I wrote about how I would use the observer pattern to alter the functionality of getters/setters and respond to changes without using Flash's events. Tyler and I brainstormed more about the solution and as I knew would happen we came up with a better implementation.</p>
<h3>Changes</h3>
<p>One thing I didn't like about my initial go at it, is that you had to create a class with three methods, and usually you only used one of them. So methods were being called that didn't need to be for the sake of it. We decided to add functions on an as-needed basis. I named the different types checks, hooks, and observers.</p>
<ul>
<li><strong>Check</strong>, a method which checks the validity of the data being added. If the check determines the data is invalid it returns a boolean of false. Otherwise it returns true.</li>
<li><strong>Hook</strong>, a method which can modify the data of a setter before it gets set. This can allow formatting or other customization.</li>
<li><strong>Observer</strong>, a method which is notified when a property has been changed. Flex uses the PropertyChange event currently.</li>
</ul>
<p>Something Tyler didn't like was the requirement of an interface. He wanted to be able to add the observing functionality from outside an object with a static utility class. Binding would then know if objects used the observer system with additional info in the bindable metadata [Bindable(observable)]. So I created a static utility class called Observe which would allow the logic to be handled separately. Our <a href="http://jacwright.com/blog/167/better-than-flex-binding/">data-binding</a> relies on the metadata to indicate if a given property uses Observe.</p>
<h3>API</h3>
<p>The <a href="http://github.com/jacwright/flight-framework/blob/ioc/project/src/flight/observers/Observe.as"> Observe interface</a> ended up looking like this:</p>
<div class="syntax_hilite">
<div id="actionscript-5">
<div class="actionscript"><span style="color: #0066CC;">public</span> Observe<br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> addCheck<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">target</span>:<span style="color: #0066CC;">Object</span>, property:<span style="color: #0066CC;">String</span>, observerHost:IEventDispatcher, observer:<span style="color: #000000; font-weight: bold;">Function</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;<br />
&nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> removeCheck<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">target</span>:<span style="color: #0066CC;">Object</span>, property:<span style="color: #0066CC;">String</span>, observer:<span style="color: #000000; font-weight: bold;">Function</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;</p>
<p>&nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> addHook<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">target</span>:<span style="color: #0066CC;">Object</span>, property:<span style="color: #0066CC;">String</span>, observerHost:IEventDispatcher, observer:<span style="color: #000000; font-weight: bold;">Function</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;<br />
&nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> removeHook<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">target</span>:<span style="color: #0066CC;">Object</span>, property:<span style="color: #0066CC;">String</span>, observer:<span style="color: #000000; font-weight: bold;">Function</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;</p>
<p>&nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> addObserver<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">target</span>:<span style="color: #0066CC;">Object</span>, property:<span style="color: #0066CC;">String</span>, observerHost:IEventDispatcher, observer:<span style="color: #000000; font-weight: bold;">Function</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;<br />
&nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> removeObserver<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">target</span>:<span style="color: #0066CC;">Object</span>, property:<span style="color: #0066CC;">String</span>, observer:<span style="color: #000000; font-weight: bold;">Function</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// remove all checks, hooks, and observers from the given target</span><br />
&nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> release<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">target</span>:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// called from within the setter</span><br />
&nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> canChange<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">target</span>:<span style="color: #0066CC;">Object</span>, property:<span style="color: #0066CC;">String</span>, oldValue:*, newValue:*<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Boolean</span>;<br />
&nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> modifyChange<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">target</span>:<span style="color: #0066CC;">Object</span>, property:<span style="color: #0066CC;">String</span>, oldValue:*, newValue:*<span style="color: #66cc66;">&#41;</span>:*<br />
&nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> notifyChange<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">target</span>:<span style="color: #0066CC;">Object</span>, property:<span style="color: #0066CC;">String</span>, oldValue:*, newValue:*<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><br />
<span style="color: #66cc66;">&#125;</span></p>
<p><span style="color: #808080; font-style: italic;">// an example setter which hooks into the observer system</span><br />
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> foo<span style="color: #66cc66;">&#40;</span>value:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> oldValue:<span style="color: #0066CC;">Object</span> = _foo;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>oldValue == value || !Observe.<span style="color: #006600;">canChange</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>, <span style="color: #ff0000;">"foo"</span>, oldValue, value<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; value = Observe.<span style="color: #006600;">modifyChange</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>, <span style="color: #ff0000;">"foo"</span>, oldValue, value<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; _foo = value;<br />
&nbsp; &nbsp; Observe.<span style="color: #006600;">notifyChange</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>, <span style="color: #ff0000;">"foo"</span>, oldValue, value<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p>observerHost is the object the observer method belongs to. Using this allows a reference to be made to the method so that we can be completely weak-referenced and memory friendly. If the observer does not belong to an IEventDispatcher you must hold that method in a variable and pass in null to observerHost. Each check, hook, and observer can have 0-4 parameters looking like:</p>
<div class="syntax_hilite">
<div id="actionscript-6">
<div class="actionscript"><span style="color: #000000; font-weight: bold;">function</span> foo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #000000; font-weight: bold;">function</span> foo<span style="color: #66cc66;">&#40;</span>value:*<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #000000; font-weight: bold;">function</span> foo<span style="color: #66cc66;">&#40;</span>oldValue:*, newValue:*<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #000000; font-weight: bold;">function</span> foo<span style="color: #66cc66;">&#40;</span>propName:<span style="color: #0066CC;">String</span>, oldValue:*, newValue:*<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #000000; font-weight: bold;">function</span> foo<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">target</span>:<span style="color: #0066CC;">Object</span>, propName:<span style="color: #0066CC;">String</span>, oldValue:*, newValue:*<span style="color: #66cc66;">&#41;</span>;</div>
</div>
</div>
<p></p>
<p>Checks should return a boolean, hooks should return a value, observers don't need to return anything.</p>
<h3>Additional functionality</h3>
<p>By passing in "*" as the property name to any of the methods, your check, hook, or observer will be called when any property is changed on the provided object.</p>
<p>If you pass in a class as the target to Observe.addCheck|Hook|Observer then whenever the given property is changed for <strong>any</strong> instance of that class or a subclass thereof you will be notified.</p>
<p>For example, if you call</p>
<div class="syntax_hilite">
<div id="actionscript-7">
<div class="actionscript">Observe.<span style="color: #006600;">addObserver</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">Object</span>, <span style="color: #ff0000;">"*"</span>, <span style="color: #0066CC;">this</span>, myMethod<span style="color: #66cc66;">&#41;</span>;</div>
</div>
</div>
<p>
then <code>myMethod</code> will be called every time any property is changed anywhere (properties that use the system that is). You can respond to any property change in the entire system that uses the property observer system I've created here.</p>
<h3>Modified Flex SDK</h3>
<p>I want to use this new observing system everywhere, but I don't want to have to write out all the setters for it. That would take a lot of time, now and later as I write more code. So I checked out the flex4_beta2 tag from Adobe's open source subversion repository and modified a couple of things. Now when I add [Bindable] to a property it will be observable as well as work with Flex binding. I also added the Flight binding classes so they'll be available in all my Flex projects.</p>
<p>I know some of you will be interested in playing with the SDK and seeing what I changed, so I'm providing a download of the modified SDK as well as the changelog. I do not plan on releasing a new modified SDK with every new Flex SDK release. I may do some for major releases, but most likely I'll just do it for my own use unless there gains some interest. Reflex might have it's own SDK down the road too.</p>
<p>So go ahead, download the SDK, create a new ActionScript Project called Test that uses it, and make the following your application class</p>
<div class="syntax_hilite">
<div id="actionscript-8">
<div class="actionscript">package<br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0066CC;">import</span> flight.<span style="color: #006600;">binding</span>.<span style="color: #006600;">Bind</span>;<br />
&nbsp; &nbsp; <span style="color: #0066CC;">import</span> flight.<span style="color: #006600;">observers</span>.<span style="color: #006600;">Observe</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>Bindable<span style="color: #66cc66;">&#93;</span><br />
&nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Test <span style="color: #0066CC;">extends</span> Sprite<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> test:<span style="color: #0066CC;">String</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> test2:<span style="color: #0066CC;">String</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> test3:<span style="color: #0066CC;">String</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Test<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Bind.<span style="color: #006600;">addBinding</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>, <span style="color: #ff0000;">"test2"</span>, <span style="color: #0066CC;">this</span>, <span style="color: #ff0000;">"test"</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; test = <span style="color: #ff0000;">"Hello World"</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"test2:"</span>, test2<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Observe.<span style="color: #006600;">addHook</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>, <span style="color: #ff0000;">"test3"</span>, <span style="color: #0066CC;">this</span>, addNot<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; test3 = <span style="color: #ff0000;">"I hate Flash"</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"test3:"</span>, test3<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> addNot<span style="color: #66cc66;">&#40;</span>value:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> value + <span style="color: #ff0000;">"...NOT!"</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p>You'll see how hooks can work and how binding works! I haven't tested the speed, but guaranteed this is way faster and better memory than creating a new PropertyChangeEvent object each time a property is changed. And I still have room for optimization I've been thinking about. I know the API will change for the setter code.</p>
<h3>Dilemma, need your opinion</h3>
<p>So I have a dilemma and need your feedback. Checks and Hooks could be really cool, allowing you to change stuff at run-time, inserting code into the setter as it were. However, this could also cause headache, security issues, difficult to diagnose bugs, and other potential issues. With great power comes great responsibility and jazz like that.</p>
<p>Is it worth it? Should I remove the checks and hooks altogether and just leave observers? What are the costs, and do they out-weigh the benefits? Please comment and let me know your thoughts.</p>
<h3>Downloads</h3>
<ul>
<li><b><a href="/blog/resources/flexsdk/observersdk.zip">Observer Flex SDK</a></b></li>
<li><b><a href="/blog/resources/flexsdk/observer.diff">Diff File</a></b></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://jacwright.com/blog/373/as3-property-observer-implementation/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>An Alternative to using PropertyChangeEvent</title>
		<link>http://jacwright.com/blog/367/an-alternative-to-using-propertychangeevent/</link>
		<comments>http://jacwright.com/blog/367/an-alternative-to-using-propertychangeevent/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 16:00:07 +0000</pubDate>
		<dc:creator>Jacob Wright</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Reflex]]></category>

		<guid isPermaLink="false">http://jacwright.com/blog/?p=367</guid>
		<description><![CDATA[Update: A post about a trial implementation, and then the final implementation. I've been trying to figure out the best way to include a styling framework with Reflex as needed, without requiring APIs and including extra classes in the core. Pay-as-you-go. The solution that I finally came up with turns out to be great for [...]]]></description>
			<content:encoded><![CDATA[<p><b>Update:</b> A post about a <a href="http://jacwright.com/blog/373/as3-property-observer-implementation/">trial implementation</a>, and then the <a href="http://github.com/jacwright/flight-framework/blob/master/project/src/flight/observers/Observe.as">final implementation</a>.</p>
<p>I've been trying to figure out the best way to include a styling framework with Reflex as needed, without requiring APIs and including extra classes in the core. Pay-as-you-go. The solution that I finally came up with turns out to be great for not only styling, but effects/transitions and data-binding too. I'll start with data-binding to try and describe the benefit.</p>
<h3>Flex Data-binding</h3>
<p>In Flex we have data-binding. Data-binding is cool. Allows us to do stuff a lot easier. We use it in almost everything we write in Flex.</p>
<p>Unfortunately, data-binding is based on the event dispatching system in Flex. What this means in practice is that every time a value is changed in Flex, a new one-use object is created and then garbage-collected shortly afterwards (the PropertyChangeEvent object). Others have <a href="http://robertpenner.com/flashblog/2009/08/my-critique-of-as3-events-part-1.html">complained</a> about the event system and even offered <a href="http://robertpenner.com/flashblog/2009/09/my-new-as3-event-system-signals.html">alternatives</a>. Partly for API, partly for performance.</p>
<p>Another side-effect of Flex data-binding is that it ties objects into memory. The garbage collector is unable to clean up objects that are bound because of the references to them.</p>
<h3>Weak-reference Data-Binding</h3>
<p>We improved with <a href="http://www.xtyler.com/">Tyler's</a> <a href="http://code.google.com/p/flight-framework/source/browse/#svn/trunk/project/src/flight/binding">weak-reference data-binding</a> included in the Flight framework. However, it is still dependent on the event system of Flex. And object creation and destruction (in the amount that happens whenever a property is changed) can be expensive.</p>
<h3>Property Observers</h3>
<p>The observer pattern is very common. Being a pattern it doesn't define exact implementation, and Flash events in essence follow the observer patten. But wanting 1) better performance with property changes and binding and 2) greater flexibility to extend the functionality of a component without overriding it and 3) better code-reuse in setters, I created a light property observer system to replace event dispatching for property changes. (whew, that was a long sentence.) I also modified Tyler's binding (actually a refactor of it I did the other week) to use this property observer system and fall back on events as a last resort.</p>
<p>Note: this isn't meant to replace the event system, only substitute for property change events.</p>
<p>The system works with two interfaces, <code>IPropertyObservable</code> and <code>IPropertyObserver</code>.</p>
<h4><code>PropertyObserverable</code></h4>
<p><code>IPropertyObservable</code> is the object with the properties. The interface requires two methods:</p>
<ul>
<li><code>addPropertyObserver(observer:IPropertyObserver):void</code></li>
<li><code>removePropertyObserver(observer:IPropertyObserver):void</code></li>
</ul>
<p>Whenever a property is set, all the observers are notified and can do several things.</p>
<h4><code>IPropertyObserver</code></h4>
<p><code>IPropertyObserver</code> responds to property changes by implementing these 3 methods:</p>
<ul>
<li><code>allowPropertyChange(target:Object, name:String, currentValue:*, newValue:*):Boolean</code></li>
<li><code>propertyChanging(target:Object, name:String, currentValue:*, newValue:*):*</code></li>
<li><code>propertyChange(target:Object, name:String, oldValue:*, newValue:*):void</code></li>
</ul>
<p>These methods allow an observer to respectively:</p>
<ul>
<li>stop the property change from happening</li>
<li>alter the value the property will be changed to</li>
<li>respond to a change</li>
</ul>
<h3>How about some code</h3>
<p>Since the functionality of the observables will all be the same, I've implemented it into a class that can either be extended or used under-the-hood just like EventDispatcher. <code>PropertyObservable</code> is listed, undocumented, unpackaged, here for your viewing pleasure:</p>
<div class="syntax_hilite">
<div id="actionscript-11">
<div class="actionscript"><span style="color: #0066CC;">public</span> final <span style="color: #000000; font-weight: bold;">class</span> PropertyObservable <span style="color: #0066CC;">implements</span> IPropertyObservable<br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">target</span>:IPropertyObservable;<br />
&nbsp; &nbsp; <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> observers:<span style="color: #0066CC;">Array</span> = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;</p>
<p>&nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> PropertyObservable<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">target</span>:IPropertyObservable = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">target</span> = <span style="color: #0066CC;">target</span> || <span style="color: #0066CC;">this</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></p>
<p>&nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> addPropertyObserver<span style="color: #66cc66;">&#40;</span>observer:IPropertyObserver<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>observers.<span style="color: #0066CC;">indexOf</span><span style="color: #66cc66;">&#40;</span>observer<span style="color: #66cc66;">&#41;</span> != -<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; observers.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>observer<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></p>
<p>&nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> removePropertyObserver<span style="color: #66cc66;">&#40;</span>observer:IPropertyObserver<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">index</span>:<span style="color: #0066CC;">int</span> = observers.<span style="color: #0066CC;">indexOf</span><span style="color: #66cc66;">&#40;</span>observer<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">index</span> == -<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; observers.<span style="color: #0066CC;">splice</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">index</span>, <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></p>
<p>&nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> changeProperty<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">name</span>:<span style="color: #0066CC;">String</span>, oldValue:*, newValue:*<span style="color: #66cc66;">&#41;</span>:*<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>!allowPropertyChange<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">name</span>, oldValue, newValue<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> oldValue;<br />
&nbsp; &nbsp; &nbsp; &nbsp; newValue = propertyChanging<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">name</span>, oldValue, newValue<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>!allowPropertyChange<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">name</span>, oldValue, newValue<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> oldValue;<br />
&nbsp; &nbsp; &nbsp; &nbsp; propertyChange<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">name</span>, oldValue, newValue<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> newValue;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></p>
<p>&nbsp; &nbsp; <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> allowPropertyChange<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">name</span>:<span style="color: #0066CC;">String</span>, currentValue:*, newValue:*<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Boolean</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span> each <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> observer:IPropertyObserver <span style="color: #b1b100;">in</span> observers<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>!observer.<span style="color: #006600;">allowPropertyChange</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">target</span>, <span style="color: #0066CC;">name</span>, currentValue, newValue<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">true</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></p>
<p>&nbsp; &nbsp; <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> propertyChanging<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">name</span>:<span style="color: #0066CC;">String</span>, currentValue:*, newValue:*<span style="color: #66cc66;">&#41;</span>:*<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span> each <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> observer:IPropertyObserver <span style="color: #b1b100;">in</span> observers<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> result:* = observer.<span style="color: #006600;">propertyChanging</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">target</span>, <span style="color: #0066CC;">name</span>, currentValue, newValue<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>result !== <span style="color: #0066CC;">undefined</span><span style="color: #66cc66;">&#41;</span> newValue = result;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> newValue;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></p>
<p>&nbsp; &nbsp; <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> propertyChange<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">name</span>:<span style="color: #0066CC;">String</span>, oldValue:*, newValue:*<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span> each <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> observer:IPropertyObserver <span style="color: #b1b100;">in</span> observers<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; observer.<span style="color: #006600;">propertyChange</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">target</span>, <span style="color: #0066CC;">name</span>, oldValue, newValue<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p><code>IPropertyObserver</code>s are custom and depend on what functionality the provide.</p>
<h3>Great! another way to do the same thing</h3>
<p>You might think this is the same thing except different, but this actually allows some pretty cool code-reuse and other features that event dispatching does not. First, let me show a traditional setter for binding, then a setter that uses the observable system:</p>
<div class="syntax_hilite">
<div id="actionscript-12">
<div class="actionscript"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> foo<span style="color: #66cc66;">&#40;</span>value:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>_foo == value<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span>; <span style="color: #808080; font-style: italic;">// don't want to dispatch a change if it didn't change</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> oldValue:<span style="color: #0066CC;">Number</span> = _foo;<br />
&nbsp; &nbsp; _foo = value;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; dispatchEvent<span style="color: #66cc66;">&#40;</span>PropertyChangeEvent.<span style="color: #006600;">createUpdateEvent</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>, <span style="color: #ff0000;">"foo"</span>, oldValue, value<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span></p>
<p><span style="color: #808080; font-style: italic;">// revisited</span><br />
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> foo<span style="color: #66cc66;">&#40;</span>value:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; _foo = changeProperty<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"foo"</span>, _foo, value<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p>We can also add additional setter logic without overriding the setter, or remove setter logic that was previously added. A few examples of cool stuff you could do with this is:</p>
<ul>
<li>Make a draggable component snap into place on a grid system.</li>
<li>Prevent x/y from changing, locking a component in place, when certain conditions are met.</li>
<li>Use <a href="http://jacwright.com/blog/184/actionscript-3-wildcard-setters/">wildcard setters</a> and turn String values of width and height into pixel values (e.g. 2cm, 1in).</li>
<li>Create transitions, so when x/y is set, instead of setting immediately a tweening engine can update the property over time (e.g. shape.x = 100; shape.y = 200; could tween from the current location to the new one over 1 second)</li>
<li>Create a styling framework that keeps track of whether styling set a property or something else did. If something else did, don't override the value with the one from the stylesheet.</li>
</ul>
<p>And one of my favorite parts: one instance of an observer can be used for every component. I don't need to create a new observer instance for each component. No more objects created each time a property is changed, and not even for each observable. This would be a case in which singletons are a great help.</p>
<p>We'll see whether this ends up in the Reflex code-base or if it is move into the Flight framework library were the data-binding resides. It will probably make most sense to become part of Flight. But I'm quite happy with the general model, and I know Tyler and others will have great ideas on how it can be improved (size, performance, or features).</p>
]]></content:encoded>
			<wfw:commentRss>http://jacwright.com/blog/367/an-alternative-to-using-propertychangeevent/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Displaying Flash generated images in HTML &lt;img/&gt;, realtime</title>
		<link>http://jacwright.com/blog/254/displaying-flash-generated-images-in-html-img-realtime/</link>
		<comments>http://jacwright.com/blog/254/displaying-flash-generated-images-in-html-img-realtime/#comments</comments>
		<pubDate>Sat, 28 Nov 2009 13:00:45 +0000</pubDate>
		<dc:creator>Jacob Wright</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://jacwright.com/blog/?p=254</guid>
		<description><![CDATA[So there may not be an exorbitant amount of use-cases for displaying an image in HTML that was generated real-time in Flash, but I thought's cool, so I'm sharing anyway. It makes the inner-geek in me smile. Short background: I was looking into creating a Flash drawing app that consisted only of the canvas portion, [...]]]></description>
			<content:encoded><![CDATA[<p>So there may not be an exorbitant amount of use-cases for displaying an image in HTML that was generated real-time in Flash, but I thought's cool, so I'm sharing anyway. It makes the inner-geek in me smile.</p>
<p>Short background: I was looking into creating a Flash drawing app that consisted only of the canvas portion, with the rest of the UI in HTML. This is for many reasons including the size of Flex, the native OS components in the browser, speed, and hey, it forces separation of core app logic from the presentation logic, right? ;)</p>
<p>So I wanted a way to display thumbnails of the pages of my drawing. This was all proof of concept and I only have one page and one thumbnail. To do so, I used the <a href="http://en.wikipedia.org/wiki/Data_URI_scheme">URI Data scheme</a> to set the data of a HTML <code>&lt;IMG&gt;</code> element. The only modern browser (if you can call it that) which this doesn't work with is IE6, an acceptable loss for proof-of-concept. Here is how it works in my proof of concept:</p>
<p>See the <strong><a href="http://withincode.com/studio/">Studio proof of concept</a></strong></p>
<p>First, I take a resized bitmap snapshot of my canvas anytime something on it changes, in this example, when it scrolls (I may post about the scrolling in another post, also cool geek stuff). This is done using the BitmapData.copy() method. Actually, I'm using my <a href="http://code.google.com/p/jacwright/source/browse/trunk/flash/jac/src/jac/image/ImageUtils.as">ImageUtils</a> class which ensures it looks good when resized (more about that in <a title="High quality, high performance thumbnails in Flash" href="http://jacwright.com/blog/221/high-quality-high-performance-thumbnails-in-flash/">another post</a>). Then I turn the BitmapData object into a PNG with <a href="http://code.google.com/p/as3corelib/source/browse/trunk/src/com/adobe/images/PNGEncoder.as">AS3CoreLib's PNGEncoder</a>. I now have the bytes for a PNG, so how do I get it to HTML without sending it to the server first? Well, I'll tell you!!</p>
<p>I take the ByteArray that the PNG encoder class gives me and I encode it with Base64Encoder, prepend "data:image/png;base64," to it, then send it out to javascript using ExternalInterface to call a JS function already on the page. That function receives the base 64 encoded string and sets it to the "src" property of the little thumb image down at the bottom of the page. You can see every time you scroll it updates. No biggie right, but if you right-click on it you'll see that it is just an HTML image, right-click on the canvas and see that it is a Flash movie. No server round-trips, 100% Flash/Javascript realtime interaction. Tada!</p>
<p>I did a bit more playing around with it, giving the bitmap rounded corners in Flash before sending it to the browser, tried shadows too, but decided they didn't look great in this interface, so left them out.</p>
<p>Really fun stuff. I hope this is inspiring, even if it isn't useful in your situation. Next time I'll talk about how the native browser scrollbar is scrolling the Flash content, though you may be able to guess easy enough.</p>
]]></content:encoded>
			<wfw:commentRss>http://jacwright.com/blog/254/displaying-flash-generated-images-in-html-img-realtime/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Simple REST server in PHP &#8211; Supports JSON &amp; AMF</title>
		<link>http://jacwright.com/blog/250/simple-rest-server-in-php-supports-json-amf/</link>
		<comments>http://jacwright.com/blog/250/simple-rest-server-in-php-supports-json-amf/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 17:37:29 +0000</pubDate>
		<dc:creator>Jacob Wright</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://jacwright.com/blog/?p=250</guid>
		<description><![CDATA[After building a couple of RESTful services using the Zend Framework, I decided to create a dead simple REST server that allowed me to skip all the features I didn't need as well as a tons of classes that came with Zend Framework MVC. There are still useful features to add (XML support for example), [...]]]></description>
			<content:encoded><![CDATA[<p>After building a couple of RESTful services using the <a href="http://framework.zend.com/">Zend Framework</a>, I decided to create a dead simple REST server that allowed me to skip all the features I didn't need as well as a <strong>tons of classes</strong> that came with Zend Framework MVC. There are still useful features to add (XML support for example), but overall I'm quite happy with what I've come up with.</p>
<p>My solution, <code>RestServer</code>, is a JSON REST server, so far. It should be trivial to add support for XML or other formats, but there would have to be assumptions on what your object would look like in XML (XML-RPC style, your own custom XML format, etc). First we'll look at the classes that you write to handle the requests, then we'll look at how to tie it together in your index.php file.</p>
<h3>REST Controllers</h3>
<p>The <code>RestServer</code> class assumes you are using <a href="#url-rewrite" title="Example .htaccess file for Apache">URL rewriting</a> and looks at the URL from the request to map to the necessary actions. The map that gets a request from URL to class method is all in the doc-comments of the classes. Here is an example of a class that would handle some user actions:</p>
<div class="syntax_hilite">
<div id="php-16">
<div class="php"><span style="color:#000000; font-weight:bold;">class</span> TestController<br />
<span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; <span style="color:#008000;">/**<br />
&nbsp; &nbsp;&nbsp; * Returns a JSON string object to the browser when hitting the root of the domain<br />
&nbsp; &nbsp;&nbsp; * <br />
&nbsp; &nbsp;&nbsp; * @url GET /<br />
&nbsp; &nbsp;&nbsp; */</span><br />
&nbsp; &nbsp; public <span style="color:#000000; font-weight:bold;">function</span> test<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#FF0000;">"Hello World"</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color:#008000;">/**<br />
&nbsp; &nbsp;&nbsp; * Logs in a user with the given username and password POSTed. Though true<br />
&nbsp; &nbsp;&nbsp; * REST doesn't believe in sessions, it is often desirable for an AJAX server.<br />
&nbsp; &nbsp;&nbsp; * <br />
&nbsp; &nbsp;&nbsp; * @url POST /login<br />
&nbsp; &nbsp;&nbsp; */</span><br />
&nbsp; &nbsp; public <span style="color:#000000; font-weight:bold;">function</span> login<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$username</span> = <span style="color:#0000FF;">$_POST</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'username'</span><span style="color:#006600; font-weight:bold;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$password</span> = <span style="color:#0000FF;">$_POST</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'password'</span><span style="color:#006600; font-weight:bold;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// validate input and log the user in</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color:#008000;">/**<br />
&nbsp; &nbsp;&nbsp; * Gets the user by id or current user<br />
&nbsp; &nbsp;&nbsp; * <br />
&nbsp; &nbsp;&nbsp; * @url GET /users/:id<br />
&nbsp; &nbsp;&nbsp; * @url GET /users/current<br />
&nbsp; &nbsp;&nbsp; */</span><br />
&nbsp; &nbsp; public <span style="color:#000000; font-weight:bold;">function</span> getUser<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$id</span> = <span style="color:#000000; font-weight:bold;">null</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$id</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$user</span> = User::<span style="color:#006600;">load</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$id</span><span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#FF9933; font-style:italic;">// possible user loading method</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$user</span> = <span style="color:#0000FF;">$_SESSION</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'user'</span><span style="color:#006600; font-weight:bold;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#0000FF;">$user</span>; <span style="color:#FF9933; font-style:italic;">// serializes object into JSON</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color:#008000;">/**<br />
&nbsp; &nbsp;&nbsp; * Saves a user to the database<br />
&nbsp; &nbsp;&nbsp; * <br />
&nbsp; &nbsp;&nbsp; * @url POST /users<br />
&nbsp; &nbsp;&nbsp; * @url PUT /users/:id<br />
&nbsp; &nbsp;&nbsp; */</span><br />
&nbsp; &nbsp; public <span style="color:#000000; font-weight:bold;">function</span> saveUser<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$id</span> = <span style="color:#000000; font-weight:bold;">null</span>, <span style="color:#0000FF;">$data</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// ... validate $data properties such as $data-&gt;username, $data-&gt;firstName, etc.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$data</span>-&gt;<span style="color:#006600;">id</span> = <span style="color:#0000FF;">$id</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$user</span> = User::<span style="color:#006600;">saveUser</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$data</span><span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#FF9933; font-style:italic;">// saving the user to the database&nbsp; </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#0000FF;">$user</span>; <span style="color:#FF9933; font-style:italic;">// returning the updated or newly created user object</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
<span style="color:#006600; font-weight:bold;">&#125;</span></div>
</div>
</div>
<p></p>
<p>Let's walk through the above <code>TestController</code> class to talk about the features demonstrated. First we'll look at the <code>test</code> method. You'll notice there is a new kind of doc-comment tag in the docblock. <code>@url</code> maps a URL to the method below it and is in the form:</p>
<p><code>@url &lt;REQUEST_METHOD&gt; &lt;URL&gt;</code></p>
<p>In this particular example, when someone does a GET on http://www.example.com/ (assuming example.com is where our service is located) it will print out:</p>
<p><code>"Hello World"</code></p>
<p>which is a valid representation of a string in JSON.</p>
<p>Moving on to the next method, <code>login</code>, we see the <code>@url</code> maps any POSTs to http://www.example.com/login to the <code>login</code> method. Getting data from a regular web-type POST is the same as any PHP application, allowing you to use your own validation or other framework in conjunction with this REST server. Sessions can also be kept if desired. Though keeping sessions isn't true REST style, often all we want a REST server for is to serve up data to our ajax application, and it can be easier to just use sessions than something more RESTful.</p>
<p>Next we have our <code>getUser</code> method (you'll notice that it doesn't really matter what I name my methods because our <code>@url</code> directives define what URLs map to the method). You can see a couple of things here. First, we have multiple <code>@url</code> mappings for this method. And second, there is an odd "/:id" in that first URL mapping. The RestServer treats any ":keyword" placeholders as wildcards in the URL and will take that section of the URL and pass it into the parameter with the same name in the method. In this example, when hitting http://www.example.com/users/1234, <code>$id</code> will equal <code>1234</code>. When hitting http://www.example.com/users/current, <code>$id</code> will equal <code>null</code>. It doesn't matter what order your parameters are in, so long as they have the same name as the placeholder (<code>:id</code> and <code>$id</code>, <code>:username</code> and <code>$username</code>). You'll also want to be sure to make your parameters optional (<code>$id = null</code>) when you have several URL mappings that don't all require a parameter. Otherwise you'll have an error thrown telling you that you didn't pass in a required parameter.</p>
<p>One last thing to note in <code>getUser</code> is that this method simply returns a User object. This gets serialized into JSON (or potentially XML) and printed out for consumption by the application.</p>
<p>Finally we get to <code>saveUser</code>. You see here we have multiple URL mappings again. This time they also have different HTTP methods (POST and PUT) for creating and updating a user. The new thing here is the <code>$data</code> variable. This is a special keyword parameter that will contain the value of whatever was POSTed or PUT to the server. This is different than your regular web POST in that it doesn't need to only be name-value pairs, but can be as robust as JSON, sending complex objects. For example, the body of a regular web POST, let's say the <code>login</code> request, might look like this:</p>
<p><code>username=bob&#038;password=supersecretpassw0rd</code></p>
<p>but POSTing a new user object for our <code>saveUser</code> method could look like this:</p>
<p><code>{ "username": "bob", "password": "supersecretpassword", "firstName": "Bob", "lastName": "Smith" }</code></p>
<p>So you're able to allow POSTing JSON in addition to regular web style POSTs.</p>
<p>I call these classes that handle the requests Controllers. And they can be completely self-contained with their URL mappings, database configs, etc. so that you could drop them into other RestServer services without any hassle.</p>
<h3>REST index.php</h3>
<p>In order to get the whole server kicked off, you'll want to create an index.php file, have your URL rewriting direct requests to it (another topic which you can learn about elsewhere), and create the RestServer and add controller classes to it for handling. <code>RestServer</code> will cache the URL mappings between requests using APC or a file to speed up requests. You won't have to load every controller file on every request if you use autoload and this cache, only the one needed for the request. The cache only runs in production mode. Here is an example index.php file:</p>
<div class="syntax_hilite">
<div id="php-17">
<div class="php">spl_autoload_register<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#FF9933; font-style:italic;">// don't load our classes unless we use them</span></p>
<p><span style="color:#0000FF;">$mode</span> = <span style="color:#FF0000;">'debug'</span>; <span style="color:#FF9933; font-style:italic;">// 'debug' or 'production'</span><br />
<span style="color:#0000FF;">$server</span> = <span style="color:#000000; font-weight:bold;">new</span> RestServer<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$mode</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#FF9933; font-style:italic;">// $server-&gt;refreshCache(); // uncomment momentarily to clear the cache if classes change in production mode</span></p>
<p><span style="color:#0000FF;">$server</span>-&gt;<span style="color:#006600;">addClass</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'TestController'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#0000FF;">$server</span>-&gt;<span style="color:#006600;">addClass</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'ProductsController'</span>, <span style="color:#FF0000;">'/products'</span><span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#FF9933; font-style:italic;">// adds this as a base to all the URLs in this class</span></p>
<p><span style="color:#0000FF;">$server</span>-&gt;<span style="color:#006600;">handle</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</div>
</div>
<p></p>
<p>That's it. You can add as many classes as you like. If there are conflicts, classes added later will overwrite duplicate URL mappings that were added earlier. And the second parameter in <code>addClass</code> can be a base URL which will be prepended to URL mappings in the given class, allowing you to be more modular.</p>
<p>You can <a href="/blog/resources/RestServer.txt">view the RestServer class</a>, copy it and use it for your own purposes. It is under the MIT license. Features to be added include XML support and HTTP Authentication support. If you make this class better please share your updates with everyone by leaving a comment. I will try and keep this class updated with new features as they are shared. I hope you enjoy!</p>
<p>I changed the title of this post to remove the AMF portion but was asked to cover it, so I will quickly talk about the AMF support. <code>RestServer</code> supports the AMF format in addition to the JSON format. This is a binary format used by Adobe Flash in their remoting services, but because their remoting services are not RESTful, you can't use classes such as RemoteObject with REST.</p>
<p>In order to use the AMF format with this service, you'll need to have the Zend Framework in your classpath so that the classes to serialize and deserialize AMF are present (e.g. Zend/Amf/Parse/Amf3/Serializer.php). Then you're ready so server up AMF. The way you consume AMF in Flash is using URLLoader or URLStream to load the data and ByteArray to convert it into an object. To tell the server you want AMF rather than JSON your URLRequest object will need to add an Accept header of "application/x-amf". Below I will show you how this could be done.</p>
<div class="syntax_hilite">
<div id="actionscript-18">
<div class="actionscript"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getUser<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> loader:URLLoader = <span style="color: #000000; font-weight: bold;">new</span> URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; loader.<span style="color: #006600;">dataFormat</span> = URLLoaderDataFormat.<span style="color: #006600;">BINARY</span>;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> request:URLRequest = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"http://www.example.com/users/current"</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; request.<span style="color: #006600;">requestHeaders</span>.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> URLRequestHeader<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"Accept"</span>, <span style="color: #ff0000;">"application/x-amf"</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; loader.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>, onComplete<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; loader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>request<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span></p>
<p><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> onComplete<span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> loader:URLLoader = event.<span style="color: #0066CC;">target</span> as URLLoader;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> byteArray:ByteArray = loader.<span style="color: #0066CC;">data</span> as ByteArray;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> user:<span style="color: #0066CC;">Object</span> = byteArray.<span style="color: #006600;">readObject</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// do something with the user</span><br />
<span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p>You can even make your PHP objects cast into their actionscript equivalents using the <code>$_explicitType</code> property or <code>getASClassName</code> method in PHP as defined in the <a href="http://framework.zend.com/manual/en/zend.amf.server.html#zend.amf.server.typedobjects">documentation</a> and by using registerAlias in Flash.</p>
<p>Good luck and let me know if you end up using it!</p>
<p><a name="url-rewrite"></a><strong>Update:</strong> I am including an example .htaccess file for anyone who might need it. It will only rewrite requests to files that don't exist, so you can have images, css, or other PHP files in your webroot and they will still work. Anything that would give a 404 will redirect to your index.php file.</p>
<pre>
DirectoryIndex index.php
&lt;IfModule mod_rewrite.c&gt;
	RewriteEngine On
	RewriteRule ^$ index.php [QSA,L]
	RewriteCond %{REQUEST_FILENAME} !-f
	RewriteCond %{REQUEST_FILENAME} !-d
	RewriteRule ^(.*)$ index.php [QSA,L]
&lt;/IfModule&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://jacwright.com/blog/250/simple-rest-server-in-php-supports-json-amf/feed/</wfw:commentRss>
		<slash:comments>52</slash:comments>
		</item>
		<item>
		<title>Response Pattern in ActionScript 3</title>
		<link>http://jacwright.com/blog/271/response-pattern-in-actionscript-3/</link>
		<comments>http://jacwright.com/blog/271/response-pattern-in-actionscript-3/#comments</comments>
		<pubDate>Mon, 24 Aug 2009 19:13:32 +0000</pubDate>
		<dc:creator>Jacob Wright</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[Flash]]></category>

		<guid isPermaLink="false">http://jacwright.com/blog/?p=271</guid>
		<description><![CDATA[Earlier I posted about the Response Pattern. Today I wanted to show an ActionScript implementation and how it would be used. The API I decided to go with for this pattern uses method chaining. This is one of those things where people love it or they despise it. I'm sorry if you are one of [...]]]></description>
			<content:encoded><![CDATA[<p>Earlier I posted about the Response Pattern. Today I wanted to show an ActionScript implementation and how it would be used.</p>
<p>The API I decided to go with for this pattern uses method chaining. This is one of those things where people love it or they despise it. I'm sorry if you are one of the latter. Method chaining is where an object returns a reference to itself as the result of its methods. This allows you to call many methods on the object in one line. jQuery uses this and it is used a lot in the Zend Framework. As example you might have a drawing API helper that does this:</p>
<div class="syntax_hilite">
<div id="actionscript-22">
<div class="actionscript">shape.<span style="color: #006600;">fill</span><span style="color: #66cc66;">&#40;</span>0x660000<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">rect</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">100</span>, <span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">fill</span><span style="color: #66cc66;">&#40;</span>0x990000<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">rect</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">98</span>, <span style="color: #cc66cc;">98</span><span style="color: #66cc66;">&#41;</span>;</div>
</div>
</div>
<p></p>
<p>This would draw a red box with a darker red border around it, all in one line. If the above code gives you tingles in your stomach, you'll like response. If it upsets you, you can still use response with one method per line of code.</p>
<p>You can see in the interface <a href="http://code.google.com/p/jacwright/source/browse/trunk/flash/jac/src/jac/net/IResponse.as">IResponse</a> that it has method chaining and supports a bindable progress property as well. The <a href="http://code.google.com/p/jacwright/source/browse/trunk/flash/jac/src/jac/net/Response.as">Response</a> implementation has a few more methods that are useful when first creating a response. It allows events to trigger the complete cycle (which runs through the result handlers and calls them) or the error cycle. It also will create a flash.net.Responder object that can be used in NetConnection calls or other situations that take a native responder object. There are also some more advanced uses of Response which we might talk about another time. But let's see it in action!</p>
<p>I've posted my <a href="http://code.google.com/p/jacwright/source/browse/#svn/trunk/flash/jac">library of code</a> I reuse on Google Code under the MIT license. Here you'll find a class called <a href="http://code.google.com/p/jacwright/source/browse/trunk/flash/jac/src/jac/image/ImageLibrary.as">ImageLibrary</a>. Here is how it works.</p>
<div class="syntax_hilite">
<div id="actionscript-23">
<div class="actionscript"><span style="color: #808080; font-style: italic;">// MySprite which extends Sprite</span></p>
<p><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> addImage<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> bitmap:Bitmap = <span style="color: #000000; font-weight: bold;">new</span> Bitmap<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; addChild<span style="color: #66cc66;">&#40;</span>bitmap<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; ImageLibrary.<span style="color: #006600;">getInstance</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getImage</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">handle</span><span style="color: #66cc66;">&#40;</span>addDataToBitmap, bitmap<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span></p>
<p><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> addDataToBitmap<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span>:<span style="color: #0066CC;">Object</span>, bitmap:Bitmap<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span> is BitmapData<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bitmap.<span style="color: #006600;">bitmapData</span> = <span style="color: #0066CC;">data</span> as BitmapData;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p>In this example, we have a result handler that sets the BitmapData object to the bitmap object. We could have created and added the bitmap to the Sprite in the handler, but I wanted to show how you can pass extra parameters to the handlers in the <code>handle(resultHandler:Function, ...rest)</code> method. Another thing to note, is that if we were to add the same image to our <code>MySprite</code> class again, addDataToBitmap would be called instantly because ImageLibrary caches the results of previous calls so that the player won't have to load them from the server again. This results in virtualized lists having the images instantaneously showing up after the first load.</p>
<p>The RemoteProxy class is a Flash remoting service which is extremely small. It uses Response.</p>
<div class="syntax_hilite">
<div id="actionscript-24">
<div class="actionscript"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> loadContacts<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:IResponse <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> service:RemoteProxy = <span style="color: #000000; font-weight: bold;">new</span> RemoteProxy<span style="color: #66cc66;">&#40;</span>gateway<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// call the list operation and then handle the results with setContacts</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// and handle errors with errorLoading</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> service.<span style="color: #006600;">contacts</span>.<span style="color: #0066CC;">list</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">handle</span><span style="color: #66cc66;">&#40;</span>setContacts<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">handleError</span><span style="color: #66cc66;">&#40;</span>errorLoading<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span></p>
<p><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setContacts<span style="color: #66cc66;">&#40;</span>contacts:<span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; _contacts = contacts;<br />
<span style="color: #66cc66;">&#125;</span></p>
<p>protected <span style="color: #000000; font-weight: bold;">function</span> errorLoading<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">error</span>:<span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; Logging.<span style="color: #0066CC;">log</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"Error loading contacts: "</span> + <span style="color: #0066CC;">error</span>.<span style="color: #0066CC;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p>In this example we pass the Response object along so that whoever is calling <code>loadContacts</code> can know when the result is ready or if there was an error.</p>
<p>Here is a list of some of the classes I've made which use Response:</p>
<ul>
<li><a href="http://code.google.com/p/jacwright/source/browse/trunk/flash/jac/src/jac/image/ImageLibrary.as">jac.image.ImageLoader</a></li>
<li><a href="http://code.google.com/p/jacwright/source/browse/trunk/flash/jac/src/jac/net/RemoteProxy.as">jac.net.RemoteProxy</a></li>
<li><a href="http://code.google.com/p/jacwright/source/browse/trunk/flash/jac/src/jac/net/RemoteService.as">jac.net.RemoteService</a></li>
<li><a href="http://code.google.com/p/jacwright/source/browse/trunk/flash/jac_air/src/jac/db/Database.as">jac.db.Database</a></li>
<li><a href="http://code.google.com/p/jacwright/source/browse/trunk/flash/jac_air/src/jac/filesystem/AsyncFile.as">jac.filesystem.AsyncFile</a></li>
</ul>
<p>This is just a little taste of using the Response Pattern. I have found it much nicer to use when you are waiting for the results of a particular call. Event listeners are still useful for user input and for socket-based libraries such as XMPP or FMS, but I like Response for most other async operations.</p>
]]></content:encoded>
			<wfw:commentRss>http://jacwright.com/blog/271/response-pattern-in-actionscript-3/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>High quality, high performance thumbnails in Flash</title>
		<link>http://jacwright.com/blog/221/high-quality-high-performance-thumbnails-in-flash/</link>
		<comments>http://jacwright.com/blog/221/high-quality-high-performance-thumbnails-in-flash/#comments</comments>
		<pubDate>Fri, 17 Jul 2009 19:37:03 +0000</pubDate>
		<dc:creator>Jacob Wright</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>

		<guid isPermaLink="false">http://jacwright.com/blog/?p=221</guid>
		<description><![CDATA[Ever need a thumbnail of an image in Flash? I do, and honestly speaking, the resampling that Flash does is less than ideal. Unless you only need to resize by half or bigger. But my thumbnails usually need to be smaller. I searched for a solution and found on voq.com a promising library with some [...]]]></description>
			<content:encoded><![CDATA[<p>Ever need a thumbnail of an image in Flash? I do, and honestly speaking, the resampling that Flash does is less than ideal. Unless you only need to resize by half or bigger. But my thumbnails usually need to be smaller.</p>
<p>I searched for a solution and found on voq.com a <a href="http://blog.voq.jp/2007/12/creating_high_quality_thumnail.html">promising library</a> with some algorithms that worked quite nicely and a demo. The quality was nice, but the speed was slow. It also fiddled with the color a little bit. If I put the "easyScaling" parameter down from .5 to .25 I ended up with a nicer thumbnail but was slower to make and had more color disfiguration. You could tell what the original piece was better though.</p>
<p>After some more searching I found that <a href="http://www.brooksandrus.com/blog/2009/03/11/bilinear-resampling-with-flash-player-and-pixel-bender/">Brooks Andrus played with some algorithms using Pixel Bender</a>. The conclusion to that was he found he could do the same thing with the "smoothing" option in the BitmapData.draw() method. And this still leaves me with yucky thumbnails at small sizes.</p>
<p>I began thinking I would need to take the algorithms from the first source and port them into pixel bender like Brooks did for his ThumbGenie application.</p>
<p>I thought I'd try one more option first. I had played around in my mind with the idea of resizing a bitmap in half, then in half again, until I reached my destination size, since resizing by half still had good results. After discussing it with Tyler I tried it out and ended up with some great results. That last resize to get to the final thumbnail size wasn't half because in an imperfect world your thumbnails aren't always a power of two times smaller than your original. So I had an odd-man-out scale at the end I was applying. Tyler suggested I put that odd-man-out scale at the first instead. So instead of:</p>
<p>1000x750 * .5 resize * .5 resize * .5 resize * .8 = 100x75</p>
<p>It worked like this:</p>
<p>1000x750 * .8 resize * .5 resize * .5 resize * .5 = 100x75</p>
<p>Doing it that way landed me with even better results! It seems that Flash does it's best work when resizing by exactly 1/2.</p>
<p><img src="http://jacwright.com/blog/wp-content/uploads/2009/07/Thumbnailing-Results.png" alt="Thumbnail tests" /></p>
<p>The above image shows my test results as follows taking a snapshot of my homepage in an HTMLLoader in AIR.</p>
<ol>
<li>Regular BitmapData.draw() method without smoothing (0 miliseconds)</li>
<li>Regular BitmapData.draw() method <strong>with</strong> smoothing (1 miliseconds)</li>
<li>Using my own method that resizes by the odd scale first, then by halves (8 milliseconds)</li>
<li>Using my method but after the odd scale, only scaling down by quarters (33 miliseconds)</li>
<li>Using the voq Lanczos3 method with easyScaling at .25 and sharpening (694 miliseconds)</li>
<li>Using the voq Lanczos3 method with easyScaling at .25 and no sharping (676 miliseconds)</li>
<li>Using the voq Triangle method with easyScaling at .25 and sharpening (275 miliseconds)</li>
<li>Using the voq Triangle method with easyScaling at .25 and no sharpening (266 miliseconds)</li>
</ol>
<p>My personal opinion is that the best looking thumb is #3. I was surprised that #4 wouldn't be as good. I knew the performance would be lower because I was doing more iterations of BitmapData.draw(), but I thought that scaling it to 75% (down a quarter) each time would end up nicer. Looks like 50% is the best scale to use.</p>
<p>The voq.com algos looked pretty decent, but obviously were slow. I'm quite happy with the solution I've found. I only had to write a little bit of code, it's fast enough, and looks better than any other solution I've found (even Fireworks resizing IMO).</p>
<p>Here is the method I wrote for this. Note, when scaling up, it seemed to look better to just use smoothing and do it in one draw() and not iterations of 2.</p>
<div class="syntax_hilite">
<div id="actionscript-26">
<div class="actionscript"><span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> const IDEAL_RESIZE_PERCENT:<span style="color: #0066CC;">Number</span> = .<span style="color: #cc66cc;">5</span>;</p>
<p><span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> resizeImage<span style="color: #66cc66;">&#40;</span>source:BitmapData, <span style="color: #0066CC;">width</span>:uint, <span style="color: #0066CC;">height</span>:uint, constrainProportions:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>:BitmapData<br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> scaleX:<span style="color: #0066CC;">Number</span> = <span style="color: #0066CC;">width</span>/source.<span style="color: #0066CC;">width</span>;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> scaleY:<span style="color: #0066CC;">Number</span> = <span style="color: #0066CC;">height</span>/source.<span style="color: #0066CC;">height</span>;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>constrainProportions<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>scaleX&gt; scaleY<span style="color: #66cc66;">&#41;</span> scaleX = scaleY;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">else</span> scaleY = scaleX;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> bitmapData:BitmapData = source;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>scaleX&gt;= <span style="color: #cc66cc;">1</span> &amp;&amp; scaleY&gt;= <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; bitmapData = <span style="color: #000000; font-weight: bold;">new</span> BitmapData<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">ceil</span><span style="color: #66cc66;">&#40;</span>source.<span style="color: #0066CC;">width</span>*scaleX<span style="color: #66cc66;">&#41;</span>, <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">ceil</span><span style="color: #66cc66;">&#40;</span>source.<span style="color: #0066CC;">height</span>*scaleY<span style="color: #66cc66;">&#41;</span>, <span style="color: #000000; font-weight: bold;">true</span>, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; bitmapData.<span style="color: #006600;">draw</span><span style="color: #66cc66;">&#40;</span>source, <span style="color: #000000; font-weight: bold;">new</span> Matrix<span style="color: #66cc66;">&#40;</span>scaleX, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, scaleY<span style="color: #66cc66;">&#41;</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> bitmapData;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// scale it by the IDEAL for best quality</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> nextScaleX:<span style="color: #0066CC;">Number</span> = scaleX;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> nextScaleY:<span style="color: #0066CC;">Number</span> = scaleY;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>nextScaleX &lt;<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> nextScaleX /= IDEAL_RESIZE_PERCENT;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>nextScaleY &lt;<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> nextScaleY /= IDEAL_RESIZE_PERCENT;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>scaleX &lt;IDEAL_RESIZE_PERCENT<span style="color: #66cc66;">&#41;</span> nextScaleX *= IDEAL_RESIZE_PERCENT;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>scaleY &lt;IDEAL_RESIZE_PERCENT<span style="color: #66cc66;">&#41;</span> nextScaleY *= IDEAL_RESIZE_PERCENT;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> temp:BitmapData = <span style="color: #000000; font-weight: bold;">new</span> BitmapData<span style="color: #66cc66;">&#40;</span>bitmapData.<span style="color: #0066CC;">width</span>*nextScaleX, bitmapData.<span style="color: #0066CC;">height</span>*nextScaleY, <span style="color: #000000; font-weight: bold;">true</span>, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; temp.<span style="color: #006600;">draw</span><span style="color: #66cc66;">&#40;</span>bitmapData, <span style="color: #000000; font-weight: bold;">new</span> Matrix<span style="color: #66cc66;">&#40;</span>nextScaleX, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, nextScaleY<span style="color: #66cc66;">&#41;</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; bitmapData = temp;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; nextScaleX *= IDEAL_RESIZE_PERCENT;<br />
&nbsp; &nbsp; nextScaleY *= IDEAL_RESIZE_PERCENT;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>nextScaleX&gt;= scaleX || nextScaleY&gt;= scaleY<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> actualScaleX:<span style="color: #0066CC;">Number</span> = nextScaleX&gt;= scaleX ? IDEAL_RESIZE_PERCENT : <span style="color: #cc66cc;">1</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> actualScaleY:<span style="color: #0066CC;">Number</span> = nextScaleY&gt;= scaleY ? IDEAL_RESIZE_PERCENT : <span style="color: #cc66cc;">1</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; temp = <span style="color: #000000; font-weight: bold;">new</span> BitmapData<span style="color: #66cc66;">&#40;</span>bitmapData.<span style="color: #0066CC;">width</span>*actualScaleX, bitmapData.<span style="color: #0066CC;">height</span>*actualScaleY, <span style="color: #000000; font-weight: bold;">true</span>, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; temp.<span style="color: #006600;">draw</span><span style="color: #66cc66;">&#40;</span>bitmapData, <span style="color: #000000; font-weight: bold;">new</span> Matrix<span style="color: #66cc66;">&#40;</span>actualScaleX, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, actualScaleY<span style="color: #66cc66;">&#41;</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; bitmapData.<span style="color: #006600;">dispose</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; nextScaleX *= IDEAL_RESIZE_PERCENT;<br />
&nbsp; &nbsp; &nbsp; &nbsp; nextScaleY *= IDEAL_RESIZE_PERCENT;<br />
&nbsp; &nbsp; &nbsp; &nbsp; bitmapData = temp;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> bitmapData;<br />
<span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p>Enjoy!</p>
<p><strong>Update:</strong> I was getting unsatisfactory images when the resize scale was more than 50%. For example, the original code posted would size an image from 100x100 to 60x60 using one BitmapData.draw() step. And it didn't look that great.</p>
<p>I found that if I sized the image up to a scale that allowed it to be size back down by exactly 50% that the results were much better. In the above example, the 100x100 would scale up by 120% to 120x120, then scale down 50% to 60x60. The final image looks much better this way. The code has been updated to work like this. It also had the option to turn constrain proportions off.</p>
<p><strong>Update:</strong> I've <a href="http://code.google.com/p/jacwright/source/browse/#svn/trunk/flash/jac/src/jac/image">posted my code</a> library to Google Code. You can see my final implementation of bitmap resizing in the <a href="http://code.google.com/p/jacwright/source/browse/trunk/flash/jac/src/jac/image/ImageUtils.as">ImageUtils</a> class.</p>
]]></content:encoded>
			<wfw:commentRss>http://jacwright.com/blog/221/high-quality-high-performance-thumbnails-in-flash/feed/</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
		<item>
		<title>Would it be bad to leave behind our Flash roots?</title>
		<link>http://jacwright.com/blog/209/would-it-be-bad-to-leave-behind-our-flash-roots/</link>
		<comments>http://jacwright.com/blog/209/would-it-be-bad-to-leave-behind-our-flash-roots/#comments</comments>
		<pubDate>Thu, 14 May 2009 17:02:49 +0000</pubDate>
		<dc:creator>Jacob Wright</dc:creator>
				<category><![CDATA[AIR]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>

		<guid isPermaLink="false">http://jacwright.com/blog/?p=209</guid>
		<description><![CDATA[I am working with Tyler on Stealth, our high-performance component framework. After reading this article on performance by Arno Gourdol of Adobe I began wondering if we should leave behind our Flash roots of motion and timeline design by defaulting framerate to 0 in our Stealth-based applications. Framerate makes great sense when doing games or [...]]]></description>
			<content:encoded><![CDATA[<p>I am working with <a href="http://www.xtyler.com/">Tyler</a> on Stealth, our high-performance component framework. After reading <a href="http://arno.org/arnotify/2009/05/writing-well-behaved-efficient-air-applications/" title="Writing well-behaved, efficient, AIR applications">this article</a> on performance by Arno Gourdol of Adobe I began wondering if we should leave behind our Flash roots of motion and timeline design by defaulting framerate to 0 in our Stealth-based applications.</p>
<p>Framerate makes great sense when doing games or timeline based animations, but in applications do we need it? We can update the screen on mouse moves, roll overs, etc. with the MouseEvent.updateAfterEvent instance method. And for transitions and tweening the class could use a Timer for the duration of the animation and again call Timer.updateAfterEvent. Then the screen would only refresh when it needs to. Performance would be greatly increased. Seems like it makes sense. Would this be something to add to Flex? Would it give us the performance we need/want for mobile applications and more responsive desktop applications?</p>
<p>Any foreseeable drawbacks? What do you think?</p>
<p><strong>Update:</strong> I did some testing and it seems that the Timer class is directly influenced by the frameRate. With a frame rate of 0 a timer which should fire immediately (set to 0ms) doesn't fire for 20 seconds! With the frame rate a 0.1 it happens at about 2 seconds, a frame rate of 1 is about 145 ms and a frame rate of anything over 4 seems to be around the same (10ms - 30ms probably depending on what the OS is currently doing).</p>
<p>MouseEvent updating and such happen as they should however, so as long as you start of with a frame rate of 4 so that the app can initialize visually, you could drop it down to 0 until a tween is needed and then bump it up to 4 for the duration of the tween. The rest of the visual changes can respond to mouse events (resize, click, rollover, etc). Or leaving it at 4 frames a second probably isn't too bad on CPU, either way, you'd have to call updateAfterEvent when needed.</p>
]]></content:encoded>
			<wfw:commentRss>http://jacwright.com/blog/209/would-it-be-bad-to-leave-behind-our-flash-roots/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Flight Framework Highlights</title>
		<link>http://jacwright.com/blog/144/flight-framework-highlights/</link>
		<comments>http://jacwright.com/blog/144/flight-framework-highlights/#comments</comments>
		<pubDate>Thu, 12 Feb 2009 18:46:03 +0000</pubDate>
		<dc:creator>Jacob Wright</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://jacwright.com/blog/?p=144</guid>
		<description><![CDATA[The Flight Framework is basically a collection of great utilities and independent tidbits that are organized to help us build our application. So I thought I'd make a call out to all the great standalone features in Flight, tell what they are, and leave it to another day to go more in depth on them. [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://www.flightxd.com/flightframework/">Flight Framework</a> is basically a collection of great utilities and independent tidbits that are organized to help us build our application. So I thought I'd make a call out to all the great standalone features in Flight, tell what they are, and leave it to another day to go more in depth on them.</p>
<ol>
<li><strong>Fix for abstract classes</strong><br />
The tiny static classes in the flight.error package allow you to ensure an abstract class is not instantiated and that abstract methods are overriden.</li>
<li><strong>Registry</strong><br />
A global registry to store objects or data that anything in an application can have access too.</li>
<li><strong>Reflection<br />
</strong>The "Type" class handles all your reflection needs, including caching the expensive describeType calls and helping pull out needed metadata.</li>
<li><strong>Nice base for models<br />
</strong>The ValueObject class is a lazy IEventDispatcher that implements an equals() and clone() method which will work with all subclasses.</li>
<li><strong>ValueObjectEditor</strong><br />
This nifty utility lets you "edit" a ValueObject and then either commit the changes or revert them. Great for dialogs that let ValueObjects be changed but can then either Cancel or Ok the change.</li>
<li><strong>AS3 ArrayContainer</strong><br />
When working in an AS3 project, it would be nice to know when an array is changed, but Flex's ArrayCollection brings in half the framework when referenced. This little guy is the little brother AS3 has been wishing for.</li>
<li><strong>Services<br />
</strong>Some common services (RPC services, e.g. remoting, HTTP, etc.) that can be used in Flex or AS3. Much smaller than Flex's built in services.</li>
<li><strong>Configuration<br />
</strong>Config lets you define your application configurations in the app, an XML file, SharedObjects, and even the URL. Not only that, you can have multiple sources with some overriding others, and it is all accessible to your classes through a global config property. Super neat!</li>
<li><strong>Undo framework</strong><br />
With CommandHistory and all the command interfaces available, you can build undo-redo into your application without using any of the rest of Flight. Way, way awesome.</li>
<li><strong>Weak-referenced binding</strong><br />
Using the Bind class you can create faster, smaller, and weak-referenced binds. With two-way deep binds and being able to use in AS3-only projects without importing half the Flex framework, this is my favorite nugget.</li>
</ol>
<p>None of the above pieces references any of the others. They can all be used independently in your own frameworks or applications. That's my favorite part about Flight, is I can use it 100% if I want to, but if I can't that doesn't mean I have to give up my favorite features. I could use any/all of the above with any other framework, AS3 only, or Flex.</p>
]]></content:encoded>
			<wfw:commentRss>http://jacwright.com/blog/144/flight-framework-highlights/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Undo/Redo for all your TextFields</title>
		<link>http://jacwright.com/blog/112/undo-redo-for-all-textfields/</link>
		<comments>http://jacwright.com/blog/112/undo-redo-for-all-textfields/#comments</comments>
		<pubDate>Wed, 04 Feb 2009 11:00:12 +0000</pubDate>
		<dc:creator>Jacob Wright</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>

		<guid isPermaLink="false">http://jacwright.com/blog/?p=112</guid>
		<description><![CDATA[I made the TextField undo component. Add this component to any Flash or Flex application and all your text components will have undo and redo. This has been open sourced and is on http://code.google.com/p/undo-textfields/. Instead of making different Flex and Flash components to use in place of the available components, I made one that adds [...]]]></description>
			<content:encoded><![CDATA[<p>I made the TextField undo component. Add this component to any Flash or Flex application and all your text components will have undo and redo. This has been open sourced and is on <a title="Google Code Project" href="http://code.google.com/p/undo-textfields/">http://code.google.com/p/undo-textfields/</a>.</p>
<p>Instead of making different Flex and Flash components to use in place of the available components, I made one that adds undo to existing text components. It should work in Flash and in Flex, though I just have a Flex example to show you today. You can see that it can be container specific, though you would probably just put it on the root of your application or the stage. Type in the text components in the top panel. Then do the same in the bottom. You should be able to press Ctrl+Z and Ctrl+Y (or Ctrl+Shift+Z) to undo and redo. If you're on a Mac just substitute Ctrl with Cmd. I've only tested it in Safari and Firefox on a Mac. I hope the key command work on other machines without issue. But I also added a couple of menu items to the right-click context menu. Give it a whirl below, view the source, and see how easy it is to add text undo and redo to all your existing applications without any refactoring.</p>
<p><strong>Update:</strong> The keyboard shortcuts don't work in the ActiveX version of Flash (Internet Explorer on Windows).</p>

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_0" width="100%" height="430" align="center">
      <param name="movie" value="http://jacwright.com/blog/wp-content/uploads/2009/02/undotest.swf" />
      <param name="align" value="center" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://jacwright.com/blog/wp-content/uploads/2009/02/undotest.swf" width="100%" height="430" align="center">
      <!--<![endif]-->
        Undo TextFields
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>

<p>Let me know if the key-shortcuts don't work on your system. Maybe phase 2 can be some javascript magic to capture those better.</p>
]]></content:encoded>
			<wfw:commentRss>http://jacwright.com/blog/112/undo-redo-for-all-textfields/feed/</wfw:commentRss>
		<slash:comments>49</slash:comments>
		</item>
		<item>
		<title>Undo in TextFields</title>
		<link>http://jacwright.com/blog/109/undo-in-textfields/</link>
		<comments>http://jacwright.com/blog/109/undo-in-textfields/#comments</comments>
		<pubDate>Thu, 22 Jan 2009 17:01:10 +0000</pubDate>
		<dc:creator>Jacob Wright</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>

		<guid isPermaLink="false">http://jacwright.com/blog/?p=109</guid>
		<description><![CDATA[I presented on creating your own undo/redo at 360 MAX unconference and a uFlash meeting. People seemed interested in knowing how to do it, but were more interested in having the code. It really should be part of Flex TextAreas and TextInputs. So I was going to code up a couple custom components that extended [...]]]></description>
			<content:encoded><![CDATA[<p>I presented on creating your own undo/redo at 360 MAX unconference and a uFlash meeting. People seemed interested in knowing how to do it, but were more interested in having the code. It really should be part of Flex TextAreas and TextInputs. So I was going to code up a couple custom components that extended TextArea and TextInput (e.g. UndoableTextArea) and release them for use. However, as <a href="http://www.xtyler.com" title="My twin brother">Tyler</a> reminded me, that wouldn't work in Flash apps, only Flex. So I have a proposal and would like to get your feedback.</p>
<p>If I created a component that you put on the root of your application (or any MXML component) which added undo/redo to any/all INPUT TextFields with in Flash or Flex would that be better? If you're not doing any custom INPUT TextFields I don't see any issues. But it could cause problems if you're doing your own text components. Also, I would do it by listening to Event.ADDED events and whenever a TextField is added to the display list it would initialize it and add some right-click menu items to it. And I can even make sure when I have script access through ExternalInterface to capture the Ctrl+Z key combo.</p>
<p>Comment and let me know if this would be better than the Flex components. It would work in Flash apps too is the nice part and also you could add it to existing applications without any find-replace. The downsides is it could interfere with text manipulation code you may have in place for custom text components. I'll decide this week and get to work on it next!</p>
]]></content:encoded>
			<wfw:commentRss>http://jacwright.com/blog/109/undo-in-textfields/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
