Skip to content

Commit 1b4882b

Browse files
committed
Merge pull request #366 from twalpole/form_attribute
support use of disable-with on elements using the html5 "form" attribute
2 parents 61f0448 + c1330f3 commit 1b4882b

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

src/rails.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
if ( $.rails !== undefined ) {
1616
$.error('jquery-ujs has already been loaded!');
1717
}
18-
18+
1919
// Shorthand to make it a little easier to call public rails functions from within rails.js
2020
var rails;
2121
var $document = $(document);
@@ -184,14 +184,21 @@
184184
form.hide().append(metadataInput).appendTo('body');
185185
form.submit();
186186
},
187-
187+
188+
// Helper function that returns form elements that match the specified CSS selector
189+
// If form is actually a "form" element this will return associated elements outside the from that have
190+
// the html form attribute set
191+
formElements: function(form, selector) {
192+
return form.is('form') ? $(form[0].elements).filter(selector) : form.find(selector)
193+
},
194+
188195
/* Disables form elements:
189196
- Caches element value in 'ujs:enable-with' data store
190197
- Replaces element text with value of 'data-disable-with' attribute
191198
- Sets disabled property to true
192199
*/
193200
disableFormElements: function(form) {
194-
form.find(rails.disableSelector).each(function() {
201+
rails.formElements(form, rails.disableSelector).each(function() {
195202
var element = $(this), method = element.is('button') ? 'html' : 'val';
196203
element.data('ujs:enable-with', element[method]());
197204
element[method](element.data('disable-with'));
@@ -204,7 +211,7 @@
204211
- Sets disabled property to false
205212
*/
206213
enableFormElements: function(form) {
207-
form.find(rails.enableSelector).each(function() {
214+
rails.formElements(form, rails.enableSelector).each(function() {
208215
var element = $(this), method = element.is('button') ? 'html' : 'val';
209216
if (element.data('ujs:enable-with')) element[method](element.data('ujs:enable-with'));
210217
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)