From 45a80af793fe7d4641621437f9025105bd5a1292 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sen=C3=A9n=20Rodero=20Rodr=C3=ADguez?= Date: Mon, 27 Jul 2020 11:29:37 +0200 Subject: [PATCH] Do not remove click event definition before defining it Use delegated handlers instead so there is not risk to run method multiple times. --- app/assets/javascripts/forms.js | 4 ++-- app/assets/javascripts/legislation_annotatable.js | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/app/assets/javascripts/forms.js b/app/assets/javascripts/forms.js index 28a92bc6b..822b2391b 100644 --- a/app/assets/javascripts/forms.js +++ b/app/assets/javascripts/forms.js @@ -9,13 +9,13 @@ }); }, submitOnChange: function() { - $(".js-submit-on-change").off("change").on("change", function() { + $("body").on("change", ".js-submit-on-change", function() { $(this).closest("form").submit(); return false; }); }, toggleLink: function() { - $(".js-toggle-link").off("click").on("click", function() { + $("body").on("click", ".js-toggle-link", function() { var toggle_txt; $($(this).data("toggle-selector")).toggle("down"); if ($(this).data("toggle-text") !== undefined) { diff --git a/app/assets/javascripts/legislation_annotatable.js b/app/assets/javascripts/legislation_annotatable.js index 982dfaae6..9b1125722 100644 --- a/app/assets/javascripts/legislation_annotatable.js +++ b/app/assets/javascripts/legislation_annotatable.js @@ -69,14 +69,14 @@ $("#comments-box").html(""); App.LegislationAllegations.show_comments(); $("#comments-box").show(); - $.event.trigger({ + $("body").trigger({ type: "renderLegislationAnnotation", annotation_id: target.data("annotation-id"), annotation_url: target.closest(".legislation-annotatable").data("legislation-annotatable-base-url"), offset: target.offset().top }); parents_ids.each(function(i, pid) { - $.event.trigger({ + $("body").trigger({ type: "renderLegislationAnnotation", annotation_id: pid, annotation_url: target.closest(".legislation-annotatable").data("legislation-annotatable-base-url") @@ -144,7 +144,7 @@ $("html,body").animate({ scrollTop: el.offset().top }); - $.event.trigger({ + $("body").trigger({ type: "renderLegislationAnnotation", annotation_id: ann_id, annotation_url: el.closest(".legislation-annotatable").data("legislation-annotatable-base-url"), @@ -188,9 +188,9 @@ }, initialize: function() { var current_user_id; - $(document).off("renderLegislationAnnotation").on("renderLegislationAnnotation", App.LegislationAnnotatable.renderAnnotationComments); - $(document).off("click", "[data-annotation-id]").on("click", "[data-annotation-id]", App.LegislationAnnotatable.onClick); - $(document).off("click", "[data-cancel-annotation]").on("click", "[data-cancel-annotation]", function(e) { + $("body").on("renderLegislationAnnotation", App.LegislationAnnotatable.renderAnnotationComments); + $("body").on("click", "[data-annotation-id]", App.LegislationAnnotatable.onClick); + $("body").on("click", "[data-cancel-annotation]", function(e) { e.preventDefault(); $("#comments-box").html(""); $("#comments-box").hide();