2017-07-01 41 views
0

我寫了波紋管代碼。我想回調方法沒有通過coffeescript返回

initialize : -> 
@model.apiForecast = new ApiForecastModel(
    model: @model.get('apiForecast') 
) 
@model.forecast = new ForecastModel(
    model: @model.get('forecast') 
) 
cookie = Cookie() 
forecastCall = this.model.forecast.fetch(
    data: 
    token: cookie['Authorization'] 
    headers: 
    Authorization: cookie['Authorization'] 
    success: -> 
    console.log('Success Forecast') 
    error: (e) -> 
    console.log('Service request failure: ' + e) 
) 

$.when(forecastCall) 
.done(() -> (
    @getApiForecast() 
    return 
).bind(@) 
    return 
) 
return 

但是然後我得到了這個錯誤。

error: unexpected indentation

其實我想編譯成這樣的ajax代碼。

$.when(forecastCall).done(
    function() { 
    this.getApiForecast(); 
    }.bind(this) 
); 

你有什麼決議?

回答

0

你在那個bind電話上班的地方有你的括號。你想換行括號中的整個匿名函數,而不只是函數體:

$.when(forecastCall) 
.done((-> 
    @getApiForecast() 
    return 
).bind(@)) 

或以上(或至少減少噪音),使用=>功能,讓CoffeeScript中採取結合的護理:

$.when(forecastCall).done(=> 
    @getApiForecast() 
    return 
) 

我假設您的問題中的所有代碼實際上都在initialize之內。