David Hilbert
David Hilbert

Reputation: 3

Stop executing if it is the case

$('.btn-process-request', node).bind('click', function(e){
  e.preventDefault();
  var data = {};
  var fax_number_empty = {{ contract.request.customer.fax }}
  if (fax_number_empty == 0) {
    alert('Please, attach the fax number to your profile');
  }
  alert('Hello! What is your name?');
  if ($(this).data('reason')) {
      data.reason = prompt($(this).data('reason'));
      if (!data.reason.length && $(this).data('reason-required')) {
        alert($(this).data('reason-required'));
        return false;
      }
  }
  $.ajax({
    url : $(this).attr('href'),
    type: 'POST',
    data : data,
    success: function(data, textStatus, jqXHR) {
      if (data.success) {
          if (data.redirect_to) {
            window.location.href = data.redirect_to;
          }
          else if (data.reload) {
            window.location.reload();
          }
      }
      else {
        alert('Error! See console for details :(');
        console.error(textStatus, data);
      }
    },
    error: function (jqXHR, textStatus, errorThrown) {
      console.error(textStatus, errorThrown);
    }
  });
  return false;
});

In this code, I've created the lines

var fax_number_empty = {{ contract.request.customer.fax }}
      if (fax_number_empty == 0) {
        alert('Please, attach the fax number to your profile');
      }

I don't how to implement this correctly. I'd like say, ok, if contract.request.customer.fax is empty, then display an alert which will say 'Please, attach the fax number to your profile.' The most important thing here is to stop executing. In other words, what is bellow those lines would not be executed. Could anyone have time to tell me how to improve that code?

P.S. Please don't be shy to let me know if my question is unclear.

Upvotes: 0

Views: 38

Answers (1)

Karim
Karim

Reputation: 8632

Just return in case the field fax_number_empty is a falsy value , and the function will jump to the end avoiding the rest of the code.

   if (!fax_number_empty) {
     alert('Please, attach the fax number to your profile');
     return; 
   }

Upvotes: 1

Related Questions