Javascript 我想通过coffeescript调用不返回的方法
我写下面的代码Javascript 我想通过coffeescript调用不返回的方法,javascript,coffeescript,Javascript,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:
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
但后来我犯了这个错误
错误:意外缩进
实际上,我想编译成这样的ajax代码
$.when( forecastCall ).done(
function () {
this.getApiForecast();
}.bind(this)
);
你有什么解决办法吗?你的括号放错了
bind
工作呼叫的位置。您希望将整个匿名函数包装在括号中,而不仅仅是函数体:
$.when( forecastCall )
.done( ( ->
@getApiForecast()
return
).bind(@))
或者更好(或者至少噪音更小),使用=>
函数,让CoffeeScript处理绑定:
$.when( forecastCall ).done( =>
@getApiForecast()
return
)
我假设你问题中的所有代码实际上都在
initialize
中