Tuesday, February 7th, 2006 at 11:38 am Filed under: Flash, Web Design Tags: , ,

As I mentioned in my last post, the Delegate class is quite useful even if I couldn’t pass parameters as easily as I would have wanted. While looking around today, I stumbled on this method which also makes things much easier. I don’t like how it’s written because of the in-line functions, but it really does solve the problem. Here’s the code, which I took the liberty to customize a little bit:

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 < 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;
}

And you can call it like this:

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...
    }
}

This was a huge time savor and suits how I like to write code.

Comments RSS

You can leave a response, or trackback from your own site. No Responses to “Eureka Follow up”.

Leave a Reply