0

I have a dialog that checks if user really wants to save the data when a vehicle exceed kilometers params. The dialog is shown in the correct moment, but the code continues and save the data before the user says yes or no to that option.

I put the code below:

if(vehicleIDkm > 0){                                    
                                        //Instrucciones para comprobar si no supera la media de kilometros del vehiculo
                                        $.ajax({
                                            url: "getVehicleKMmax.json?vehicleId=" + vehicleIDkm,
                                            method: 'GET',
                                            dataType: "json",
                                            async: false,
                                            contentType: 'application/json',
                                            success: function(kmMaxVehicle){
                                                if(kilometrosLlegada - kilometrosSalida > kmMaxVehicle){
                                                    // Abrir la ventana para informar y pedir confirmacion      
                                                    $("#dialog-activity-data-confirm").dialog({
                                                        resizable: false,
                                                        height: 300,
                                                        width: 400,
                                                        modal: true,
                                                        async: false,
                                                        dialogClass: "confirmDialogClass",
                                                        title: "Exceso de kilómetros",
                                                        open: function() {
                                                          var markup = 'Ha excedido la cantidad de kilometros a realizar en un dia por este vehiculo, ¿quiere guardar la producción igualmente? De ser así, pulse en continuar, si no, cierre esta ventana.';
                                                          $(this).html(markup);
                                                        },
                                                        buttons:
                                                        [
                                                            {
                                                                text: "Continuar",
                                                                "class": "buttonTextDelete roundedButton roundedButtonAccent confirmBG",
                                                                click: function() {
                                                                    sePuedeGuardar = true;
                                                                    noSePuedeGuardarPorKilometros = false;
                                                                    $(this).dialog('close');
                                                                }
                                                            }   
                                                        ],
                                                        close: function() { 
                                                            sePuedeGuardar = false;
                                                            noSePuedeGuardarPorKilometros = true;
                                                        }
                                                    });
                                                }                                               
                                            },
                                            error: function(error){
                                                console.log("Se ha producido un error al comprobar si es festivo el día.");
                                            }
                                        });
                                    }



                                    if(sePuedeGuardar){
                                        // Guardamos los datos
                                        $.ajax({
                                            type: "POST",
                                            url: finalUrl,
                                            ...
                                            ...
                                            ...

As you see, I use a variable "sePuedeGuardar" to check if you can save or not the data, but it always go in that if, no matter if it is true or false, because it goes there before the user answer the dialog question.

Can anyone help me? Thanks!

Imrik
  • 674
  • 2
  • 14
  • 32
  • Your code says: start this ajax request, now check if sePeudeGuardar set, then get the ajax response and show a dialog, then stop... the user then, later, clicks (after your code has finished) and sets sePeudeGuardar. You need to embrace events and asynchronous requests. – freedomn-m Apr 14 '20 at 13:06
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – freedomn-m Apr 14 '20 at 13:06
  • See https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call?rq=1 - this applies to user-initiated-events (eg click) (which are effectively asynchronous) as well. – freedomn-m Apr 14 '20 at 13:06

0 Answers0