Skip to content

Commit c00a972

Browse files
committed
Merge branch 'doits-master'
2 parents 3af83ec + eb732f4 commit c00a972

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

src/rails.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949

5050
$.rails = rails = {
5151
// 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]',
5353

5454
// Select elements bound by jquery-ujs
5555
inputChangeSelector: 'select[data-remote], input[data-remote], textarea[data-remote]',
@@ -72,6 +72,9 @@
7272
// Form file input elements
7373
fileInputSelector: 'input:file',
7474

75+
// Link onClick disable selector with possible reenable after remote submission
76+
linkDisableSelector: 'a[data-disable-with]',
77+
7578
// Make sure that every Ajax request sends the CSRF token
7679
CSRFProtection: function(xhr) {
7780
var token = $('meta[name="csrf-token"]').attr('content');
@@ -253,15 +256,43 @@
253256
});
254257
}
255258
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
256280
}
281+
257282
};
258283

259284
$.ajaxPrefilter(function(options, originalOptions, xhr){ if ( !options.crossDomain ) { rails.CSRFProtection(xhr); }});
260285

286+
$(rails.linkDisableSelector).live('ajax:complete', function() {
287+
rails.enableElement($(this));
288+
});
289+
261290
$(rails.linkClickSelector).live('click.rails', function(e) {
262291
var link = $(this);
263292
if (!rails.allowAction(link)) return rails.stopEverything(e);
264293

294+
if (link.is(rails.linkDisableSelector)) rails.disableElement(link);
295+
265296
if (link.data('remote') !== undefined) {
266297
rails.handleRemote(link);
267298
return false;

test/public/test/data-disable.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ module('data-disable', {
1515
.find('form:last')
1616
// WEEIRDD: the form won't submit to an iframe if the button is name="submit" (??!)
1717
.append($('<input type="submit" data-disable-with="submitting ..." name="submit2" value="Submit" />'));
18+
19+
$('#qunit-fixture').append($('<a />', {
20+
text: 'Click me',
21+
href: '/echo',
22+
'data-disable-with': 'clicking...'
23+
}));
1824
}
1925
});
2026

@@ -133,3 +139,44 @@ asyncTest('form textarea with "data-disable-with" attribute', 3, function() {
133139
ok(textarea.is(':disabled'), 'textarea should be disabled');
134140
equal(textarea.val(), 'processing ...', 'textarea should have disabled value given to it');
135141
});
142+
143+
asyncTest('link with "data-disable-with" attribute disables', 4, function() {
144+
var link = $('a[data-disable-with]');
145+
146+
ok(!link.data('ujs:enable-with'), 'link should not be disabled');
147+
equal(link.html(), 'Click me', 'link should have value given to it');
148+
149+
function checkDisabledLink() {
150+
ok(link.data('ujs:enable-with'), 'link should be disabled');
151+
equal(link.html(), 'clicking...');
152+
}
153+
154+
link.trigger('click');
155+
checkDisabledLink();
156+
start();
157+
});
158+
159+
asyncTest('remote link with "data-disable-with" attribute disables and re-enables', 6, function() {
160+
var link = $('a[data-disable-with]').attr('data-remote', true);
161+
162+
function checkEnabledLink() {
163+
ok(!link.data('ujs:enable-with'), 'link should not be disabled');
164+
equal(link.html(), 'Click me', 'link should have value given to it');
165+
}
166+
checkEnabledLink();
167+
168+
function checkDisabledLink() {
169+
ok(link.data('ujs:enable-with'), 'link should be disabled');
170+
equal(link.html(), 'clicking...');
171+
}
172+
173+
link
174+
.bind('ajax:beforeSend', function() {
175+
checkDisabledLink();
176+
})
177+
.live('ajax:complete', function() {
178+
checkEnabledLink();
179+
start();
180+
})
181+
.trigger('click');
182+
});

0 commit comments

Comments
 (0)