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

Dispatch & Broadcast API #43

Closed
wants to merge 5 commits into from

Conversation

Projects
None yet
4 participants
@wspl
Copy link

commented Jun 10, 2019

A proposal that bring back the $dispatch and $broadcast APIs. I am still waiting for new features of Vue 3.0 to make this API more elegant.

Rendered

@posva

This comment has been minimized.

Copy link
Member

commented Jun 10, 2019

Hi thanks for the detailed proposal but we removed them for a reason. Please see https://vuejs.org/v2/guide/migration.html#dispatch-and-broadcast-replaced for a detailed explanation.

We prefer a data-driven approach whenever possible because it makes everything easier to reason about and to debug.
However, if in your specific application, dispatch/broadcast fits better, you can always implement it to use it and you showed in the Element codebase and I think that is how it should be because of the impacts mentioned in the link above

@HerringtonDarkholme

This comment has been minimized.

Copy link
Member

commented Jun 10, 2019

Similar functionality can be implemented via provide/inject. We can inject an event emitter to every component that needs to listen dispatched events.
Injection based event emitter is better since it supports namespaced events out of box (just add a new provide/inject symbol). And we can debug where the event might be emitted by injection symbol instead of grepping some event string.

@wspl

This comment has been minimized.

Copy link
Author

commented Jun 10, 2019

Similar functionality can be implemented via provide/inject. We can inject an event emitter to every component that needs to listen dispatched events.
Injection based event emitter is better since it supports namespaced events out of box (just add a new provide/inject symbol). And we can debug where the event might be emitted by injection symbol instead of grepping some event string.

I used to use provide and inject to implement this feature. It works for calling the method of parent/grandparent component from child component (without bubbling support), but the reverse is very troublesome (if it encounter a component structural change (add, remove, cross-level move) or go through a vue-portal component, this is no longer applicable.)

I think this is a very common feature. It does have a bunch of alternatives, but it would be great if it works out of the box.

@Nandiin

This comment has been minimized.

Copy link

commented Jun 18, 2019

While broadcast/dispatch pattern does have significant value in certain usecases, it should be kept away from the core library for following reasons:

  1. Better solutions, like Vuex, are available if arbitrary component communication is necessary, without the brittleness that broadcast/dispatch brings.

  2. broadcast/dispatch is easily implementable on userland.

    In Vue 2.x, using a external api like EventBus may seem exotic since all the official apis either are bound on this or have a place on component options. However, with the introduction of Function-based Component API, use of an external EventBus like api would seem much more consistent with the Vue official apis.

@wspl wspl closed this Jun 18, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.