Skip to content

Commit 8eabf3a

Browse files
committed
Added a[disabled=disabled] selector to data-remote so if a link has that attribute, it won't send the request via ajax. Test added to validate implementation
1 parent 610dfb8 commit 8eabf3a

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/rails.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
// Link onClick disable selector with possible reenable after remote submission
4848
linkDisableSelector: 'a[data-disable-with]',
4949

50+
// Link disabled should not be able to transmit ajax request
51+
linkDisabledAttribute: 'a[disabled=disabled]',
52+
5053
// Make sure that every Ajax request sends the CSRF token
5154
CSRFProtection: function(xhr) {
5255
var token = $('meta[name="csrf-token"]').attr('content');
@@ -293,7 +296,7 @@
293296

294297
$(document).delegate(rails.linkClickSelector, 'click.rails', function(e) {
295298
var link = $(this), method = link.data('method'), data = link.data('params');
296-
if (!rails.allowAction(link)) return rails.stopEverything(e);
299+
if (!rails.allowAction(link) || link.is(rails.linkDisabledAttribute)) return rails.stopEverything(e);
297300

298301
if (link.is(rails.linkDisableSelector)) rails.disableElement(link);
299302

test/public/test/data-remote.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ module('data-remote', {
1212
'data-remote': 'true',
1313
method: 'post'
1414
}))
15+
.append($('<a />', {
16+
href: '/echo',
17+
'data-remote': 'true',
18+
disabled: 'disabled',
19+
text: 'Disabed link'
20+
}))
1521
.find('form').append($('<input type="text" name="user_name" value="john">'));
1622

1723
}
@@ -66,7 +72,7 @@ asyncTest('ctrl-clicking on a link still fires ajax for non-GET links and for li
6672
});
6773

6874
asyncTest('clicking on a link with data-remote attribute', 5, function() {
69-
$('a[data-remote]')
75+
$('a[data-remote]:not([disabled=disabled])')
7076
.bind('ajax:success', function(e, data, status, xhr) {
7177
App.assert_callback_invoked('ajax:success');
7278
App.assert_request_path(data, '/echo');
@@ -78,6 +84,15 @@ asyncTest('clicking on a link with data-remote attribute', 5, function() {
7884
.trigger('click');
7985
});
8086

87+
asyncTest('clicking on a link with disabled attribute', 0, function() {
88+
$('a[disabled=disabled]')
89+
.bind("ajax:before", function(e, data, status, xhr) {
90+
ok(false, "should not send the request");
91+
})
92+
.trigger('click')
93+
setTimeout(function(){ start(); }, 13);
94+
});
95+
8196
asyncTest('changing a select option with data-remote attribute', 5, function() {
8297
$('form')
8398
.append(

0 commit comments

Comments
 (0)