<?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>Francois Faubert &#187; Event Delegation</title>
	<atom:link href="http://www.francoisfaubert.com/tag/event-delegation/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.francoisfaubert.com</link>
	<description></description>
	<lastBuildDate>Tue, 08 Sep 2009 19:00:31 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Eureka Follow up</title>
		<link>http://www.francoisfaubert.com/2006/02/07/eureka-follow-up/</link>
		<comments>http://www.francoisfaubert.com/2006/02/07/eureka-follow-up/#comments</comments>
		<pubDate>Tue, 07 Feb 2006 16:38:19 +0000</pubDate>
		<dc:creator>francoisfaubert</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[Actionscript 2]]></category>
		<category><![CDATA[Event Delegation]]></category>
		<category><![CDATA[OOP]]></category>

		<guid isPermaLink="false">http://themusictank.com/fake/?p=12</guid>
		<description><![CDATA[The use of a static method to delegate events in Flash is one of the easiest ways of associating functions to an event.]]></description>
			<content:encoded><![CDATA[<p>As I mentioned in my last post, the Delegate class is quite useful even if I couldn&#8217;t pass parameters as easily as I would have wanted. While looking around today, I stumbled on <a href="http://www.person13.com/articles/proxy/Proxy.htm">this method</a> which also makes things much easier. I don&#8217;t like how it&#8217;s written because of the in-line functions, but it really does solve the problem. Here&#8217;s the code, which I took the liberty to customize a little bit:</p>
<pre><code>class Utils {
// Desc		:	Usually, when you attach a function to an event, you can't pass
//			parameters, using this function instead of the Delegate class
//			will allow you to do the same thing but with unlimitted parameters
// 			Window: General Information.
// 			http://www.person13.com/articles/proxy/Proxy.htm
public static function delegateEvent(oTarget:Object, fFunction:Function):Function {
    // Create an array of the extra parameters passed to the method. Loop
    // through every element of the arguments array starting with index 2,
    // and add the element to the aParameters array.
    var aParameters:Array = new Array();
    for(var i:Number = 2; i &lt; arguments.length; i++) {
        aParameters[i - 2] = arguments[i];
    }
    // Create a new function that will be the proxy function.
    var fProxy:Function = function():Void {
        var aActualParameters:Array = arguments.concat(aParameters);
        fFunction.apply(oTarget, aActualParameters);
    };
    return fProxy;
}</code></pre>
<p>And you can call it like this:</p>
<pre><code>private function someFunction():Void {
    this._xml.onLoad = Utils.delegateEvent(this, xml_onLoad, "GeneralInformation");
}

private function xml_onLoad(success:Boolean, nextStep:String):Void {
    if(success)
    // Get the contents of xml
    switch (nextStep) {
        case "GeneralInformation" :
            this.open_GeneralInformation(); break;
    //etc...
    }
}</code></pre>
<p>This was a huge time savor and suits how I like to write code.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.francoisfaubert.com/2006/02/07/eureka-follow-up/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Eureka!</title>
		<link>http://www.francoisfaubert.com/2006/02/01/eureka/</link>
		<comments>http://www.francoisfaubert.com/2006/02/01/eureka/#comments</comments>
		<pubDate>Wed, 01 Feb 2006 19:13:52 +0000</pubDate>
		<dc:creator>francoisfaubert</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[Actionscript 2]]></category>
		<category><![CDATA[Event Delegation]]></category>
		<category><![CDATA[OOP]]></category>

		<guid isPermaLink="false">http://themusictank.com/fake/?p=11</guid>
		<description><![CDATA[I’ve been having a lot of trouble having Event (clicks, loads, etc&#8230;) in Flash for as long as I’ve been doing Object-Oriented ActionScript. I barely managed to call inline functions and having to use the absolute path to the property. Example:
class Foo {
	public var _someProperty:String
	public function Foo() {
		// Lets skip how I actually make the [...]]]></description>
			<content:encoded><![CDATA[<p>I’ve been having a lot of trouble having Event (clicks, loads, etc&#8230;) in Flash for as long as I’ve been doing Object-Oriented ActionScript. I barely managed to call inline functions and having to use the absolute path to the property. Example:</p>
<pre><code>class Foo {
	public var _someProperty:String
	public function Foo() {
		// Lets skip how I actually make the button,
		// but I usually use .attach() and I make
		// exportable the object in the Library. I
		// think I should use createClassComponent however.
		var but:MovieClip = new MovieClip();
		but.onPress = function () {
		_root.MyFooObject._someProperty = “Clicked”;
		}
	}
}</code></pre>
<p>To access my attribute, I must leave my class and follow the absolute, practically hard-coded, path to my value. It&#8217;s really not a good thing to do. Instead, I found you can use the Delegate class to forward events to a function inside the parent class. Like this:</p>
<pre><code>class Foo {
	private var _someProperty:String
	public function Foo() {
		// Lets skip how I actually make the button here too.
		var but:MovieClip = new movieclip();
		but.onPress = mx.utils.Delegate.create(this, button_onClick);
	}
	private function button_onClick():Void {
		this._someProperty = “Clicked”;
	}
}</code></pre>
<p>Where &#8220;this&#8221; means the class you are in (could be another one), and &#8220;button_onClick&#8221; is the name of the function you will be calling after the event is triggered.</p>
<p>There you go! It’s a lot more powerful, much more logical from a semantic point of view and it makes the code a lot clearer. Notice how I can protect my call properties now using the private attribute.</p>
<p>Speaking of being semantic, you always should call you call properties with &#8220;getters and setters&#8221; instead of calling it directly like I did in my example. It should look like this instead:</p>
<pre><code>private var _someProperty:String
public function get someProperty():String { return this._someProperty;}
public function set someProperty(val:String):Void {
    // validation if needed; this._someProperty = val;
}</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.francoisfaubert.com/2006/02/01/eureka/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
