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

how to catch all events? #230

Closed
twarogowski opened this issue Apr 10, 2014 · 14 comments
Closed

how to catch all events? #230

twarogowski opened this issue Apr 10, 2014 · 14 comments

Comments

@twarogowski
Copy link

Hi,

if there a possibility to use VM $on() method to catch all incoming events?
It would be even cooler to enable wildcards like:

this.vm.$on('delete-*', function(){
//...
});

to catch all events starting with 'delete-'

Right now such functionality is useful for me for diagnostics, but I think it would be useful in several other scenarios as well.

@twarogowski
Copy link
Author

just a thought, maybe regex fits better in this case than wildcards :)

@yyx990803
Copy link
Member

Here's a pretty hacky way to do it (if you take a look at src/emitter.js it should be fairly easy to understand)

var oldEmit = this.compiler.emitter.emit
this.compiler.emitter.emit = function () {
    console.log('got event: ' + arguments[0])
    oldEmit.apply(this, arguments)
}

Introducing wildcards/regex into the emitter implementation would bring about performance concerns, since the emitter is very central in the observation implementation and emit() is called a lot.

@twarogowski
Copy link
Author

Thanks, this kind of hacky solution is good enough for me 😄

@endoplasmic
Copy link

endoplasmic commented May 18, 2016

For those looking to do the same thing with version 1.0+, the syntax is like this:

const old_on = this.$on;
this.$on = (...args) => {
  // custom logic here like pushing to a callback array or something
  old_on.apply(this, args);
};

@xiaolp
Copy link

xiaolp commented Jul 20, 2017

example for version 2.* ?

@endoplasmic
Copy link

Did you try that code? Should still work :)

@njleonzhang
Copy link

@endoplasmic your solution can also work for 2.0

@endoplasmic
Copy link

@njleonzhang ya, I edited my comment above to say 1.0+ so if others stumble on this, it's a bit more clear :)

@njleonzhang
Copy link

@endoplasmic It's great and has no dependence to Vue implementation

@xiaolp
Copy link

xiaolp commented Jul 21, 2017

@njleonzhang any example for this ? it did not work at my side.

@njleonzhang
Copy link

@xiaolp endoplasmic has provided sample

@phyrun
Copy link

phyrun commented Nov 30, 2017

i very stress

@souse
Copy link

souse commented May 6, 2019

For those looking to do the same thing with version 1.0+, the syntax is like this:

const old_on = this.$on;
this.$on = (...args) => {
  // custom logic here like pushing to a callback array or something
  old_on.apply(this, args);
};

i add this code into beforeCreate but not work

@javastation
Copy link

Is there a way to catch all click events

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants