[英]Call a function of a class, inside a function of the same class
我有以下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()
时没有错误。
我在做什么错,我该如何解决?
您需要对内部函数使用=>
。
内部函数,带有->
是默认情况下绑定到undefined的普通函数。 使用=>,可以将其与函数实例化上下文的this
值绑定。
检查以下内容: 从实例函数内部运行的同一个类中的另一个方法调用该类中的方法
由于“ this.team_id()”实际上是从“ click”事件回调中调用的,因此“ this”上下文可能是全局窗口。 您可以在附加“ 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"
或(而且我不是咖啡专家,所以语法可能不正确)
$('a[href="#rankings"]').click ->
$.proxy($.ajax(CoffeeRoutes.path('rankings_team', { 'id': tID }), {
type: 'GET',
dataType: 'script'
}, this)
// etc.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.