From 895bd2bf119f247842c955e281f50d94df2bb6e8 Mon Sep 17 00:00:00 2001 From: Igor Lemos Vicente Date: Thu, 9 Aug 2018 14:54:14 -0300 Subject: [PATCH] Fix jQuery's 3.0 deprecated method --- lib/assets/javascripts/sweet-alert-confirm.js | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/lib/assets/javascripts/sweet-alert-confirm.js b/lib/assets/javascripts/sweet-alert-confirm.js index 5f7deda..44b6ee4 100644 --- a/lib/assets/javascripts/sweet-alert-confirm.js +++ b/lib/assets/javascripts/sweet-alert-confirm.js @@ -2,7 +2,7 @@ var sweetAlertConfirmConfig = sweetAlertConfirmConfig || {}; // Add default conf (function( $ ) { var sweetAlertConfirm = function(event) { - + swalDefaultOptions = { title: sweetAlertConfirmConfig.title || 'Are you sure?', type: sweetAlertConfirmConfig.type || 'warning', @@ -36,7 +36,7 @@ var sweetAlertConfirmConfig = sweetAlertConfirmConfig || {}; // Add default conf 'method', 'function' ]; - + function afterAlertCallback(r){ if (nameFunction) { window[nameFunction](); @@ -68,13 +68,13 @@ var sweetAlertConfirmConfig = sweetAlertConfirmConfig || {}; // Add default conf else { window.location.href = $linkToVerify.attr('href'); } - + } } } - + var beforeFunction = null; - + $.each($linkToVerify.data(), function(key, val){ if ($.inArray(key, optionKeys) >= 0) { swalOptions[key] = val @@ -87,7 +87,7 @@ var sweetAlertConfirmConfig = sweetAlertConfirmConfig || {}; // Add default conf beforeFunction = val; } }); - + // Skip alert if false if(beforeFunction != null) { var beforeRes = window[beforeFunction]($linkToVerify); @@ -95,7 +95,7 @@ var sweetAlertConfirmConfig = sweetAlertConfirmConfig || {}; // Add default conf return afterAlertCallback(true); // Skip alert } } - + var nameFunction = swalOptions['function']; message = $linkToVerify.attr('data-sweet-alert-confirm') swalOptions['title'] = message @@ -103,11 +103,23 @@ var sweetAlertConfirmConfig = sweetAlertConfirmConfig || {}; // Add default conf return false; } - $(document).on('ready turbolinks:load page:update ajaxComplete', function() { + + $(function() { $('[data-sweet-alert-confirm]').on('click', sweetAlertConfirm) + }) + + $(document).on('turbolinks:load page:update ajaxComplete', function() { + $('[data-sweet-alert-confirm]').on('click', sweetAlertConfirm) + }); + + $(function() { + //To avoid "Uncaught TypeError: Cannot read property 'querySelector' of null" on turbolinks + if (typeof window.sweetAlertInitialize === 'function') { + window.sweetAlertInitialize(); + } }); - $(document).on('ready turbolinks:load page:load', function() { + $(document).on('turbolinks:load page:load', function() { //To avoid "Uncaught TypeError: Cannot read property 'querySelector' of null" on turbolinks if (typeof window.sweetAlertInitialize === 'function') { window.sweetAlertInitialize();