diff --git a/src/rails.js b/src/rails.js index 39cf9158..4865dcd7 100644 --- a/src/rails.js +++ b/src/rails.js @@ -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; @@ -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); @@ -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); @@ -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); @@ -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; diff --git a/test/public/test/data-remote.js b/test/public/test/data-remote.js index f8c6b2a8..c0b47586 100644 --- a/test/public/test/data-remote.js +++ b/test/public/test/data-remote.js @@ -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( + $('