This is my first question here, though I’m frequent visitor. I tried to find answers on my own to this but no joy.
So, I have a class in coffescript (Rails 3.1) like this:
root = global ? window
class root.CTimelineGraph
constructor: (div, @w, @nw, @table_name) ->
@columnNames = ""
@dbReadTable(@table_name)
..
dbReadTable: (table_name) ->
if table_name == "SOMETABLE" then @dbReadTableA1()
dbReadTableA1: ->
sipa = ""
$.ajax
async: false
type: "GET"
url: "ajax/getcolumnnames"
dataType: 'json'
success: (data) ->
sipa = data
@columnNames = sipa
My question is, how can I populate @columnNames (declared in the constructor) with the data that I get back from ajax/getcolumnnames controller? I’m using third variable sipa to get data out of success function. Am I doing this wrong?
The code I posted is working. I’m asking the question because I intend to have many instances of this class all calling dbReadTable a lot.
Thanks.
Here’s a better way, without the extra variable:
The key is the double arrow which binds
thisto your class instance in thesuccesscallback, which makes @columnNames available inside the function. This also assumes that dbReadTableA1 is declared as a member function in the class.