Sweet Alert 2 перенаправить на CANCEL - PullRequest
0 голосов
/ 05 ноября 2018

Я пишу проект в laravel, на моем текущем этапе рабочего процесса я хочу, чтобы пользователь мог нажать кнопку, чтобы опубликовать или удалить событие из текущего списка.

Я использую SA2, чтобы остановить отправку на маршрут до того, как это произойдет. Если пользователь нажимает кнопку ОК, то все идет по плану, когда пользователь нажимает кнопку отмены, я хочу перенаправить обратно на страницу.

Проблема, с которой я сталкиваюсь, заключается в том, что когда пользователь нажимает кнопку отмены, перенаправление на страницу происходит в любом случае ...

function warnRemove(linkURL) {
    swal({
        title: 'Are you sure?',
        type: 'warning',
        showCancelButton: true,
        confirmButtonColor: 'D6B55',
        confirmButtonText: 'Yes, remove it!'
    }).then(function () {
        window.location = linkURL;
    });
}

function warnPublish(linkURL) {
    swal({
        title: 'Are you sure?',
        type: 'warning',
        text: 'This event will go live on the screen after next refresh.',
        showCancelButton: true,
        confirmButtonColor: 'D6B55',
        confirmButtonText: 'Yes, publish it!'
    }).then(function () {
        window.location = linkURL;
    });
}



$('.remove').click(function(e) {
    e.preventDefault(); // Prevent the href from redirecting directly
    let linkURL = $(this).attr("href");
    warnRemove(linkURL);
});

$('.publish').click(function(e) {
    e.preventDefault(); // Prevent the href from redirecting directly
    let linkURL = $(this).attr("href");
    warnPublish(linkURL);
});

1 Ответ

0 голосов
/ 05 ноября 2018

Вам нужно будет использовать обратный вызов с логическим значением isConfirm:

function(isConfirm) {
    if (isConfirm) {
        // do confirmed things
    }
}

Из документов:

swal({
  title: 'Are you sure?',
  text: "You won't be able to revert this!",
  type: 'warning',
  showCancelButton: true,
  confirmButtonColor: '#3085d6',
  cancelButtonColor: '#d33',
  confirmButtonText: 'Yes, delete it!'
}).then((result) => {
  // redirect only if true
  if (result.value) {
    // redirect here
  }
})
...