有没有办法在 CoffeeScript 中向此 JQuery 调用添加另一个函数引用参数,而无需为每个参数使用一行?
Is there a way to add another function reference parameter to this JQuery call in CoffeeScript without using a line for each parameter?
有没有一种方法可以将 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: function(data) {
alert('Success');
return $('#row' + index).detach();
},
error: function() {
return alert("Could not delete the food.");
}
});
你可以这样做...
$.ajax type:'DELETE', url: '/history', data: {id: id}
,success: (data) ->
alert 'Success'
$('#row'+index).detach()
,error: ->
alert "Could not delete the food."
...但我认为这不是一个好习惯。
如果您的目标是了解 coffeescript 的工作原理,我建议您使用网站上的实时解析器,例如:http://coffeescript.org/#try:
旁注: All jquery ajax functions (post/get/ajax) return promises,这比通常的有很多好处回调。更多相关信息:https://gist.github.com/domenic/3889970
如果您乐于编写难以理解的代码,这会让那些卡在维护您的代码中的人充满仇恨,您可以添加括号:
$.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
无论如何,我更喜欢避免比几行长的匿名函数,缩进很快就会让人混淆。
有没有一种方法可以将 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: function(data) {
alert('Success');
return $('#row' + index).detach();
},
error: function() {
return alert("Could not delete the food.");
}
});
你可以这样做...
$.ajax type:'DELETE', url: '/history', data: {id: id}
,success: (data) ->
alert 'Success'
$('#row'+index).detach()
,error: ->
alert "Could not delete the food."
...但我认为这不是一个好习惯。
如果您的目标是了解 coffeescript 的工作原理,我建议您使用网站上的实时解析器,例如:http://coffeescript.org/#try:
旁注: All jquery ajax functions (post/get/ajax) return promises,这比通常的有很多好处回调。更多相关信息:https://gist.github.com/domenic/3889970
如果您乐于编写难以理解的代码,这会让那些卡在维护您的代码中的人充满仇恨,您可以添加括号:
$.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
无论如何,我更喜欢避免比几行长的匿名函数,缩进很快就会让人混淆。