0

I have the following code in my document.ready

  technicianDropdownFromCBU(cbu_id);
  assetsDropdownFromCBU(cbu_id);

  if(#{@appointment.technician_id.present?}){
     $('#appointment_technician_id').val('{@appointment.technician_id}');
}

I only want the if statement to run after the first two functions are finished executing. I can't modify those functions and include the if statement in that code, because the function is called multiple times.

2

2 Answers 2

2

A Simple solution will be, create a callback function and put your if condition in that function. Call your callback function inside the body of your uper functions like this:

function technicianDropdownFromCBU(cbu_id){
   //do you work here
   assetsDropdownFromCBU(cbu_id);
}

function assetsDropdownFromCBU(cbu_id){
   //do you work here
   callback();
}

function callback(){
  if(#{@appointment.technician_id.present?}){
     $('#appointment_technician_id').val('{@appointment.technician_id}');
  }
}

technicianDropdownFromCBU(cbu_id);

Notes: It will execute your functions in sequence. So your if condition will run in the end.

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

1 Comment

I didn't want to modify those other functions, but thanks for the answer!
1

If you are using jQuery you can use:

function someFunctions(cbu_id) {
    technicianDropdownFromCBU(cbu_id);
    assetsDropdownFromCBU(cbu_id);
}

$.when($.ajax(someFunctions(cbu_id))).then(function () {
    if(#{@appointment.technician_id.present?}){
        $('#appointment_technician_id').val('{@appointment.technician_id}');
    }
});

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.