jquery-ujs
jquery-ujs copied to clipboard
Strange behaviour with data-disable-with buttons
Using jquery-remotipart I've got some strange behaviour that could be avoided by doing a little change in disableFormElement function. The problem occurs when this function gets called a second time when the submit button has already been disabled. When this happens, it is not possible to get the original text show at the button when it is enabled back since the "ujs-enabla-with" attribute has been overwritten with the "disabled-with" text in the second call to the disableFormElement. I wonder if this function shouldn't check if ujs-enable-with already exists and, in that case, don't overwrite it. This is what I mean:
disableFormElements: function(form) {
form.find(rails.disableSelector).each(function() {
var element = $(this), method = element.is('button') ? 'html' : 'val';
/*//// ORIGINAL LINE:
element.data('ujs:enable-with', element[method]()); */
//// NEW LINE
if (!element.data('ujs:enable-with')) element.data('ujs:enable-with', element[method]());
element[method](element.data('disable-with'));
element.prop('disabled', true);
});
},
/* Re-enables disabled form elements:
- Replaces element text with cached value from 'ujs:enable-with' data store (created in `disableFormElements`)
- Sets disabled property to false
*/
enableFormElements: function(form) {
form.find(rails.enableSelector).each(function() {
var element = $(this), method = element.is('button') ? 'html' : 'val';
/* ORIGINAL LINE
if (element.data('ujs:enable-with')) element[method](element.data('ujs:enable-with')); */
//// NEW LINES
if (element.data('ujs:enable-with')){
element[method](element.data('ujs:enable-with'));
element.data('ujs:enable-with', null);
}
element.prop('disabled', false);
});
},
I've opened also an issue for the remotipart project in order to know if this is a problem of remotipar: https://github.com/JangoSteve/remotipart/issues/97
@arojoal could you work on a Pull Request for these changes?