阿贾克斯自定义错误回调函数 [英] Ajax Customizable Error Callback Function
问题描述
所以这个功能完美的作品exceptI有六个按钮,不希望我的code有大量的重复code。如果用户不是管理等如何我,我可以让这个自定义每个按钮的定制部分我想为每个回调是不同的,例如文本追加请登录有所不同?谢谢!
So this function works perfectly exceptI have six more buttons and do not want my code to have a ton of repeat code. For the customizing part I would like for each callback to be different for example the text append "Please Log In" to be different if the user is not admin and etc. How I can I make this customizable for each button? Thank You!
error: (xhr, ajaxOptions, thrownError) ->
console.dir arguments
console.log("*| Status ", xhr.status)
console.log("*| Error", thrownError)
console.log("*| Ajax", ajaxOptions)
if (not username? or not password?)
$('#data-text').empty()
$('#data-text').append ("""<h1>Please Log In</h1>""")
$('#input_username').fadeTo(100, 0.1).fadeTo(200, 1.0);
$('#input_password').fadeTo(100, 0.1).fadeTo(200, 1.0);
$('#header_user').css "background-color": "#d34242"
$('#header_password').css "background-color": "#d34242"
$('#data-text').css "background-color": "#d38642"
else
$('#data-text').empty()
$('#data-text').append ("""<h1>Failed Log In</h1>""")
$('#input_username').fadeTo(100, 0.1).fadeTo(200, 1.0);
$('#input_password').fadeTo(100, 0.1).fadeTo(200, 1.0);
$('#header_user').css "background-color": "#d34242"
$('#header_password').css "background-color": "#d34242"
$('#data-text').css "background-color": "#d38642"
请保持code中的CoffeeScript
PLEASE Keep code in CoffeeScript
推荐答案
在CoffeeScript中,您可以在功能定义功能。不要让任何人告诉你的CoffeeScript或JavaScript是一种功能性的语言。函数是第一类对象和对象可以封装其他对象。
In CoffeeScript, you may define functions within functions. Don't let anyone tell you CoffeeScript or JavaScript is a functional language. Functions are first-class objects and objects can encapsulate other objects.
error: (xhr, ajaxOptions, thrownError) ->
console.dir(arguments)
console.log("*| Status ", xhr.status)
console.log("*| Error", thrownError)
console.log("*| Ajax", ajaxOptions)
needsAGoodName = (msg) ->
$('#data-text').empty()
$('#data-text').append(msg)
$('#input_username').fadeTo(100, 0.1).fadeTo(200, 1.0)
$('#input_password').fadeTo(100, 0.1).fadeTo(200, 1.0)
$('#header_user').css("background-color": "#d34242")
$('#header_password').css("background-color": "#d34242")
$('#data-text').css("background-color": "#d38642")
if not username? or not password?
needsAGoodName("""<h1>Please Log In</h1>""")
else
needsAGoodName("""<h1>Failed Log In</h1>""")
我去掉周围括号如果
并添加周围的造型函数调用。它的工作原理两种方式。
I removed parenthesis around if
and added them around function calls for styling. It works either way.
这篇关于阿贾克斯自定义错误回调函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!