Last active
October 6, 2015 09:51
-
-
Save skyrpex/9c769394e3554f6abac0 to your computer and use it in GitHub Desktop.
Vue Directives: Prevent Default
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
}, | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.