Skip to content

Commit c1330f3

Browse files
committed
refactor form element selection into helper function
1 parent d3b65a4 commit c1330f3

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/rails.js

Lines changed: 11 additions & 6 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);
@@ -182,15 +182,21 @@
182182
form.hide().append(metadataInput).appendTo('body');
183183
form.submit();
184184
},
185-
185+
186+
// Helper function that returns form elements that match the specified CSS selector
187+
// If form is actually a "form" element this will return associated elements outside the from that have
188+
// the html form attribute set
189+
formElements: function(form, selector) {
190+
return form.is('form') ? $(form[0].elements).filter(selector) : form.find(selector)
191+
},
192+
186193
/* Disables form elements:
187194
- Caches element value in 'ujs:enable-with' data store
188195
- Replaces element text with value of 'data-disable-with' attribute
189196
- Sets disabled property to true
190197
*/
191198
disableFormElements: function(form) {
192-
var elements = form.is('form') ? $(form[0].elements).filter(rails.disableSelector) : form.find(rails.disableSelector);
193-
elements.each(function() {
199+
rails.formElements(form, rails.disableSelector).each(function() {
194200
var element = $(this), method = element.is('button') ? 'html' : 'val';
195201
element.data('ujs:enable-with', element[method]());
196202
element[method](element.data('disable-with'));
@@ -203,8 +209,7 @@
203209
- Sets disabled property to false
204210
*/
205211
enableFormElements: function(form) {
206-
var elements = form.is('form') ? $(form[0].elements).filter(rails.enableSelector) : form.find(rails.enableSelector);
207-
elements.each(function() {
212+
rails.formElements(form, rails.enableSelector).each(function() {
208213
var element = $(this), method = element.is('button') ? 'html' : 'val';
209214
if (element.data('ujs:enable-with')) element[method](element.data('ujs:enable-with'));
210215
element.prop('disabled', false);

0 commit comments

Comments
 (0)