Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/rails.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@
return element.attr('href');
},

// Checks "data-remote" if true to handle the request through a XHR request.
isRemote: function(element) {
return element.data('remote') !== undefined && element.data('remote') !== false;
},

// Submits "remote" forms and links with ajax
handleRemote: function(element) {
var method, url, data, elCrossDomain, crossDomain, withCredentials, dataType, options;
Expand Down Expand Up @@ -367,7 +372,7 @@

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

if (link.data('remote') !== undefined) {
if (rails.isRemote(link)) {
if (metaClick && (!method || method === 'GET') && !data) { return true; }

var handleRemote = rails.handleRemote(link);
Expand All @@ -387,8 +392,8 @@

$document.delegate(rails.buttonClickSelector, 'click.rails', function(e) {
var button = $(this);

if (!rails.allowAction(button)) return rails.stopEverything(e);
if (!rails.allowAction(button) || !rails.isRemote(button)) return rails.stopEverything(e);

if (button.is(rails.buttonDisableSelector)) rails.disableFormElement(button);

Expand All @@ -404,6 +409,7 @@

$document.delegate(rails.inputChangeSelector, 'change.rails', function(e) {
var link = $(this);
if (! rails.isRemote(link)) return rails.stopEverything(e);
if (!rails.allowAction(link)) return rails.stopEverything(e);

rails.handleRemote(link);
Expand All @@ -412,7 +418,7 @@

$document.delegate(rails.formSubmitSelector, 'submit.rails', function(e) {
var form = $(this),
remote = form.data('remote') !== undefined,
remote = rails.isRemote(form),
blankRequiredInputs,
nonBlankFileInputs;

Expand Down
93 changes: 93 additions & 0 deletions test/public/test/data-remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,96 @@ asyncTest('returning false in form\'s submit bindings in non-submit-bubbling bro

setTimeout(function(){ start(); }, 13);
});

asyncTest('clicking on a link with falsy "data-remote" attribute does not fire ajaxyness', 0, function() {
$('a[data-remote]')
.attr('data-remote', 'false')
.bind('ajax:beforeSend', function() {
ok(false, 'ajax should not be triggered');
})
.bind('click', function() {
return false;
})
.trigger('click');

setTimeout(function(){ start(); }, 20);
});

asyncTest('ctrl-clicking on a link with falsy "data-remote" attribute does not fire ajaxyness even if "data-params" present', 0, function() {
var link = $('a[data-remote]'), e;
e = $.Event('click');
e.metaKey = true;

link
.removeAttr('data-params')
.attr('data-remote', 'false')
.attr('data-method', 'POST')
.bind('ajax:beforeSend', function() {
ok(false, 'ajax should not be triggered');
})
.bind('click', function() {
return false;
})
.trigger(e);

e = $.Event('click');
e.metaKey = true;

link
.removeAttr('data-method')
.attr('data-params', 'name=steve')
.trigger(e);

setTimeout(function(){ start(); }, 20);
});

asyncTest('clicking on a button with falsy "data-remote" attribute', 0, function() {
$('button[data-remote]:first')
.attr('data-remote', 'false')
.bind('ajax:beforeSend', function() {
ok(false, 'ajax should not be triggered');
})
.bind('click', function() {
return false;
})
.trigger('click');

setTimeout(function(){ start(); }, 20);
});

asyncTest('submitting a form with falsy "data-remote" attribute', 0, function() {
$('form[data-remote]:first')
.attr('data-remote', 'false')
.bind('ajax:beforeSend', function() {
ok(false, 'ajax should not be triggered');
})
.bind('submit', function() {
return false;
})
.trigger('submit');

setTimeout(function(){ start(); }, 20);
});

asyncTest('changing a select option with falsy "data-remote" attribute', 0, function() {
$('form')
.append(
$('<select />', {
'name': 'user_data',
'data-remote': 'false',
'data-params': 'data1=value1',
'data-url': '/echo'
})
.append($('<option />', {value: 'optionValue1', text: 'option1'}))
.append($('<option />', {value: 'optionValue2', text: 'option2'}))
);

$('select[data-remote=false]:first')
.bind('ajax:beforeSend', function() {
ok(false, 'ajax should not be triggered');
})
.val('optionValue2')
.trigger('change');

setTimeout(function(){ start(); }, 20);
});