Skip to content

Commit 18769f5

Browse files
committed
support use of disable-with on elements using the "form" attribute
1 parent 7317021 commit 18769f5

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

src/rails.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@
189189
- Sets disabled property to true
190190
*/
191191
disableFormElements: function(form) {
192-
form.find(rails.disableSelector).each(function() {
192+
var elements = form.is('form') ? $(form[0].elements).filter(rails.disableSelector) : form.find(rails.disableSelector);
193+
elements.each(function() {
193194
var element = $(this), method = element.is('button') ? 'html' : 'val';
194195
element.data('ujs:enable-with', element[method]());
195196
element[method](element.data('disable-with'));
@@ -202,7 +203,9 @@
202203
- Sets disabled property to false
203204
*/
204205
enableFormElements: function(form) {
205-
form.find(rails.enableSelector).each(function() {
206+
var elements = form.is('form') ? $(form[0].elements).filter(rails.enableSelector) : form.find(rails.enableSelector);
207+
elements.each(function() {
208+
// form.find(rails.enableSelector).each(function() {
206209
var element = $(this), method = element.is('button') ? 'html' : 'val';
207210
if (element.data('ujs:enable-with')) element[method](element.data('ujs:enable-with'));
208211
element.prop('disabled', false);

test/public/test/data-disable.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ module('data-disable', {
1010

1111
$('#qunit-fixture').append($('<form />', {
1212
action: '/echo',
13-
method: 'post'
13+
method: 'post',
14+
id: 'not_remote'
1415
}))
1516
.find('form:last')
1617
// WEEIRDD: the form won't submit to an iframe if the button is name="submit" (??!)
@@ -21,6 +22,14 @@ module('data-disable', {
2122
href: '/echo',
2223
'data-disable-with': 'clicking...'
2324
}));
25+
26+
$('#qunit-fixture').append($('<input />', {
27+
type: 'submit',
28+
form: 'not_remote',
29+
'data-disable-with': 'form attr submitting',
30+
name: 'submit3',
31+
value: 'Form Attr Submit'
32+
}));
2433
},
2534
teardown: function() {
2635
$(document).unbind('iframe:loaded');
@@ -100,6 +109,27 @@ asyncTest('form input[type=submit][data-disable-with] disables', 6, function(){
100109
}, 30);
101110
});
102111

112+
asyncTest('form input[type=submit][data-disable-with] using "form" attribute disables', 6, function() {
113+
var form = $('#not_remote'), input = $('input[form=not_remote]');
114+
checkEnabledState(input, 'Form Attr Submit');
115+
116+
// WEEIRDD: attaching this handler makes the test work in IE7
117+
$(document).bind('iframe:loading', function(e, form) {});
118+
119+
$(document).bind('iframe:loaded', function(e, data) {
120+
setTimeout(function() {
121+
checkDisabledState(input, 'form attr submitting');
122+
start();
123+
}, 30);
124+
});
125+
form.trigger('submit');
126+
127+
setTimeout(function() {
128+
checkDisabledState(input, 'form attr submitting');
129+
}, 30);
130+
131+
});
132+
103133
asyncTest('form[data-remote] input[type=submit][data-disable-with] is replaced in ajax callback', 2, function(){
104134
var form = $('form:not([data-remote])').attr('data-remote', 'true'), origFormContents = form.html();
105135

0 commit comments

Comments
 (0)