简体   繁体   中英

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM