|
51 | 51 | // Link onClick disable selector with possible reenable after remote submission |
52 | 52 | linkDisableSelector: 'a[data-disable-with], a[data-disable]', |
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'); |
|
189 | 192 | // If form is actually a "form" element this will return associated elements outside the from that have |
190 | 193 | // the html form attribute set |
191 | 194 | formElements: function(form, selector) { |
192 | | - return form.is('form') ? $(form[0].elements).filter(selector) : form.find(selector) |
| 195 | + return form.is('form') ? $(form[0].elements).filter(selector) : form.find(selector); |
193 | 196 | }, |
194 | 197 |
|
195 | 198 | /* Disables form elements: |
|
199 | 202 | */ |
200 | 203 | disableFormElements: function(form) { |
201 | 204 | rails.formElements(form, rails.disableSelector).each(function() { |
202 | | - var element, method, replacement; |
| 205 | + rails.disableFormElement($(this)); |
| 206 | + }); |
| 207 | + }, |
203 | 208 |
|
204 | | - element = $(this); |
205 | | - method = element.is('button') ? 'html' : 'val'; |
206 | | - replacement = element.data('disable-with'); |
| 209 | + disableFormElement: function(element) { |
| 210 | + var method, replacement; |
207 | 211 |
|
208 | | - element.data('ujs:enable-with', element[method]()); |
209 | | - if (replacement !== undefined) { |
210 | | - element[method](replacement); |
211 | | - } |
| 212 | + method = element.is('button') ? 'html' : 'val'; |
| 213 | + replacement = element.data('disable-with'); |
212 | 214 |
|
213 | | - element.prop('disabled', true); |
214 | | - }); |
| 215 | + element.data('ujs:enable-with', element[method]()); |
| 216 | + if (replacement !== undefined) { |
| 217 | + element[method](replacement); |
| 218 | + } |
| 219 | + |
| 220 | + element.prop('disabled', true); |
215 | 221 | }, |
216 | 222 |
|
217 | 223 | /* Re-enables disabled form elements: |
|
220 | 226 | */ |
221 | 227 | enableFormElements: function(form) { |
222 | 228 | rails.formElements(form, rails.enableSelector).each(function() { |
223 | | - var element = $(this), method = element.is('button') ? 'html' : 'val'; |
224 | | - if (element.data('ujs:enable-with')) element[method](element.data('ujs:enable-with')); |
225 | | - element.prop('disabled', false); |
| 229 | + rails.enableFormElement($(this)); |
226 | 230 | }); |
227 | 231 | }, |
228 | 232 |
|
| 233 | + enableFormElement: function(element) { |
| 234 | + var method = element.is('button') ? 'html' : 'val'; |
| 235 | + if (element.data('ujs:enable-with')) element[method](element.data('ujs:enable-with')); |
| 236 | + element.prop('disabled', false); |
| 237 | + }, |
| 238 | + |
229 | 239 | /* For 'data-confirm' attribute: |
230 | 240 | - Fires `confirm` event |
231 | 241 | - Shows the confirmation dialog |
|
306 | 316 | } |
307 | 317 | element.unbind('click.railsDisable'); // enable element |
308 | 318 | } |
309 | | - |
310 | 319 | }; |
311 | 320 |
|
312 | 321 | if (rails.fire($document, 'rails:attachBindings')) { |
|
317 | 326 | rails.enableElement($(this)); |
318 | 327 | }); |
319 | 328 |
|
| 329 | + $document.delegate(rails.buttonDisableSelector, 'ajax:complete', function() { |
| 330 | + rails.enableFormElement($(this)); |
| 331 | + }); |
| 332 | + |
320 | 333 | $document.delegate(rails.linkClickSelector, 'click.rails', function(e) { |
321 | 334 | var link = $(this), method = link.data('method'), data = link.data('params'), metaClick = e.metaKey || e.ctrlKey; |
322 | 335 | if (!rails.allowAction(link)) return rails.stopEverything(e); |
|
345 | 358 | var button = $(this); |
346 | 359 | if (!rails.allowAction(button)) return rails.stopEverything(e); |
347 | 360 |
|
348 | | - rails.handleRemote(button); |
| 361 | + if (button.is(rails.buttonDisableSelector)) rails.disableFormElement(button); |
| 362 | + |
| 363 | + var handleRemote = rails.handleRemote(button); |
| 364 | + // response from rails.handleRemote() will either be false or a deferred object promise. |
| 365 | + if (handleRemote === false) { |
| 366 | + rails.enableFormElement(button); |
| 367 | + } else { |
| 368 | + handleRemote.error( function() { rails.enableFormElement(button); } ); |
| 369 | + } |
349 | 370 | return false; |
350 | 371 | }); |
351 | 372 |
|
|
0 commit comments