Skip to content

Instantly share code, notes, and snippets.

@lynxerzhang
Created March 29, 2013 09:54
Show Gist options
  • Save lynxerzhang/5269949 to your computer and use it in GitHub Desktop.
Save lynxerzhang/5269949 to your computer and use it in GitHub Desktop.
package
{
import flash.events.Event;
import flash.events.EventPhase;
import flash.events.IEventDispatcher;
/**
* @see once$
* @example
* this.stage.addEventListener(MouseEvent.CLICK, event$(function(evt:MouseEvent, value:int):void{
* trace(evt);
* trace(value);
* }, null)(5));
*
* will out put mouseEvent's verbose infomation and a int value 5
*/
public function event$(fun:Function, scope:* = null, once:Boolean = false):Function{
return function(...args):Function{
return function(evt:Event):void{
if(scope){
fun.apply(scope, [evt].concat(args));
}
else{
if(fun.length == 1){
fun(evt);
}
else{
fun.apply(null, [evt].concat(args));
}
}
if(once){
IEventDispatcher(evt.currentTarget).removeEventListener(evt.type, arguments.callee, evt.eventPhase == EventPhase.CAPTURING_PHASE);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment