有没有办法将 error:
参数添加到此行,而不使用每行格式使用单个参数?
$.ajax type:'DELETE', url: '/history', data: {id: id}, success: (data)->
$('#row'+index).detach()
我知道我可以把它变成
$.ajax
type: 'DELETE'
url: '/history'
data: id: id
success: (data) ->
$('#row' + index).detach()
error: ->
alert 'Error'
但我想尝试更多地了解 CoffeeScript 语法的复杂性。我知道我可以对 $.post
使用括号,但这允许链接回调,这与此 $.ajax
格式不同。
$.post("/history", {food: food, size: size, unit: unit}, (data)->
alert 'Success'
).fail ->
alert 'Fail'
我尝试了以下操作,但它从未调用成功回调:
$.ajax type:'DELETE', url: '/history', data: {id: id},
success: (data)->
alert 'Success'
$('#row'+index).detach()
error: ->
alert "Could not delete the food."
<小时/>
这有效!
$.ajax type:'DELETE', url: '/history', data: {id: id}, success: ((data)->
$('#row'+tmpIndex).detach()
), error: ->
alert "Could not delete the food."
最佳答案
如果你喜欢编写难以理解的代码,让维护你的代码的人充满仇恨,你可以添加括号:
$.ajax type:'DELETE', url: '/history', data: {id: id}, success: ((data)-> ...), error: (-> ...)
但是请不要这样做。您最好使用多行版本或使用命名函数:
success = (data) ->
# ...
error = ->
# ...
$.ajax type:'DELETE', url: '/history', data: {id: id}, success: success, error: error
无论如何,我更喜欢避免超过几行的匿名函数,缩进很快就会变得困惑。
关于javascript - 有没有办法在 CoffeeScript 中向此 JQuery 调用添加另一个函数引用参数,而无需为每个参数使用一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31330559/