Skip to content

Instantly share code, notes, and snippets.

@olanb7
Last active August 29, 2015 14:06
Show Gist options
  • Save olanb7/784a7bb491cd6fb8004f to your computer and use it in GitHub Desktop.
Save olanb7/784a7bb491cd6fb8004f to your computer and use it in GitHub Desktop.
TrainingPeaks UI workoutCommentView bug

Problem: Often when editing comments the post-workout input box loses focus after a short period of time. For example, if I've just paused from typing for a few seconds.

Looking at the JS, there appears to be a timer to autosave comments every 2s.

views/workoutCommentsEditor/workoutCommentView

    onCommentBodyChanged: function() {
      this.saveTimeout && clearTimeout(this.saveTimeout)
      var a = this
      this.saveTimeout = setTimeout(function() {
          var b = c.utils.conversion.parseUnitsValue("text", a.ui.editedComment.val())
          null !== b && b !== a.model.get("comment") && (a.model.set("comment", b, {
              silent: !0
          }), a.trigger("commentedited"))
      }, 2e3)
    }

The 'commentedited' event ultimately triggers a model.autosave.

views/workoutCommentsEditor/workoutCommentsEditor

    saveComments: function() {
        this.model.set("workoutComments", this.getCommentsAsArray(), {
            silent: !0
        }), this.model.autosave({})
    },

However in some instances this is also triggering a 'blur' event, as the following method (which is triggered by a blur) is called.

views/workoutCommentsEditor/workoutCommentView

    onCommentBodyBlur: function() {
        if (this.saveTimeout && clearTimeout(this.saveTimeout), this.editable) {
            var a = c.utils.conversion.parseUnitsValue("text", this.ui.editedComment.val())
            null !== a && a !== this.model.get("comment") && (this.model.set("comment", a), this.trigger("commentedited")), this.editable = !1, this.template.template = f, this.render(), this.$el.removeClass("editable")
        }
    },

To reproduce, click into an existing workout comment, press key down until the end of the comment and press enter a few times. Wait 2s, and the comment box will lose focus. Annoying.

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