Heya,

I never quite understood the choice of the Cocoa model for the
EventDispatcher. This isn't exactly widely known by web developers,
and seems absolutely counter intuitive to me.

Therefore I would like to request that at least for Sf2, the
ECMAScript/JavaScript model be adopted instead. I'm sure this is much
more common among web developer, and even to those that don't know it
in detail, it's quite simple to grasp.

I went ahead and committed a php implementation of it in a new branch
in my fork, you can see the details there:
http://github.com/Seldaek/symfony/tree/ecma_events/src/Symfony/Components/EventDispatcher/
- I've had the code for a while and did some quick refactoring of it,
so stuff might be broken, but I want to discuss it before spending
more time on it.

Basically it has the same concept as the Cocoa one in term of having
an Event and EventDispatcher class, but here is how it goes:

Note you can find a copy of this code at http://pastie.org/961908 if
it looks crappy in the email.

<?php

$dispatcher = new EventDispatcher();

// attaching (= connect in cocoa)

// 1 is the priority, so you can be sure to receive before another
// listener to override it
$dispatcher->addEventListener('request', array($this, 'handleRequest'), 1);

// dispatching

// here is where it gets interesting
// notify/notifyUntil in the cocoa version is utterly confusing imo, I never
// found anyone that got it right without reading the docs 3 times and thinking
// about it for a while.
// this version is much more straightforward, any listener can call
// $event->stopImmediatePropagation() to stop it
$dispatcher->dispatchEvent(new Event('request'));

// handling

public function handleRequest($event) {
    // stop the bubbling of the event
    $event->stopPropagation();

    // stop it immediately (won't dispatch to anyone else)
    $event->stopImmediatePropagation();

    // cancel the default action, only available one events instanciated
    // with $cancelable flag set to true. If you do so, the dispatchEvent call
    // will return false to let the caller know that the default action
    // should not take place
    $event->preventDefault();
}

// ------------------------------------------------------------------

It also supports bubbling via nesting of the dispatchers, but since we
do not really have a DOM or any sort of nested structure in PHP it
might make sense to remove this feature altogether and remove
stopPropagation along with it to even simplify things further.

Other than that, I believe this model is really much easier to grasp
and more explicit with direct calls on the event object to stop its
propagation rather than relying on return values.

Feedback appreciated.

Cheers

-- 
Jordi Boggiano
@seldaek :: http://seld.be/

-- 
If you want to report a vulnerability issue on symfony, please send it to 
security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "symfony developers" group.
To post to this group, send email to symfony-devs@googlegroups.com
To unsubscribe from this group, send email to
symfony-devs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/symfony-devs?hl=en

Reply via email to