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

Multiple events in single v-on similar to v-bind="object" #5578

Closed
crswll opened this issue May 2, 2017 · 13 comments
Closed

Multiple events in single v-on similar to v-bind="object" #5578

crswll opened this issue May 2, 2017 · 13 comments

Comments

@crswll
Copy link

crswll commented May 2, 2017

What problem does this feature solve?

It could make binding a bunch of events to an element more clear. It would also make v-bind and v-on work similarly.

What does the proposed API look like?

v-on="{ 'click.shift': methodOne, 'keyup.left': methodTwo, 'keyup.right.stop': methodTwo }"

This way we can create a computed list that returns a hash if there's too many event listeners / repetitive events.

I can get something close with a directive but without the modifiers it's not nearly as useful. It also doesn't remove the events but it was a quick attempt.

https://jsfiddle.net/crswll/2fL6gtbg/4/

@nickmessing
Copy link
Member

@crswll, that means that we need to parse all event modifiers in the vue itself as opposite to template compiler at the moment, are you sure that is a good idea?

@crswll
Copy link
Author

crswll commented May 3, 2017

I don't know enough about the Vue internals to answer that well. I guess attempting this feature on my own would be a good way to figure it out.

@lonelyclick
Copy link
Contributor

I think it's too complicated, do we also need to consider custom events?

@ghost
Copy link

ghost commented May 16, 2017

IMO if you do this it should be a separate directive (like v-on-many), but not the native v-on rewritten itself. The reason is v-on-many (if it exists) should also handle custom events as mentioned by @lonelyclick (otherwise there's little sense having such a bulk directive), and all this additional inner-library work is redundant for 99% v-on use cases.

@lukeed
Copy link

lukeed commented May 29, 2017

I think this could be a good fit (and avoid any rewrites) if we followed @wostex's suggestion. Although, a v-events or @events would make most sense in my mind.

This could open the door for passing around event names+handlers, which would shortcut the (sometimes) repetitive template assignments.

@nickmessing
Copy link
Member

@lukeed, I still think this should not be a part of Vue, it should be a separate directive that you can use.

@lukeed
Copy link

lukeed commented May 31, 2017

I not opposed to that idea either 👍

@Kingwl
Copy link
Member

Kingwl commented Jun 23, 2017

the modifier has been parsed when template compiling
if we support modifier we should support parse modifier at runtime
IMO this is not a good idea😅

@ghost
Copy link

ghost commented Jun 23, 2017

I've tried to create such a directive, here's the code: link

Working sample on jsfiddle: link

It should work fine with native events, as described in the question.

P. S. I don't know how to add support for Vue-created custom events though. Maybe someone can point me into right direction?

@yyx990803
Copy link
Member

This is partially implemented in 11614d6 - however, this syntax is introduced primarily for making it easier to proxy events in components. Modifiers for this syntax introduce too much runtime complexity and is not supported.

@vhoyer
Copy link

vhoyer commented Jul 31, 2019

So how do I bind .native events using this technique? like, I tryed using v-on.native="data.nativeOn" and it doesn't seem to work.

I even tried v-native-on="data.nativeOn" but it doesn't work either 😅

Tried v-on="{ ...listeners, data.nativeOn }", also no success

the only thing I could do to manage it to work was to do manually ;-;

v-on="listeners"
@click.native="listeners.click" // cuz if I will need to do this, I might as well just treat click as a component event 

@victorwpbastos
Copy link

Would be great if we could apply modifiers too.

@KarmaBlackshaw
Copy link

So how do I bind .native events using this technique? like, I tryed using v-on.native="data.nativeOn" and it doesn't seem to work.

I even tried v-native-on="data.nativeOn" but it doesn't work either 😅

Tried v-on="{ ...listeners, data.nativeOn }", also no success

the only thing I could do to manage it to work was to do manually ;-;

v-on="listeners"
@click.native="listeners.click" // cuz if I will need to do this, I might as well just treat click as a component event 

any update on this ?

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

10 participants