After I instantiate my class, I call myClass.testScope(), which calls another function inside the class. But, when I pass the function as a parameter, I lose scope and calling testScopeFromPassedParam from data1 results in the error Uncaught TypeError: Object [object global] has no method 'testScopeFromPassedParam'
Can someone please aid in the best way to handle this?
class MyClass
test: () ->
@testScope()
testScope: () ->
console.log 'worky'
testScopeFromPassedParam: () ->
console.log 'no worky'
data1: (cb) ->
# Shoot. The error.
@testScopeFromPassedParam()
setTimeout (->
cb '1'
), 1000
data2: (cb) ->
setTimeout (->
cb '2'
), 3000
loadData: () ->
getDeferred = (someFunction) ->
deferred = $.Deferred()
someFunction (data) ->
console.log data
deferred.resolve()
deferred
dataFunctions = [
@data1
@data2
]
arrayOfPromises = [ ]
for someFunction in dataFunctions
arrayOfPromises.push getDeferred(someFunction)
$.when.apply(null, arrayOfPromises).done () =>
alert 'returned'
myClass = new MyClass()
myClass.testScope()
myClass.loadData()