|
51 | 51 | // Link onClick disable selector with possible reenable after remote submission
|
52 | 52 | linkDisableSelector: 'a[data-disable-with]',
|
53 | 53 |
|
| 54 | + // Button onClick disable selector with possible reenable after remote submission |
| 55 | + buttonDisableSelector: 'button[data-remote][data-disable-with]', |
| 56 | + |
54 | 57 | // Make sure that every Ajax request sends the CSRF token
|
55 | 58 | CSRFProtection: function(xhr) {
|
56 | 59 | var token = $('meta[name="csrf-token"]').attr('content');
|
|
199 | 202 | */
|
200 | 203 | disableFormElements: function(form) {
|
201 | 204 | rails.formElements(form, rails.disableSelector).each(function() {
|
202 |
| - var element = $(this), method = element.is('button') ? 'html' : 'val'; |
203 |
| - element.data('ujs:enable-with', element[method]()); |
204 |
| - element[method](element.data('disable-with')); |
205 |
| - element.prop('disabled', true); |
| 205 | + rails.disableFormElement($(this)); |
206 | 206 | });
|
207 | 207 | },
|
208 | 208 |
|
| 209 | + disableFormElement: function(element) { |
| 210 | + var method = element.is('button') ? 'html' : 'val'; |
| 211 | + element.data('ujs:enable-with', element[method]()); |
| 212 | + element[method](element.data('disable-with')); |
| 213 | + element.prop('disabled', true); |
| 214 | + }, |
| 215 | + |
209 | 216 | /* Re-enables disabled form elements:
|
210 | 217 | - Replaces element text with cached value from 'ujs:enable-with' data store (created in `disableFormElements`)
|
211 | 218 | - Sets disabled property to false
|
212 | 219 | */
|
213 | 220 | enableFormElements: function(form) {
|
214 | 221 | rails.formElements(form, rails.enableSelector).each(function() {
|
215 |
| - var element = $(this), method = element.is('button') ? 'html' : 'val'; |
216 |
| - if (element.data('ujs:enable-with')) element[method](element.data('ujs:enable-with')); |
217 |
| - element.prop('disabled', false); |
| 222 | + rails.enableFormElement($(this)); |
218 | 223 | });
|
219 | 224 | },
|
220 | 225 |
|
| 226 | + enableFormElement: function(element) { |
| 227 | + var method = element.is('button') ? 'html' : 'val'; |
| 228 | + if (element.data('ujs:enable-with')) element[method](element.data('ujs:enable-with')); |
| 229 | + element.prop('disabled', false); |
| 230 | + }, |
| 231 | + |
221 | 232 | /* For 'data-confirm' attribute:
|
222 | 233 | - Fires `confirm` event
|
223 | 234 | - Shows the confirmation dialog
|
|
293 | 304 | }
|
294 | 305 | element.unbind('click.railsDisable'); // enable element
|
295 | 306 | }
|
296 |
| - |
297 | 307 | };
|
298 | 308 |
|
299 | 309 | if (rails.fire($document, 'rails:attachBindings')) {
|
|
304 | 314 | rails.enableElement($(this));
|
305 | 315 | });
|
306 | 316 |
|
| 317 | + $document.delegate(rails.buttonDisableSelector, 'ajax:complete', function() { |
| 318 | + rails.enableFormElement($(this)); |
| 319 | + }); |
| 320 | + |
307 | 321 | $document.delegate(rails.linkClickSelector, 'click.rails', function(e) {
|
308 | 322 | var link = $(this), method = link.data('method'), data = link.data('params'), metaClick = e.metaKey || e.ctrlKey;
|
309 | 323 | if (!rails.allowAction(link)) return rails.stopEverything(e);
|
|
332 | 346 | var button = $(this);
|
333 | 347 | if (!rails.allowAction(button)) return rails.stopEverything(e);
|
334 | 348 |
|
335 |
| - rails.handleRemote(button); |
| 349 | + if (button.is(rails.buttonDisableSelector)) rails.disableFormElement(button); |
| 350 | + |
| 351 | + var handleRemote = rails.handleRemote(button); |
| 352 | + // response from rails.handleRemote() will either be false or a deferred object promise. |
| 353 | + if (handleRemote === false) { |
| 354 | + rails.enableFormElement(button); |
| 355 | + } else { |
| 356 | + handleRemote.error( function() { rails.enableFormElement(button); } ); |
| 357 | + } |
336 | 358 | return false;
|
337 | 359 | });
|
338 | 360 |
|
|
0 commit comments