Skip to content

Be able to pass multiple events the same function in templates #6457

@AlexandreBonneau

Description

@AlexandreBonneau

What problem does this feature solve?

Having multiple event handlers that call the very same function add noise to the templates.
I propose to DRY that a bit.

What does the proposed API look like?

Currently you can pass multiple functions to a single event handler like so:

<component
     @click="func1(); func2()"
></component>

However, I think it would be really handy to be able to do the inverse, ie. passing the same function to multiple event handlers.
Today for instance I have to do this:

<component v-model.number="foo"
    @doDelayedStuff="delayedStuff"
    @wheel.native="delayedStuff"
    @mouseenter.native="callFooUpdate"
    @focus.native="callFooUpdate"
    @keyup.native="callBar"
    @mouseleave.native="callFooClear"
    @blur.native="callFooClear"
    @change.native="callFooClear"
    @click.prevent="doStuff"
    @keyup.75="doStuff"
></component>

It would be great if you could instead do that:

<component v-model.number="foo"
    @[doDelayedStuff, wheel.native]="delayedStuff"
    @[mouseenter.native, focus.native]="callFooUpdate"
    @keyup.native="callBar"
    @[mouseleave.native, blur.native, change.native]="callFooClear"
    @[click.prevent, keyup.75]="doStuff"
></component>

That way you can more clearly see in one glance how many different behavior are attached to a component. Also, modifying the callbacks is easier.

Thoughts? 😄

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions