0

So this function works perfectly expect I have six more buttons and do not want my code to have a ton of repeat code, so is there a way to pull out the beforeSend call and make it an external callback function? Thank You In Advance

$('#button_status').on 'click', ->
username = $('#login_username').val()
password = $('#login_password').val()
mac_id = $('#login_mac').val()

$.ajax
  type: "GET"
  url: start_url + mac_id + "/status"
  dataType: "json"
  crossDomain: true
  cache: false

  beforeSend: (xhr) ->
    xhr.setRequestHeader('Authorization', 'Basic ' + btoa(username + ":" + password));

!More to Question!

I have this also part of my ajax but if I make all buttons have the same callback the error messages will be the same and pointless. 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"
1
  • 1
    Also to be noted this code is written in CoffeeScript. Commented Apr 22, 2014 at 15:11

2 Answers 2

0

Sure

callback = (xhr) -> xhr.setRequestHeader('Authorization', 'Basic ' + btoa(username + ":" + password))
beforeSend: callback

Just make sure variables are part of the scope. CoffeScript is function scoped.

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

4 Comments

Thank You! This worked I would vote up but sadly I do not have enough points. But I will accept it soon
You can select it as THE answer. One more thing, semicolons don't belong in coffescript. I'll update my answer.
Would you mind helping me with this error function to create a customizable callback?
0

Could you not turn the beforeSend into a function?

var onBeforeSend = function(xhr) {
     xhr.setRequestHeader('Authorization', 'Basic ' + btoa(username + ":" + password));
}

and then do:

$.ajax
  type: "GET"
  url: start_url + mac_id + "/status"
  dataType: "json"
  crossDomain: true
  cache: false,
  beforeSend: onBeforeSend

It's not coffeescript, but that's how I would do it with plain javascript.

Would something like this work?

onBeforeSend = (xhr) ->
    xhr.setRequestHeader('Authorization', 'Basic ' + btoa(username + ":" + password));

$.ajax '/Foo/Bar/',
    type: "GET"
    url: start_url + mac_id + "/status"
    dataType: "json"
    crossDomain: true
    cache: false,
    beforeSend: (xhr) ->
        onBeforeSend xhr

2 Comments

beforeSend: onBeforeSend Will not work with coffeescript sadly.
Undefined Function is my error but @beautifulcoder is correct.

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.