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.
This commit is contained in:
Marcin Kurczewski 2014-02-19 20:46:37 +01:00
parent b879a7c38b
commit ddbecdb16f
4 changed files with 24 additions and 19 deletions

View file

@ -3,15 +3,13 @@ $(function()
function onDomUpdate() function onDomUpdate()
{ {
$('form.edit-comment textarea, form.add-comment textarea') $('form.edit-comment textarea, form.add-comment textarea')
.off('change keyp') .bindOnce('exit-confirmation', 'change keyp', function(e)
.on('change keyup', function(e) {
{ enableExitConfirmation();
enableExitConfirmation(); });
});
$('form.edit-comment, form.add-comment') $('form.edit-comment, form.add-comment')
.off('submit') .bindOnce('comment-submit', 'submit', function(e)
.on('submit', function(e)
{ {
e.preventDefault(); e.preventDefault();
rememberLastSearchQuery(); rememberLastSearchQuery();
@ -96,7 +94,7 @@ $(function()
$.ajax(ajaxData); $.ajax(ajaxData);
}); });
$('.comment .edit a').off('click').on('click').click(function(e) $('.comment .edit a').bindOnce('edit-comment', 'click', function(e)
{ {
e.preventDefault(); e.preventDefault();
var commentDom = $(this).parents('.comment'); var commentDom = $(this).parents('.comment');

View file

@ -37,6 +37,17 @@ $.fn.hasAttr = function(name)
return this.attr(name) !== undefined; 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 //safety trigger
@ -82,14 +93,14 @@ $(function()
} }
} }
$('form.confirmable').submit(confirmEvent); $('form.confirmable').bindOnce('confirmation', 'submit', confirmEvent);
$('a.confirmable').click(confirmEvent); $('a.confirmable').bindOnce('confirmation', 'click', confirmEvent);
//simple action buttons //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; return;
e.preventDefault(); e.preventDefault();
@ -124,7 +135,7 @@ $(function()
//attach data from submit buttons to forms before .submit() gets called //attach data from submit buttons to forms before .submit() gets called
$('.submit').each(function() $('.submit').each(function()
{ {
$(this).click(function() $(this).bindOnce('submit-faux-input', 'click', function()
{ {
var form = $(this).closest('form'); var form = $(this).closest('form');
form.find('.faux-submit').remove(); form.find('.faux-submit').remove();

View file

@ -2,13 +2,9 @@ $(function()
{ {
$('body').bind('dom-update', 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.preventDefault();
e.stopPropagation();
var aDom = $(this); var aDom = $(this);
if (aDom.hasClass('inactive')) if (aDom.hasClass('inactive'))

View file

@ -2,7 +2,7 @@ $(function()
{ {
function onDomUpdate() function onDomUpdate()
{ {
$('#sidebar a.edit-post').click(function(e) $('#sidebar a.edit-post').bindOnce('edit-post', 'click', function(e)
{ {
e.preventDefault(); e.preventDefault();