Browse files

BUGFIX Fixed change tracking of checkboxes and radiobuttons in jquery…

….changetracker plugin
  • Loading branch information...
1 parent ddad7fb commit 64f6b460e9051c6148626a0cc04811f3d607437b @chillu chillu committed Dec 15, 2011
View
21 admin/javascript/jquery-changetracker/lib/jquery.changetracker.js
@@ -54,19 +54,24 @@
this.initialize = function() {
// optional metadata plugin support
if ($.meta) options = $.extend({}, options, this.data());
-
- // setup original values
- this.getFields()
- .bind('change', function(e) {
+
+ var onchange = function(e) {
var $field = $(e.target);
var origVal = $field.data('changetracker.origVal');
- if(origVal === null || $field.val() != origVal) {
+ if(origVal === null || e.target.value != origVal) {
+ // TODO Also add class to radiobutton/checkbox siblings
$field.addClass(options.changedCssClass);
self.addClass(options.changedCssClass);
}
- })
- .each(function() {
- $(this).data('changetracker.origVal', $(this).val());
+ };
+
+ // setup original values
+ var fields = this.getFields();
+ fields.filter(':radio,:checkbox').bind('click', onchange);
+ fields.not(':radio,:checkbox').bind('change', onchange);
+ fields.each(function() {
+ var origVal = $(this).is(':radio,:checkbox') ? self.find(':input[name=' + $(this).attr('name') + ']:checked').val() : $(this).val();
+ $(this).data('changetracker.origVal', origVal);
});
this.data('changetracker', true);
View
11 admin/javascript/jquery-changetracker/spec/spec.changetracker.basics.js
@@ -33,15 +33,14 @@ describe 'ChangeTracker'
it 'can track changes on input type=radio fields with existing values'
$('#form_test').append(
- '<input type="radio" name="field_radio" value="1" />'
- + '<input type="radio" name="field_radio" value="2" />'
+ '<input type="radio" id="field_radio1" name="field_radio" value="1" checked="checked" />'
+ + '<input type="radio" id="field_radio2" name="field_radio" value="2" />'
);
-
$('#form_test').changetracker();
-
- $(':input[name=field_radio]').val(1).trigger('change');
+ $('#field_radio2').attr('checked', 'checked').trigger('click');
$('#form_test').is('.changed').should.be_true
- $(':input[name=field_radio]').is('.changed').should.be_true
+ // $('#field_radio1').is('.changed').should.be_true
+ $('#field_radio2').is('.changed').should.be_true
end
it 'can track changes on select fields with existing values'

0 comments on commit 64f6b46

Please sign in to comment.