nuway
nuway

Reputation: 2394

calling a method from jquery callback with coffeescript

In coffescript, I want to call a class method from a jquery callback, how do I access the class's scope? this.loadImage(currentIndex) doesnt work

class ImageCarousel    

    currentIndex = 0
    jsonPath = "json/images.json"
    images = null  


    constructor: () ->

       this.loadJson()


loadJson: () ->

    $.ajax jsonPath,
        success  : (data, status, xhr) ->
            console.log("yea "+data)
            this.images = data.images
            this.loadImage(currentIndex)
        error    : (xhr, status, err) ->
            console.log("nah "+err)
        complete : (xhr, status) ->
            console.log("comp")




loadImage:(@index) ->

    console.log("load image "+@index)

Upvotes: 1

Views: 1269

Answers (1)

acjay
acjay

Reputation: 36521

You need to use the => operator for your callback. You might find this info helpful.

Upvotes: 4

Related Questions