     })
 
     @showingVisualEditor = true
-    @element.on('click', '.editor-tabs .tab', @onSwitchEditor)
+    @element.on('click', '.editor-tabs .tab', this, @onSwitchEditor)
 
   @setupTinyMCE: (ed) ->
     ed.addButton('wrapAsCode', {

    e.preventDefault();

    if not $(e.currentTarget).hasClass('current')
      $(e.currentTarget).addClass('current')
      $('table.mceToolbar', @element).toggleClass(HTMLEditingDescriptor.isInactiveClass)
      $(@advanced_editor.getWrapperElement()).toggleClass(HTMLEditingDescriptor.isInactiveClass)

      visualEditor = @getVisualEditor()
      if $(e.currentTarget).attr('data-tab') is 'visual'
        $('.html-tab', @element).removeClass('current')
        @showVisualEditor(visualEditor)
      else
        $('.visual-tab', @element).removeClass('current')
        @showAdvancedEditor(visualEditor)

  # Show the Advanced (codemirror) Editor. Pulled out as a helper method for unit testing.

  focusVisualEditor: (visualEditor) ->
    visualEditor.focus()

  getVisualEditor: (element) ->
    ###
    Returns the instance of TinyMCE.
    This is different from the textarea that exists in the HTML template (@tiny_mce_textarea.
    ###
    return tinyMCE.get($(element).find('.tiny-mce').attr('id'))

  save: ->
    @element.off('click', '.editor-tabs .tab', @onSwitchEditor)
    text = @advanced_editor.getValue()
    visualEditor = @getVisualEditor(@element)
    if @showingVisualEditor and visualEditor.isDirty()
      text = visualEditor.getContent({no_events: 1})
    data: text
