Skip to content

Commit 693ccdf

Browse files
committed
Update replacement logic to avoid DOM operations when we won't change the HTML after all.
1 parent cec7782 commit 693ccdf

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/rails.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,17 @@
192192
*/
193193
disableFormElements: function(form) {
194194
form.find(rails.disableSelector).each(function() {
195-
var element, method, enabledState;
195+
var element, method, replacement;
196+
196197
element = $(this);
197198
method = element.is('button') ? 'html' : 'val';
198-
enabledState = element[method]();
199+
replacement = element.data('disable-with');
200+
201+
element.data('ujs:enable-with', element[method]());
202+
if (replacement !== undefined) {
203+
element[method](replacement);
204+
}
199205

200-
element.data('ujs:enable-with', enabledState);
201-
element[method](element.data('disable-with') || enabledState);
202206
element.prop('disabled', true);
203207
});
204208
},
@@ -275,9 +279,13 @@
275279
// replace element's html with the 'data-disable-with' after storing original html
276280
// and prevent clicking on it
277281
disableElement: function(element) {
278-
var enabledState = element.html();
279-
element.data('ujs:enable-with', enabledState); // store enabled state
280-
element.html(element.data('disable-with') || enabledState); // set to disabled state
282+
var replacement = element.data('disable-with');
283+
284+
element.data('ujs:enable-with', element.html()); // store enabled state
285+
if (replacement !== undefined) {
286+
element.html(replacement);
287+
}
288+
281289
element.bind('click.railsDisable', function(e) { // prevent further clicking
282290
return rails.stopEverything(e);
283291
});

0 commit comments

Comments
 (0)