From ddbecdb16faf601fe5e591d6a2978c1b9633e236 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Wed, 19 Feb 2014 20:46:37 +0100 Subject: [PATCH] Fixed problems with multiple event handlers Whenever DOM update event handlers were executed, jQuery bindings were appended instead of being replaced. It resulted in funny scenarios like starting to show multiple confirmation dialogs when trying to delete a post, after adding/removing it from favorites. --- public_html/media/js/comment-edit.js | 14 ++++++-------- public_html/media/js/core.js | 21 ++++++++++++++++----- public_html/media/js/post-list.js | 6 +----- public_html/media/js/post-view.js | 2 +- 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/public_html/media/js/comment-edit.js b/public_html/media/js/comment-edit.js index 95a5b8c2..b0844f9f 100644 --- a/public_html/media/js/comment-edit.js +++ b/public_html/media/js/comment-edit.js @@ -3,15 +3,13 @@ $(function() function onDomUpdate() { $('form.edit-comment textarea, form.add-comment textarea') - .off('change keyp') - .on('change keyup', function(e) - { - enableExitConfirmation(); - }); + .bindOnce('exit-confirmation', 'change keyp', function(e) + { + enableExitConfirmation(); + }); $('form.edit-comment, form.add-comment') - .off('submit') - .on('submit', function(e) + .bindOnce('comment-submit', 'submit', function(e) { e.preventDefault(); rememberLastSearchQuery(); @@ -96,7 +94,7 @@ $(function() $.ajax(ajaxData); }); - $('.comment .edit a').off('click').on('click').click(function(e) + $('.comment .edit a').bindOnce('edit-comment', 'click', function(e) { e.preventDefault(); var commentDom = $(this).parents('.comment'); diff --git a/public_html/media/js/core.js b/public_html/media/js/core.js index 62bb2507..cacd6b39 100644 --- a/public_html/media/js/core.js +++ b/public_html/media/js/core.js @@ -37,6 +37,17 @@ $.fn.hasAttr = function(name) return this.attr(name) !== undefined; }; +$.fn.bindOnce = function(name, eventName, callback) +{ + $.each(this, function(i, item) + { + if ($(item).data(name) == name) + return; + $(item).data(name, name); + $(item).on(eventName, callback); + }); +}; + //safety trigger @@ -82,14 +93,14 @@ $(function() } } - $('form.confirmable').submit(confirmEvent); - $('a.confirmable').click(confirmEvent); + $('form.confirmable').bindOnce('confirmation', 'submit', confirmEvent); + $('a.confirmable').bindOnce('confirmation', 'click', confirmEvent); //simple action buttons - $('a.simple-action').click(function(e) + $('a.simple-action').bindOnce('simple-action', 'click', function(e) { - if(e.isPropagationStopped()) + if (e.isPropagationStopped()) return; e.preventDefault(); @@ -124,7 +135,7 @@ $(function() //attach data from submit buttons to forms before .submit() gets called $('.submit').each(function() { - $(this).click(function() + $(this).bindOnce('submit-faux-input', 'click', function() { var form = $(this).closest('form'); form.find('.faux-submit').remove(); diff --git a/public_html/media/js/post-list.js b/public_html/media/js/post-list.js index 885588bb..300efd7c 100644 --- a/public_html/media/js/post-list.js +++ b/public_html/media/js/post-list.js @@ -2,13 +2,9 @@ $(function() { $('body').bind('dom-update', function() { - $('.post a.toggle-tag').click(function(e) + $('.post a.toggle-tag').bindOnce('toggle-tag', 'click', function(e) { - if(e.isPropagationStopped()) - return; - e.preventDefault(); - e.stopPropagation(); var aDom = $(this); if (aDom.hasClass('inactive')) diff --git a/public_html/media/js/post-view.js b/public_html/media/js/post-view.js index 32a1df78..ad077643 100644 --- a/public_html/media/js/post-view.js +++ b/public_html/media/js/post-view.js @@ -2,7 +2,7 @@ $(function() { function onDomUpdate() { - $('#sidebar a.edit-post').click(function(e) + $('#sidebar a.edit-post').bindOnce('edit-post', 'click', function(e) { e.preventDefault();