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()
{
$('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');

View file

@ -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();

View file

@ -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'))

View file

@ -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();