2017-07-01 4 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の内部にあると仮定しています。

関連する問題