简体   繁体   中英

Ajax Customizable Error Callback Function

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"

PLEASE Keep code in CoffeeScript

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM