在同一个类的函数内调用类的函数

时间:2017-05-21 03:32:44

标签: javascript coffeescript

我有以下coffeescript课程:

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()时没有错误。

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

2 个答案:

答案 0 :(得分:1)

您需要使用=>作为内部功能。

内部函数,->是默认情况下绑定到未定义的普通函数。 使用=>,可以将其与函数实例化上下文的this值绑定。

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

答案 1 :(得分:1)

“this”上下文可能是全局窗口,因为“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"

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

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