gpt4 book ai didi

javascript - 在同一个类的函数内部调用一个类的函数

转载 作者:行者123 更新时间:2023-12-01 03:38:11 26 4
gpt4 key购买 nike

我有以下 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'

我的nav_tabs_ajax_calls函数中,我收到以下错误(例如,如果我调用我的rankings函数):Uncaught TypeError:this.team_id is不是一个函数(…)

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

我做错了什么以及如何解决它?

最佳答案

“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"

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

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

关于javascript - 在同一个类的函数内部调用一个类的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44092893/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com