 
 
   initializeUpdate: (e) =>
-    e.currentTarget.debouncedUpdate ?= _.debounce @update, 1000
-    e.currentTarget.debouncedUpdate e
+    form = e.currentTarget
+
+    @abortUpdate form
+    form.debouncedUpdate ?= _.debounce @update, 1000
+    form.debouncedUpdate form
 
 
   ajaxError: (e) =>


class @AccountEdit
  constructor: ->
    $(document).on 'input', '.js-account-edit__input', @initializeUpdate

    $(document).on 'ajax:error', '.js-account-edit', @ajaxError
    $(document).on 'ajax:send', '.js-account-edit', @ajaxSaving

    el.dataset.accountEditState = 'saving'


  abortUpdate: (form) =>
    Timeout.clear form.savedTimeout
    Timeout.clear form.savingTimeout
    form.updating?.abort()
    @clearState form


  update: (form) =>
    input = form.querySelector('.js-account-edit__input')
    value = input.value
    prevValue = form.dataset.lastValue

    return if value == prevValue

    form.dataset.lastValue = value

    form.savingTimeout = Timeout.set 1000, =>
      @saving form

    form.updating = $.ajax laroute.route('account.update'),
      method: 'PUT'
      data:
        "#{input.name}": value

    .done =>
      @saved form

    .fail (xhr, status) =>
      return if status == 'abort'

      form.lastValue = prevValue
      osu.ajaxError xhr

    .always =>
      Timeout.clear form.savingTimeout
