Skip to content

Instantly share code, notes, and snippets.

@skyrpex
Last active October 6, 2015 09:51
Show Gist options
  • Save skyrpex/9c769394e3554f6abac0 to your computer and use it in GitHub Desktop.
Save skyrpex/9c769394e3554f6abac0 to your computer and use it in GitHub Desktop.
Vue Directives: Prevent Default
const eventNames = {
BUTTON: 'click',
A: 'click',
FORM: 'submit',
};
export default {
bind() {
const eventName = eventNames[this.el.nodeName];
if (!eventName) {
throw 'Prevent Default: Unhandled element';
} else {
this.el.addEventListener(eventName, e => e.preventDefault());
}
},
};
var eventNames = {
BUTTON: 'click',
A: 'click',
FORM: 'submit'
};
module.exports = {
bind: function bind() {
var eventName = eventNames[this.el.nodeName];
if (!eventName) {
throw 'Prevent Default: Unhandled element';
} else {
this.el.addEventListener(eventName, function (e) {
return e.preventDefault();
});
}
}
};
@yyx990803
Copy link

One small thing: you can omit the update function if you don't need it. Actually it's preferred to omit it so that Vue knows the directive is non-reactive.

@skyrpex
Copy link
Author

skyrpex commented Oct 6, 2015

Perfect! Thanks for the tip.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment