Skip to content

Commit

Permalink
Optional onChange skip when setting/toggling state
Browse files Browse the repository at this point in the history
A state change could cause infinite loop if used within the onChange function, e.g. when a remote call failed and the state of the button should reset.
  • Loading branch information
mloenow committed Oct 15, 2012
1 parent 5ea2f80 commit 2ff9821
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions static/js/jquery.toggle.buttons.js
Expand Up @@ -128,7 +128,7 @@
changeStatus($(this));
});

$('.toggle-button').find('input').on('change', function (e) {
$('.toggle-button').find('input').on('change', function (e, skipOnChange) {
var $element = $(this).parent()
, active = $(this).is(':checked')
, $toggleButton = $(this).closest('.toggle-button');
Expand All @@ -139,7 +139,9 @@
$element.stop().animate({'left': active ? '0' : '-50%'}, $toggleButton.data('transitionSpeed'));

options = $toggleButton.data('options');
options.onChange($element, active, e);

if (!skipOnChange)
options.onChange($element, active, e);
});

$('.toggle-button').find('label').on('mousedown', function (e) {
Expand Down Expand Up @@ -212,12 +214,12 @@
toggleActivation: function () {
$(this).toggleClass('deactivate');
},
toggleState: function () {
toggleState: function (skipOnChange) {
var $input = $(this).find('input');
$input.attr('checked', !$input.is(':checked')).trigger('change')
$input.attr('checked', !$input.is(':checked')).trigger('change', skipOnChange)
},
setState: function(value) {
$(this).find('input').attr('checked', value).trigger('change')
setState: function(value, skipOnChange) {
$(this).find('input').attr('checked', value).trigger('change', skipOnChange)
}
};

Expand Down

0 comments on commit 2ff9821

Please sign in to comment.