Event handlers

A function that deals with DOM events is called an “event handler”. Event handlers are defined as follows:

  1. <login>
  2. <form onsubmit={ submit }>
  3. </form>
  4. <script>
  5. export default {
  6. // this method is called when above form is submitted
  7. submit(e) {
  8. }
  9. }
  10. </script>
  11. </login>

Attributes beginning with “on” (onclick, onsubmit, oninput, etc.) accept a function value which is called when the event occurs. This function can also be defined dynamically with an expression. For example:

  1. <form onsubmit={ condition ? method_a : method_b }>

All the event handlers are auto-bound and this refers to the current component instance.

So you can also call a function dynamically from a property value like this:

  1. <button onclick={ this[props.myfunc] }>Reset</button>

Event handlers do not update components so you might combine them with a this.update() call:

  1. <login>
  2. <input value={ state.val }/>
  3. <button onclick={ resetValue }>Reset</button>
  4. <script>
  5. export default {
  6. state: {
  7. val: 'initial value'
  8. },
  9. resetValue() {
  10. this.update({
  11. val: ''
  12. })
  13. }
  14. }
  15. </script>
  16. </login>