Skip to content

Suggestion: firing an event among siblings and globally. #1768

@kfirba

Description

@kfirba

Hey.

Currently we have the $emit method to fire an event in the VM itself, $dispatch method to fire an event up the parent chain and the $broadcast method to fire an event down the children chain.

Many times I find myself having a "manager" like instance which has multiple children and when one children changes it should notify its siblings for that. Imagine we have component A, B, and C where A is the "manager" and B and C are its children. Now if C changes, I would like to let B know of that and maybe do something with that. Currently I do this in a tricky way:

// run this in `C` component
this.$parent.$broadcast('MyEvent', data)

Now B will indeed get the message that something happened with its sibling.

I was wondering if maybe we can create a native method for that since it seems to be a common task? It can be just a wrapper method for this.$parent.$broadcast.

Also, currently we can't fire an event globally that ANY vue instance that is "alive" can listen for. If we want to do so we need to trigger all of the events method:

this.$emit('EventOccured', data)
this.$broadcast('EventOccured', data)
this.$dispatch('EventOccured', data)

Maybe we can add some method to handle this as-well? I honestly haven't had any occurrence where I had to do so but it seems like something that should be there.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions