Skip to content

Commit

Permalink
Fix re-enabling "data-disable-with" elements
Browse files Browse the repository at this point in the history
Closes rails#92
  • Loading branch information
JangoSteve authored and mislav committed Mar 18, 2011
1 parent e42caaf commit 32a611d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/rails.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,10 @@
}

function enableFormElements(form) {
form.find('input[data-disable-with]').each(function() {
form.find('input[data-disable-with]:disabled').each(function() {
var input = $(this);
input.val(input.data('ujs:enable-with')).removeAttr('disabled');
if (input.data('ujs:enable-with')) input.val(input.data('ujs:enable-with'));
input.removeAttr('disabled');
});
}

Expand Down
32 changes: 32 additions & 0 deletions test/public/test/data-disable.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,35 @@ asyncTest('form submit button with "data-disable-with" attribute', 6, function()

setTimeout(checkDisabledState, 30);
});

asyncTest('form with input[type=submit][data-disable-with] is replaced in ajax callback', 2, function(){
var form = $('form:not([data-remote])').attr('data-remote', 'true'), origFormContents = form.html();

form.bind('ajax:success', function(){
form.html(origFormContents);

setTimeout(function(){
var input = form.find('input[type=submit]');
ok(!input.is(':disabled'), 'new input field should not be disabled');
equal(input.val(), 'Submit', 'new input field should not have value replaced by "enable" function');

start();
}, 30);
}).trigger('submit');
});

asyncTest('form with input[data-disable-with] is replaced with disabled field in ajax callback', 2, function(){
var form = $('form:not([data-remote])').attr('data-remote', 'true'), input = form.find('input[type=submit]'),
newDisabledInput = input.clone().attr('disabled', 'disabled');

form.bind('ajax:success', function(){
input.replaceWith(newDisabledInput);

setTimeout(function(){
ok(!newDisabledInput.is(':disabled'), 'new input field should not be disabled');
equal(newDisabledInput.val(), 'Submit', 'new input field should not have value replaced if "ujs:enable-with" is blank');

start();
}, 30);
}).trigger('submit');
});

0 comments on commit 32a611d

Please sign in to comment.