Reputation: 1168
I have the following 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'
In my nav_tabs_ajax_calls
function I'm receiving the following error (if I call my rankings
function, for instance): Uncaught TypeError: this.team_id is not a function(…)
.
Removing this.nav_tabs_ajax_calls()
from my functions, it works fine, with no errors calling this.team_id()
in my other functions.
What am I doing wrong and how can I fix it?
Upvotes: 0
Views: 63
Reputation: 813
The "this" context will likely be the global Window since "this.team_id()" is actually being called from within the "click" event callback. You can either capture the team_id before attaching the "click" event listeners, or you can proxy the "click" callback functions.
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 (and I'm not a coffeescript expert, so this may not be correct syntax)
$('a[href="#rankings"]').click ->
$.proxy($.ajax(CoffeeRoutes.path('rankings_team', { 'id': tID }), {
type: 'GET',
dataType: 'script'
}, this)
// etc.
Upvotes: 1
Reputation: 81
You need to use =>
for inner functions.
Inner function, with ->
is a normal function bound to undefined by default.
With =>, you bind it with the this
value of the function instantiation context.
check this: Call method in class from another method in same class which is running inside a instance function
Upvotes: 1