Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggetion about event. #11389

Closed
mdmunir opened this issue Apr 20, 2016 · 5 comments
Closed

Suggetion about event. #11389

mdmunir opened this issue Apr 20, 2016 · 5 comments

Comments

@mdmunir
Copy link
Contributor

mdmunir commented Apr 20, 2016

Reopen old issue #3243. Maybe for 2.1

Event as jquery like

1. Trigger event using name or object (break BC? yes/no).

Currently, to send event object, it must be as as second parameter of trigger(). But, event object itself contain name property. Better first parameter can be event name or event object.

// use
Yii::$app->trigger('myEvent');
// or use
$myEvent = new Event('myEvent');
Yii::$app->trigger($myEvent);

2. Use return false; or $event->stopPropagation() to stop handling (break BC? no).

Currently to stop event handling we use $event->handled = true. Prefer using return false.

Yii::$app->on('myEvent', function($event){
    //...
    if ($condition){
        return false;
        // or $event->stopPropagation();
        // instead of $event->handled = true;
    }
});

Yii::$app->trigger('myEvent');

3. Add extra parameter when trigger event (break BC? no).

Yii::$app->on('myEvent', function($event, $param1, $param2){
    // do with $param1 and $param2
});

Yii::$app->trigger('myEvent',['param1', 'param2']);
@samdark
Copy link
Member

samdark commented Apr 20, 2016

There's going to be an event manager PSR in the works soon: https://groups.google.com/forum/#!topic/php-fig/MFL__YvumkE

@samdark
Copy link
Member

samdark commented Apr 20, 2016

Here: https://github.com/bretterer/fig-standards/blob/event-manager/proposed/event-manager.md

It totally makes sense so I'd adopt it.

@mdmunir
Copy link
Contributor Author

mdmunir commented Apr 20, 2016

hmm... What target actually is? What different with sender? If event only triggered from Event Manager, i am affraid we must adopt namespace.

@samdark
Copy link
Member

samdark commented Apr 20, 2016

PSR doesn't force you to name events in any certain way but yes, I guess it would lead to names like namespace.eventName. Target serves the same purpose as sender. You can see targets in JavaScript DOM events everywhere. Usually it is what triggered the event.

@wensiti
Copy link

wensiti commented Jun 13, 2016

@mdmunir I like this way ,Laravel's event is implemented

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants