Skip to content

Commit

Permalink
return true from ctrl-click and meta-click for remote links
Browse files Browse the repository at this point in the history
  • Loading branch information
JangoSteve committed Oct 3, 2011
1 parent 888be8a commit 287b929
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/rails.js
Expand Up @@ -291,12 +291,13 @@
});

$(rails.linkClickSelector).live('click.rails', function(e) {
var link = $(this);
var link = $(this), method = link.data('method'), data = link.data('params');
if (!rails.allowAction(link)) return rails.stopEverything(e);

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

if (link.data('remote') !== undefined) {
if ( (e.metaKey || e.ctrlKey) && (!method || method === 'GET') && !data ) { return true; }
rails.handleRemote(link);
return false;
} else if (link.data('method')) {
Expand Down
45 changes: 45 additions & 0 deletions test/public/test/data-remote.js
Expand Up @@ -17,6 +17,51 @@ module('data-remote', {
}
});

asyncTest('ctrl-clicking on a link does not fire ajaxyness', 0, function() {
var link = $('a[data-remote]'), e;

// Ideally, we'd setup an iframe to intercept normal link clicks
// and add a test to make sure the iframe:loaded event is triggered.
// However, jquery doesn't actually cause a native `click` event and
// follow links using `trigger('click')`, it only fires bindings.
link
.removeAttr('data-params')
.bind('ajax:beforeSend', function() {
ok(false, 'ajax should not be triggered');
});

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

e = $.Event('click');
e.ctrlKey = true;
link.trigger(e);

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

asyncTest('ctrl-clicking on a link still fires ajax for non-GET links and for links with "data-params"', 2, function() {
var link = $('a[data-remote]'), e;
e = $.Event('click');
e.metaKey = true;

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

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

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

asyncTest('clicking on a link with data-remote attribute', 5, function() {
$('a[data-remote]')
.bind('ajax:success', function(e, data, status, xhr) {
Expand Down

0 comments on commit 287b929

Please sign in to comment.