Update sweetalert 1.x to sweetalert 2 + javascript Promises

Asked

Viewed 333 times

1

When updating the library Sweetalert it stopped working, while debugging the error I saw that it had many modifications that made the version 1.x incompatible with the new version, so I would like help to transcribe my function deletarRegistro().

Function code with sweetalert 1.x

function deletarRegistro() {
    if (id_row > 0) {
        swal({
                title: "Você tem certeza disso?",
                text: "Uma vez deletado, não há como desfazer!",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "Sim, delete isto!",
                showLoaderOnConfirm: true,
                closeOnConfirm: false
            },
            function () {
                $.post('/api/tdocumentos/delete', {id: id_row})
                    .done(function () {
                        tbl_api.row('.info').remove().draw(false);
                        swal("Deletado!", "Seu registro foi deletado.", "success");
                        id_row = null;
                        formulario.reset();
                        $(tab_lista).click();
                    })
                    .fail(function (response) {
                        console.log(response.responseText);
                        swal("Erro!", response.responseText, "error");
                    })
                ;
            })
        ;
    } else {
        tbl_dependentes.effect('shake');
        return false;
    }
}

The Sweetalert 2.0 documentation from this example:

swal({
  title: "Are you sure?",
  text: "Once deleted, you will not be able to recover this imaginary 
file!",
  icon: "warning",
  buttons: true,
  dangerMode: true,
})
.then((willDelete) => {
  if (willDelete) {
    swal("Poof! Your imaginary file has been deleted!", {
      icon: "success",
    });
  } else {
    swal("Your imaginary file is safe!");
  }
});

Question: I would like to know how I can transcribe the function using predecessors, and know what the use of predecessors improves in function.

1 answer

0

After a few more hours of video lessons, I managed to solve this stop, follow the updated code to see how it turned out, unfortunately I can not explain the details :(

function deleteData() {
    if ( id_row > 0 ) {
        swal({
            title: "Você tem certeza disso?",
            text: "Uma vez deletado, não há como desfazer!",
            icon: "warning",
            dangerMode: true,
            buttons: {
                cancel: {
                    text: "Cancelar",
                    visible: true,
                    value: false,
                    closeModal: true,
                },
                confirm: true
            },
            closeModal: false,
            closeOnClickOutside: false,
        })
            .then((resolve) => {
                if (resolve) {
                    $.post('/api/sinodos/delete', {id: id_row})
                        .done(function () {
                            tbl_api.row('.active').remove().draw(false);
                            swal("Deletado!", "Seu registro foi deletado.", "success");
                            id_row = null;
                            cadastros_sinodos.reset();
                        })
                        .fail(function (response) {
                            console.log(response.responseText);
                            swal("Erro!", response.responseText, "error");
                        })
                    ;
                } else {
                    swal.close();
                }
            })
        ;
    }
}
  • Why did you update? Watching the code saw no apparent improvements. Maybe something related to the internal code of sweetAlert2 itself?

  • @guastallaigor Yes, the structure of the code had to be changed because the Sweetalert component changed version and with that some things were discontinued, so I had to implement in this way that I replied.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.