Hi,


I have slightly modify the Joey's version.
I use also the Array.slice which is faster than the loop.

For the last arguments, it's a mean to get a reference added as a end param to the callback handler.
Easier to dereference after.

Note the return at the end of the functions.
French comments for inside for more insights.

//!-- UTF8
/*
---------------------------------------------------------
        Proxy
        
        package : com.person13.ascb.util
        Description :
                Sert d'intermédiaire pour la délégation de référence de 
fonction.
                Cas d'un callback avec nécessité de portée spécifiqye.
---------------------------------------------------------
   ##++
   ##    Copyright (c) 2005
   ##    http://www.v-i-a.net
   ##    All Rights Reserved
   ##
   ##    E-Mail: [EMAIL PROTECTED]
   ##
## Permission to use, copy, modify and distribute is hereby granted,
   ##    providing that no charges are involved and the above  copyright
## notice and this permission appear in all copies and in supporting ## documentation. Requests for other distribution rights, including ## incorporation in commercial products, such as books, magazine
   ##    articles, or CD-ROMS should be made to the authors.
   ##
## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of
   ##    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   ##--

   For complete Licence of the Creative Commons
   Attribution-NonCommercial-ShareAlike 2.0
   See : http://creativecommons.org/licenses/by-nc-sa/2.0/
---------------------------------------------------------
        version history :
                                1.0 : 17 juin 2005
                                        - Reprise du code de Joey Lott
                                1.1 : 04 juillet 2005
                                        - Rajout d'un retour pour les fonctions 
procurées.
                                        - Rajout d'un nouvel argument pour 
déréférencer le proxy
                                        par la suite.
---------------------------------------------------------

/**
 *      Utile pour créer une référence de callback.
 *      - pour un observateur sur une fonction (listener function)
 *      - pour un callback d'un classe composant une autre classe
 *
* La fonction callback recevra une référence à la fonction Proxy en dernier paramètre. * Il est ainsi possible d'utiliser EventSource.removeEventListener ("EventName", ProxyRef)
 *      et de supprimer la référenec au proxy.
 *
 *      @author Joey Lott http://www.person13.com
 *      @author erixtekila copyleft http://www.v-i-a.net
 *      @version 1.1
 */
class com.person13.ascb.util.Proxy
{
        /**
         *      Méthode factory pour appliquer une référence à un callback
* Note : Le dernier paramètre envoyé à la méthode proxiée et une référence au proxy.
         *      Ainsi, depuis celle-ci il est possible de le déréférencer.
         *      cf http://dynamicflash.com/2005/02/delegate-class-refined
         *      
         *      @param oTarget          Objet cible du callback.
         *      @param fFunction                Callback.
         *      @return Une référence à la fonction Proxy
         */
public static function create (oTarget:Object, fFunction:Function):Function
        {
                /* Modifié de l'original
                var aParameters:Array = new Array();
                for(var i:Number = 2; i < arguments.length; i++) {
                        aParameters[i - 2] = arguments[i];
                }*/
                // Paramètres à passer au callback
                var aParameters:Array;
                aParameters = arguments.splice (2);
                
                // EventProxy, référence à une fonction
                var fProxy:Function = function ():Object
                {
                        // Paramètres
                        var aActualParameters:Array = arguments.concat 
(aParameters);
                        // Rajout de la référence au Proxy à la fin des 
paramètres
                        /**
                         * Permet de déréférencer la proxy depuis la fonction 
soumise
* Exemple : cf http://dynamicflash.com/2005/02/delegate-class-refined
                         *
                         *      public function 
DFDelegateTest(textArea:TextArea) {
                         *              _textArea = textArea;
* _textArea.addEventListener("change", Delegate.create(this, onTextAreaChange));
                         *      }
                         *
* private function onTextAreaChange(event:Object, delegate:Function) : Void {
                         *              _textArea.removeEventListener("change", 
delegate);
                         *      }
                         */
                        aActualParameters.push (arguments.callee);
                        // Activation
// Rajout d'un return pour correspondre au fonctionnement de mx.util.Delegate.
                        return fFunction.apply (oTarget, aActualParameters);
                };

                return fProxy;
        }

}


-----------
erixtekila
http://blog.v-i-a.net/
_______________________________________________
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com

Reply via email to