Command: contains bind() for wiring events up commands, without having to

create var-args functions or make your existing functions varargs so that
they can react to events.
This might turn into more. I already did a bunch of impl and then KISS since
we don't use the advanced shit yet. It might be nice to tie this more
closely to CommandButton, et al.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5300 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Ray Greenwell
2008-08-08 02:12:57 +00:00
parent aff425651c
commit 3e5a63c1bc
+75
View File
@@ -0,0 +1,75 @@
//
// $Id$
package com.threerings.util {
import flash.events.Event;
import flash.events.IEventDispatcher;
/**
* Contains a simple binding function to bind events to commands.
*
* TODO: be able to set up a Command object, and bind it to multiple functions
*/
public class Command
{
/**
* Bind an event to a command.
*/
public static function bind (
source :IEventDispatcher, eventType :String, cmdOrFn :Object, arg :Object = null) :void
{
source.addEventListener(eventType, function (... ignored) :void {
CommandEvent.dispatch(source, cmdOrFn, arg);
});
}
/**
* Convenience, since otherwise nobody usually needs to ever import CommandEvent.
*/
public static function dispatch (
source :IEventDispatcher, cmdOrFn :Object, arg :Object = null) :void
{
CommandEvent.dispatch(source, cmdOrFn, arg);
}
// public static function bind (
// source :IEventDispatcher, eventType :String, cmdOrFn :Object, arg :Object = null) :Command
// {
// var cmd :Command = new Command(cmdOrFn, arg);
// cmd.addBind(source, eventType);
// return cmd;
// }
//
// public function Command (cmdOrFn :Object, arg :Object = null)
// {
// _cmdOrFn = cmdOrFn;
// _arg = arg;
// }
//
// public function setOverrideSource (disp :IEventDispatcher) :void
// {
// _source = source;
// }
//
// public function addBind (source :IEventDispatcher, eventType :String) :void
// {
// source.addEventListener(eventType, eventHandler);
// }
//
// public function removeBind (source :IEventDispatcher, eventType :String) :void
// {
// source.removeEventListener(eventType, eventHandler);
// }
//
// protected function eventHandler (event :Event) :void
// {
// dispatch(_source || IEventDispatcher(event.source), _cmdOrFn, arg);
// }
//
// /** An override source for the command events, or null to use the triggering dispatcher. */
// protected var _source :IEventDispatcher;
// protected var _cmdOrFn :Object;
// protected var _arg :Object;
}
}