shaun <[EMAIL PROTECTED]> wrote:

>
http://blog.iconara.net/2007/11/05/a-generic-way-to-remove-an-event-listener/

Thank you gents - that solved my anonymous function problem and
started me down the road of a more generic solution.

Borrowing directly from ExtJS [1] (licenced under GPLv3) and extending
the Function primative, I came up with this:


/**
 * Create a combined function call sequence of the original function +
the passed function.
 * The resulting function returns the results of the original
function. The passed fcn is called
 * with the parameters of the original function.
 * 
 *       Parameters:
 *      
 *          * fcn : Function
 *            The function to sequence
 *          * scope : Object
 *            (optional) The scope of the passed fcn (Defaults to scope of
original function)
 *      
 *      Returns:
 *      
 *          * Function
 *            The new function
 */
Function.prototype.createSequence =
function(fcn:Function,scope:Object=null):Function {
        if(typeof fcn!="function"){
                return this
        }
        
        var C=this;
        return function(){
                var D=C.apply(this,arguments);
                fcn.apply(scope||this,arguments);
                return D
        }
}

And then an alternative addEventListener function which accepts an
options config object:

public function addListener(fn,opts=null) {

    if(opts.single) {
      fn = fn.createSequence(function(event) {
        event.currentTarget.removeEventListener(event.type, fn)
      })
    }   
    myEventBroadcaster.addEventListener(Event.EVENT_TYPE,fn);
}


More information on the ExtJS code can be found in their docs at [2]


[1] http://extjs.com
[2] http://extjs.com/deploy/dev/docs/

Reply via email to