我想回调方法而不用coffeescript返回

时间:2017-07-01 19:57:35

标签: javascript coffeescript

我写了下面的代码。

JSON.parse(string)

但是我得到了这个错误。

  

错误:意外缩进

实际上,我想编译成这样的ajax代码。

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

你有任何决议吗?

1 个答案:

答案 0 :(得分:0)

您的括号位于bind来电的错误位置。您希望将整个匿名函数包装在括号中,而不仅仅是函数体:

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

或更好(或至少噪音更小),使用=>函数让CoffeeScript处理绑定:

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

我假设你问题中的所有代码都在initialize内。