1  /**
     2   * com.sekati.utils.Delegate
     3   * @version 1.0.1
     4   * @author jason m horwitz | sekati.com
     5   * Copyright (C) 2007  jason m horwitz, Sekat LLC. All Rights Reserved.
     6   * Released under the MIT License: http://www.opensource.org/licenses/mit-license.php
     7   */
     8   
     9  /**
    10   * The Delegate class creates a function wrapper to let you run a function in the context of
    11   * the original object, rather than in the context of the second object, when you pass a
    12   * function from one object to another<br/><br/>
    13   *   
    14   * This customized version allows for function arguments & is repurposed for the framework
    15   * based on bigspaceships version of adobe/mm'd mx.utils.Delegate. 
    16   * 
    17   * {@code Usage:
    18   * myMovieClip.onEnterFrame = Delegate.create(this,_onEnterFrame,"hello world");
    19   * function _onEnterFrame($str:String):Void { trace($str); };
    20   * }
    21   */
    22  class com.sekati.utils.Delegate extends Object {
    23  
    24  	private var func:Function;
    25  
    26  	/**
    27  	 * Constructor
    28  	 * @param f (Function)
    29  	 * @return Void
    30  	 */
    31  	function Delegate(f:Function) {
    32  		func = f;
    33  	}
    34  
    35  	/**
    36  	 * Creates a functions wrapper for the original function so that it runs in the provided context.
    37  	 * @param obj (Object) Context in which to run the function.
    38  	 * @param func (Function) Function to run.
    39  	 * @return Function
    40  	 */
    41  	static function create(obj:Object, func:Function):Function {
    42  		var f:Function = function():Function {
    43  			var target:Object = arguments.callee.target;
    44  			var func:Function = arguments.callee.func;
    45  			var args:Array = arguments.callee.args;
    46  			return func.apply( target, args.concat( arguments ) );
    47  		};
    48  		f.target = arguments.shift( );
    49  		f.func = arguments.shift( );
    50  		f.args = arguments;
    51  		return f;
    52  	}
    53  
    54  	/**
    55  	 * create wrapper
    56  	 * @param obj (Object) Context in which to run the function.
    57  	 * @return Function
    58  	 */
    59  	function createDelegate(obj:Object):Function {
    60  		return create( obj, func );
    61  	}
    62  }