Skip to content

Commit e9b99b9

Browse files
simsalabimAlexander Kaupanin
authored andcommitted
Don't fire ajaxyness if "data-remote"="false", because jQuery("#my_id").data("remote") === false if input's data-remote is set to string "false" (<input id="my_id" data-remote="false" />)
1 parent 77567e7 commit e9b99b9

File tree

2 files changed

+105
-2
lines changed

2 files changed

+105
-2
lines changed

src/rails.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@
9797
return element.attr('href');
9898
},
9999

100+
// Checks whether "data-remote" is falsy or not to prevent remote submission if "data-remote"="false"
101+
// because jQuery(selector).data("remote") === "false" if data attribute is string "false",
102+
// e.g. <input id="id" data-remote="false" />
103+
isRemote: function(element) {
104+
return element.data('remote') !== undefined && element.data('remote') !== false;
105+
},
106+
100107
// Submits "remote" forms and links with ajax
101108
handleRemote: function(element) {
102109
var method, url, data, elCrossDomain, crossDomain, withCredentials, dataType, options;
@@ -367,7 +374,7 @@
367374

368375
if (!metaClick && link.is(rails.linkDisableSelector)) rails.disableElement(link);
369376

370-
if (link.data('remote') !== undefined) {
377+
if (rails.isRemote(link)) {
371378
if (metaClick && (!method || method === 'GET') && !data) { return true; }
372379

373380
var handleRemote = rails.handleRemote(link);
@@ -387,6 +394,8 @@
387394

388395
$document.delegate(rails.buttonClickSelector, 'click.rails', function(e) {
389396
var button = $(this);
397+
398+
if (! rails.isRemote(button)) return false;
390399

391400
if (!rails.allowAction(button)) return rails.stopEverything(e);
392401

@@ -404,6 +413,7 @@
404413

405414
$document.delegate(rails.inputChangeSelector, 'change.rails', function(e) {
406415
var link = $(this);
416+
if (! rails.isRemote(link)) return false;
407417
if (!rails.allowAction(link)) return rails.stopEverything(e);
408418

409419
rails.handleRemote(link);
@@ -412,7 +422,7 @@
412422

413423
$document.delegate(rails.formSubmitSelector, 'submit.rails', function(e) {
414424
var form = $(this),
415-
remote = form.data('remote') !== undefined,
425+
remote = rails.isRemote(form),
416426
blankRequiredInputs,
417427
nonBlankFileInputs;
418428

test/public/test/data-remote.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,96 @@ asyncTest('returning false in form\'s submit bindings in non-submit-bubbling bro
186186

187187
setTimeout(function(){ start(); }, 13);
188188
});
189+
190+
asyncTest('clicking on a link with falsy "data-remote" attribute does not fire ajaxyness ', 0, function() {
191+
$('a[data-remote]')
192+
.attr('data-remote', 'false')
193+
.bind('ajax:beforeSend', function() {
194+
ok(false, 'ajax should not be triggered');
195+
})
196+
.bind('click', function() {
197+
return false;
198+
})
199+
.trigger('click');
200+
201+
setTimeout(function(){ start(); }, 20);
202+
});
203+
204+
asyncTest('ctrl-clicking on a link with falsy "data-remote" attribute does not fire ajaxyness even if "data-params" present', 0, function() {
205+
var link = $('a[data-remote]'), e;
206+
e = $.Event('click');
207+
e.metaKey = true;
208+
209+
link
210+
.removeAttr('data-params')
211+
.attr('data-remote', 'false')
212+
.attr('data-method', 'POST')
213+
.bind('ajax:beforeSend', function() {
214+
ok(false, 'ajax should not be triggered');
215+
})
216+
.bind('click', function() {
217+
return false;
218+
})
219+
.trigger(e);
220+
221+
e = $.Event('click');
222+
e.metaKey = true;
223+
224+
link
225+
.removeAttr('data-method')
226+
.attr('data-params', 'name=steve')
227+
.trigger(e);
228+
229+
setTimeout(function(){ start(); }, 20);
230+
});
231+
232+
asyncTest('clicking on a button with falsy "data-remote" attribute', 0, function() {
233+
$('button[data-remote]:first')
234+
.attr('data-remote', 'false')
235+
.bind('ajax:beforeSend', function() {
236+
ok(false, 'ajax should not be triggered');
237+
})
238+
.bind('click', function() {
239+
return false;
240+
})
241+
.trigger('click');
242+
243+
setTimeout(function(){ start(); }, 20);
244+
});
245+
246+
asyncTest('submitting a form with falsy "data-remote" attribute', 0, function() {
247+
$('form[data-remote]:first')
248+
.attr('data-remote', 'false')
249+
.bind('ajax:beforeSend', function() {
250+
ok(false, 'ajax should not be triggered');
251+
})
252+
.bind('submit', function() {
253+
return false;
254+
})
255+
.trigger('submit');
256+
257+
setTimeout(function(){ start(); }, 20);
258+
});
259+
260+
asyncTest('changing a select option with falsy "data-remote" attribute', 0, function() {
261+
$('form')
262+
.append(
263+
$('<select />', {
264+
'name': 'user_data',
265+
'data-remote': 'false',
266+
'data-params': 'data1=value1',
267+
'data-url': '/echo'
268+
})
269+
.append($('<option />', {value: 'optionValue1', text: 'option1'}))
270+
.append($('<option />', {value: 'optionValue2', text: 'option2'}))
271+
);
272+
273+
$('select[data-remote=false]:first')
274+
.bind('ajax:beforeSend', function() {
275+
ok(false, 'ajax should not be triggered');
276+
})
277+
.val('optionValue2')
278+
.trigger('change');
279+
280+
setTimeout(function(){ start(); }, 20);
281+
});

0 commit comments

Comments
 (0)