Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use jquery callback functions in if statement?

I have a function whinch performs something and it works great. Now i want to add a bootstrap modal and execute the same code when modal closes. But my problem is that i have to check for user status. Is there a way how to execute this code and not to copy everything twice?

if(USERTYPE == 0) {
  $("#fastRegisterModal").modal('show');
  $("#fastRegisterModal").on('hidden', function() {
    //Code for registration and upload code
  }
} else {
  //upload code
}

I want to include this into multiple jquery function (mostly .live functions) and i don't want to move "upload code" into normal js function (I got the .js file writen as it is and i don't want to change 70% of the file)

Is this even possible in this or any other way?

EDITED

Here is some more code:

$('.someAction').live('click', function(event) {
// Init section
var data1 = $(".getData1").val();
var data2 = $(".getData2").val();

// I want to add this modal check before exsisting code

if(USERTYPE == 0) {
    $("#fastRegisterModal").modal('show').on('hidden', function() {
        var email = $(".getModalEmail").val();
        var pass = $(".getModalPass").val();

        $.ajax({
            url: "actions/fastRegister",
            type: 'POST',
            data: {email: email, pass: pass}, 
            success: function(data) {
                if (data.success == 1) {
                    // continue with existing block of code
                }
            }
        });
    });
}

// Existing block of code

$.ajax({
    url: "actions/someAction",
    type: 'POST',
    data: {data1: data1, data2: data2}, 
    success: function(data) {
        if (data != '') {
            // show success
        }
    }
});
});

I hope this explaings my situation bether :)

like image 475
BoonZ Avatar asked Dec 07 '25 19:12

BoonZ


1 Answers

Create the function.

How much more work is it than two lines of code and a cut and paste?

Resisting refactoring tends to lead to long term pain.

Doesn't something like this work? How much effort is it to get there from what you have?

var doSomeAction = function () {
    $.ajax({
          url: "actions/someAction",
          type: 'POST',
          data: {data1: data1, data2: data2}, 
          success: function(data) {
              if (data != '') {
                // show success
              }
          }
     });

 };

 if(USERTYPE == 0) {
    $("#fastRegisterModal").modal('show').on('hidden', function() {
       var email = $(".getModalEmail").val();
       var pass = $(".getModalPass").val();

       $.ajax({
          url: "actions/fastRegister",
          type: 'POST',
          data: {email: email, pass: pass}, 
         success: function(data) {
            if (data.success == 1) {
                doSomeAction();
            }
          }
      });
    });
} else {
   doSomeAction();
}
like image 105
djna Avatar answered Dec 09 '25 14:12

djna



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!