Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

support button[data-disable-with]

Closes #102
  • Loading branch information...
commit bb620a29199214b5c7a3a015372fda28b69c69a1 1 parent 32a611d
@mislav mislav authored
Showing with 31 additions and 9 deletions.
  1. +9 −9 src/rails.js
  2. +22 −0 test/public/test/data-disable.js
View
18 src/rails.js
@@ -83,19 +83,19 @@
}
function disableFormElements(form) {
- form.find('input[data-disable-with]').each(function() {
- var input = $(this);
- input.data('ujs:enable-with', input.val())
- .val(input.data('disable-with'))
- .attr('disabled', 'disabled');
+ form.find('input[data-disable-with], button[data-disable-with]').each(function() {
+ var element = $(this), method = element.is('button') ? 'html' : 'val';
+ element.data('ujs:enable-with', element[method]());
+ element[method](element.data('disable-with'));
+ element.attr('disabled', 'disabled');
});
}
function enableFormElements(form) {
- form.find('input[data-disable-with]:disabled').each(function() {
- var input = $(this);
- if (input.data('ujs:enable-with')) input.val(input.data('ujs:enable-with'));
- input.removeAttr('disabled');
+ form.find('input[data-disable-with]:disabled, button[data-disable-with]:disabled').each(function() {
+ var element = $(this), method = element.is('button') ? 'html' : 'val';
+ if (element.data('ujs:enable-with')) element[method](element.data('ujs:enable-with'));
+ element.removeAttr('disabled');
});
}
View
22 test/public/test/data-disable.js
@@ -40,6 +40,28 @@ asyncTest('form input field with "data-disable-with" attribute', 7, function() {
equal(input.val(), 'processing ...', 'input field should have disabled value given to it');
});
+asyncTest('form button with "data-disable-with" attribute', 6, function() {
+ var form = $('form[data-remote]'), button = $('<button data-disable-with="submitting ..." name="submit2">Submit</button>');
+ form.append(button);
+
+ function checkOriginalState() {
+ ok(!button.is(':disabled'), 'button should not be disabled');
+ equal(button.text(), 'Submit', 'button should have original value');
+ }
+ checkOriginalState();
+
+ form.bind('ajax:success', function(e, data) {
+ setTimeout(function() {
+ checkOriginalState();
+ start();
+ }, 13)
+ })
+ form.trigger('submit');
+
+ ok(button.is(':disabled'), 'button should be disabled');
+ equal(button.text(), 'submitting ...', 'button should have disabled value');
+});
+
asyncTest('form submit button with "data-disable-with" attribute', 6, function(){
var form = $('form:not([data-remote])'), input = form.find('input[type=submit]');
Please sign in to comment.
Something went wrong with that request. Please try again.