[xinf] DOM Events?
iiley
iiley.chen at gmail.com
Thu Jun 22 22:31:17 CEST 2006
You are the hero, Nicolas, i think i fall in love with haXe now, i
should start porting AsWing to haXe with Firdosh together ASAP, since
everything we hope of haXe it now has, the Optional Arguments, and
then such a good way of event system will very suitable for
AsWing(even since we do not need bubbling setting).
Cool~~~~~~~~~~~~~
BTW:Will you consider the "override" thing i posted in another
thread?(Too much desire of me?)
2006/6/23, Nicolas Cannasse <ncannasse at motion-twin.com>:
> > In my opinion both the two way avoid class explosion of
> > ListenerInterfaces(like ActionListener, MouseListener,
> > ComponentListener, many many listener interface in Java Swing), but
> > there are still many Event classes too, i'm new to haXe, just
> > wondering is there a way avoid Event class explosion instead of
> > handler functions like:
>
> My take on classless Events. I like a lot this solution. It's
> lightweight and Data-centric. And flexible since you don't have to
> extend/implement any class. However it's a bit more limited since it can
> only handle Bubbbling as a return true/false statement (false stop
> bubbling).
>
> What do you guys think about it ?
>
> Nicolas
>
>
>
> /// FRAMEWORK
>
> signature EventHandler<Target,Data> {
> function handleEvent( t : Target, d : Data ) : Bool;
> }
>
> class EventListener<T,D> {
> var f : T -> D -> Bool;
> public function new(f) {
> this.f = f;
> }
> public function handleEvent( t : T, d : D ) : Bool {
> return f(t,d);
> }
> }
>
> class EventSite<T,D> {
>
> private var listeners : List<EventHandler<T,D>>;
>
> public function new() {
> listeners = new List();
> }
>
> public function addHandler( f : T -> D -> Bool ) {
> addListener(new EventListener(f));
> }
>
> public function addListener( e : EventHandler<T,D> ) {
> listeners.add(e);
> }
>
> public function removeListener( e : EventHandler<T,D> ) : Bool {
> return listeners.remove(e);
> }
>
> public function dispatchEvent( target : T, data : D ) {
> for( l in listeners )
> if( !l.handleEvent(target,data) )
> return false;
> return true;
> }
>
> }
>
> // APPLICATION
>
> signature Rect {
> var x : Int;
> var y : Int;
> var width : Int;
> var height : Int;
> }
>
> class Component {
>
> public var onRefresh(default,null) : EventSite<Component,Rect>;
>
> public function new() {
> onRefresh = new EventSite();
> }
>
> }
>
> class Button extends Component {
>
> public var onClick(default,null) : EventSite<Button,{ x : Int, y : Int }>;
>
> public function new() {
> super();
> onClick = new EventSite();
> }
>
> public function click() {
> onClick.dispatchEvent(this,{ x : 0, y : 0 });
> }
>
> }
>
>
> class Application {
>
>
> public function new() {
> var b = new Button();
> // we have an handleEvent (must be public)
> b.onRefresh.addListener(this);
> // create a listnener
> var l = new EventListener(handleClick);
> b.onClick.addListener(l);
> // a listener can be removed
> b.onClick.removeListener(l);
> // adding with addHandler cannot be removed
> b.onClick.addHandler(handleClick);
> }
>
> function handleClick( target : Button, data : { x : Int, y : Int } ) {
> return true;
> }
>
> public function handleEvent( target : Component, data : Rect ) {
> return false;
> }
>
> }
>
>
>
>
> --
> xinf is not flash
> http://xinf.org/
>
>
>
--
iiley
AsWing http://www.aswing.org
Blog http://spaces.msn.com/members/iiley/
More information about the xinf
mailing list