1

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.