|
49 | 49 |
|
50 | 50 | $.rails = rails = { |
51 | 51 | // Link elements bound by jquery-ujs |
52 | | - linkClickSelector: 'a[data-confirm], a[data-method], a[data-remote]', |
| 52 | + linkClickSelector: 'a[data-confirm], a[data-method], a[data-remote], a[data-disable-with]', |
53 | 53 |
|
54 | 54 | // Select elements bound by jquery-ujs |
55 | 55 | inputChangeSelector: 'select[data-remote], input[data-remote], textarea[data-remote]', |
|
72 | 72 | // Form file input elements |
73 | 73 | fileInputSelector: 'input:file', |
74 | 74 |
|
| 75 | + // Link onClick disable selector with possible reenable after remote submission |
| 76 | + linkDisableSelector: 'a[data-disable-with]', |
| 77 | + |
75 | 78 | // Make sure that every Ajax request sends the CSRF token |
76 | 79 | CSRFProtection: function(xhr) { |
77 | 80 | var token = $('meta[name="csrf-token"]').attr('content'); |
|
253 | 256 | }); |
254 | 257 | } |
255 | 258 | return continuePropagation; |
| 259 | + }, |
| 260 | + |
| 261 | + // replace element's html with the 'data-disable-with' after storing original html |
| 262 | + // and prevent clicking on it |
| 263 | + disableElement: function(element) { |
| 264 | + element.data('ujs:enable-with', element.html()); // store enabled state |
| 265 | + element.html(element.data('disable-with')); // set to disabled state |
| 266 | + element.bind('click.railsDisable', function(e) { // prevent further clicking |
| 267 | + return rails.stopEverything(e) |
| 268 | + }); |
| 269 | + }, |
| 270 | + |
| 271 | + // restore element to its original state which was disabled by 'disableElement' above |
| 272 | + enableElement: function(element) { |
| 273 | + if (element.data('ujs:enable-with') !== undefined) { |
| 274 | + element.html(element.data('ujs:enable-with')); // set to old enabled state |
| 275 | + // this should be element.removeData('ujs:enable-with') |
| 276 | + // but, there is currently a bug in jquery which makes hyphenated data attributes not get removed |
| 277 | + element.data('ujs:enable-with', false); // clean up cache |
| 278 | + } |
| 279 | + element.unbind('click.railsDisable'); // enable element |
256 | 280 | } |
| 281 | + |
257 | 282 | }; |
258 | 283 |
|
259 | 284 | $.ajaxPrefilter(function(options, originalOptions, xhr){ if ( !options.crossDomain ) { rails.CSRFProtection(xhr); }}); |
260 | 285 |
|
| 286 | + $(rails.linkDisableSelector).live('ajax:complete', function() { |
| 287 | + rails.enableElement($(this)); |
| 288 | + }); |
| 289 | + |
261 | 290 | $(rails.linkClickSelector).live('click.rails', function(e) { |
262 | 291 | var link = $(this); |
263 | 292 | if (!rails.allowAction(link)) return rails.stopEverything(e); |
264 | 293 |
|
| 294 | + if (link.is(rails.linkDisableSelector)) rails.disableElement(link); |
| 295 | + |
265 | 296 | if (link.data('remote') !== undefined) { |
266 | 297 | rails.handleRemote(link); |
267 | 298 | return false; |
|
0 commit comments