在同一个 class 的函数中调用一个 class 的函数

Call a function of a class, inside a function of the same class

我有以下 coffeescript class:

class Teams
  rankings: ->
    this.nav_tabs_ajax_calls()

    window.history.pushState(
      '',
      '',
      CoffeeRoutes.path('rankings_team', { 'id': this.team_id() })
    )

    $('li.active').removeClass('active')
    $('li:has(a[href="#rankings"])').addClass('active')

  exercises: ->
    this.nav_tabs_ajax_calls()

    window.history.pushState(
      '',
      '',
      CoffeeRoutes.path('exercises_team', { 'id': this.team_id() })
    )

    $('li.active').removeClass('active')
    $('li:has(a[href="#exercises-list"])').addClass('active')

    $(document).on 'click', '#add-exercise', ->
      showModal("exercises", false, createModal);

    createModal("exercises");

  users: ->
    window.history.pushState(
      '',
      '',
      CoffeeRoutes.path('users_team', { 'id': this.team_id() })
    )

    $('li.active').removeClass('active')
    $('li:has(a[href="#enrolled-students"])').addClass('active')

  graph: ->
    window.history.pushState(
      '',
      '',
      CoffeeRoutes.path('graph_team', { 'id': this.team_id() })
    )

    $('li.active').removeClass('active')
    $('li:has(a[href="#graph"])').addClass('active')

    initialize_graph();

    $('#pause-resume').click ->
      if $('#pause-resume i').attr('class') == "fa fa-pause"
        pause()
        $('#pause-resume i').attr('class', 'fa fa-play')
        $('#pause-resume i').attr('title', 'Resume graph animation')

      else
        resume()
        $('#pause-resume i').attr('class', 'fa fa-pause')
        $('#pause-resume i').attr('title', "Stop graph animation")


     $('#back-center').click ->
       reset()

     $('#remove-graph').click ->
       dispose()

     $(document).on 'click', '#add-nodes', ->
       showModal('search', false, createModal)

     $(document).on 'click', '#search-btn', ->
       div = $(document.createElement('div'))
       div.attr('id', 'loading-modal')
       $('.modal-content').append(div)

  team_id: ->
    $('#show-team').data('team-id')

  nav_tabs_ajax_calls: ->
    $('a[href="#rankings"]').click ->
      $.ajax CoffeeRoutes.path('rankings_team', { 'id': this.team_id() })
         type: 'GET',
         dataType: 'script'

    $('a[href="#exercises-list"]').click ->
      $.ajax CoffeeRoutes.path('exercises_team', { 'id': this.team_id() })
         type: "GET",
         dataType: 'script'

    $('a[href="#enrolled-students"]').click ->
      $.ajax CoffeeRoutes.path('users_team', { 'id': this.team_id() })
         type: "GET",
         dataType: 'script'

    $('a[href="#graph"]').click ->
      $.ajax CoffeeRoutes.path('graph_team', { 'id': this.team_id() })
         type: "GET",
         dataType: 'script'

my nav_tabs_ajax_calls 函数中我收到以下错误(例如,如果我调用 rankings 函数):Uncaught TypeError: this.team_id is not a function(…).

从我的函数中删除 this.nav_tabs_ajax_calls(),它工作正常,在我的其他函数中调用 this.team_id() 没有错误。

我做错了什么,我该如何解决?

内部函数需要使用=>

内部函数,其中->是默认绑定到undefined的普通函数。 使用 =>,您将它与函数实例化上下文的 this 值绑定。

检查这个: Call method in class from another method in same class which is running inside a instance function

"this" 上下文可能是全局 Window,因为 "this.team_id()" 实际上是从 "click" 事件回调中调用的。您可以在附加 "click" 事件侦听器之前捕获 team_id,也可以代理 "click" 回调函数。

nav_tabs_ajax_calls: ->
tID = this.team_id();
$('a[href="#rankings"]').click ->
  $.ajax CoffeeRoutes.path('rankings_team', { 'id': tID })
     type: 'GET',
     dataType: 'script'
// etc. replacing "this.team_id()" with "tID"

或者(我不是 coffeescript 专家,所以这可能不是正确的语法)

$('a[href="#rankings"]').click ->
  $.proxy($.ajax(CoffeeRoutes.path('rankings_team', { 'id': tID }), {
     type: 'GET',
     dataType: 'script'
  }, this)
// etc.