From a940d59670f9db513f4c7b073bb78c3df8a73dd8 Mon Sep 17 00:00:00 2001 From: Alexander Kaupanin Date: Fri, 20 Sep 2013 16:11:56 +0400 Subject: [PATCH 1/2] 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" (``) --- src/rails.js | 14 ++++- test/public/test/data-remote.js | 93 +++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+), 2 deletions(-) diff --git a/src/rails.js b/src/rails.js index 39cf9158..1434a19e 100644 --- a/src/rails.js +++ b/src/rails.js @@ -97,6 +97,13 @@ return element.attr('href'); }, + // Checks whether "data-remote" is falsy or not to prevent remote submission if "data-remote"="false" + // because jQuery(selector).data("remote") === "false" if data attribute is string "false", + // e.g. + 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 +374,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,6 +394,8 @@ $document.delegate(rails.buttonClickSelector, 'click.rails', function(e) { var button = $(this); + + if (! rails.isRemote(button)) return false; if (!rails.allowAction(button)) return rails.stopEverything(e); @@ -404,6 +413,7 @@ $document.delegate(rails.inputChangeSelector, 'change.rails', function(e) { var link = $(this); + if (! rails.isRemote(link)) return false; if (!rails.allowAction(link)) return rails.stopEverything(e); rails.handleRemote(link); @@ -412,7 +422,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..6a8e7706 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( + $(' + // 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; }, @@ -395,9 +393,7 @@ $document.delegate(rails.buttonClickSelector, 'click.rails', function(e) { var button = $(this); - if (! rails.isRemote(button)) return false; - - 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); @@ -413,7 +409,7 @@ $document.delegate(rails.inputChangeSelector, 'change.rails', function(e) { var link = $(this); - if (! rails.isRemote(link)) return false; + if (! rails.isRemote(link)) return rails.stopEverything(e); if (!rails.allowAction(link)) return rails.stopEverything(e); rails.handleRemote(link); diff --git a/test/public/test/data-remote.js b/test/public/test/data-remote.js index 6a8e7706..c0b47586 100644 --- a/test/public/test/data-remote.js +++ b/test/public/test/data-remote.js @@ -187,7 +187,7 @@ 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() { +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() { @@ -201,7 +201,7 @@ asyncTest('clicking on a link with falsy "data-remote" attribute does not fire a 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() { +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;