-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
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
feat(core): add 'Vue.prototype.$notify' #7465
Conversation
I'm not a huge fan of this because it is impossible to know the order in which the callbacks get executed. When I encounter a situation where I want to get something back from an // callback can be called multiple times by different consumers...
this.$emit("get-something", arg, (value) => {
// Do stuff with value
}) Use it like so: this.$on("get-something", (arg, done) => {
// Send a value back to the emitting component
if (done) done("hello from consumer")
}) This manner creates a more predictable outcome where the developer decides whether to send a value back. It also allows for an alternative situation where you can only listen for a single value using a deferred like so: var callback
const deferred = new Promise((resolve) => { callback = resolve })
// callback will only save the value of the first caller
this.$emit("get-something", arg, callback)
deferred.then((value) => {
// Do something with resolved value
}) |
Thanks for the PR - however, I agree w/ @sirlancelot and I think explicit callbacks is more straightforward. |
It's 2018. I don't think callbacks are a good design anymore. What I want to write is: const values = await Promise.all(vm.$emit('event', arg)) Instead of var promises = []
vm.$emit('event', arg, promise => {
promises.push(promise)
})
const values = await Promise.all(promises) returning vm itself for $emit is almost useless. It's not jQuery or lodash and you don't need to chain the operations in 99% cases (there's no UT to ensure this by the way). But it's a common use case that returning values from event handler, for example filtering list remotely. Using return value instead of vender callbacks is much easier to use and a rather modern design nowadays.
Values in the returned array of What you get using callback is just If you do, just fall back to callbacks. @yyx990803 @sirlancelot any thoughts? |
Well... It's 2018. I don't think event-ping-ponging is a good design anymore. It makes the flow of your application very hard to reason about for those who are not familiar with the codebase. I don't want to encourage this kind of patterns. If you insist, you don't have to get this into Vue core to use it. Just write your own plugin. |
I agree. But it's not avoidable. There are lots of similar real requirements need to be resolved.
The element-ui developers chose to use binding function as property. I ( and maybe others ) used Now I'm facing this problem. I have chosen to use binding function too but I think things can be better.
Ok. |
What kind of change does this PR introduce? (check at least one)
Does this PR introduce a breaking change? (check one)
If yes, please describe the impact and migration path for existing applications:
The PR fulfills these requirements:
dev
branch for v2.x (or to a previous version branch), not themaster
branchfix #xxx[,#xxx]
, where "xxx" is the issue number)If adding a new feature, the PR's description includes:
Other information:
This PR adds a new function
Vue.prototype.$notify
, which works basically like$emit
, except it returns an array that collects all return values of its event callbacks.Motivation:
Developers of an component may want to collect data from callbacks of an event. There are a few options to do this:
To solve this issue, I'd like
$emit
or something else returns an array that collects all return values of its event callbacks. In order not to break old code, the behavior of$emit
was kept ( which returns this ), but I wrote a new function instead. The function name$notify
is not decided, but I can't come up with a better name.Related issue: #5443
Related question: https://segmentfault.com/q/1010000011143372