Skip to content

Commit 42e39e9

Browse files
committed
simplify main event handlers; remove "data-remote" support for submit buttons
- now with fewer `live()` listeners (2 instead of 4) - "data-remote" for submit buttons is not supported anymore
1 parent 0ff66c4 commit 42e39e9

File tree

2 files changed

+32
-61
lines changed

2 files changed

+32
-61
lines changed

src/rails.js

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -46,39 +46,10 @@
4646
});
4747
}
4848

49-
/**
50-
* confirmation handler
51-
*/
52-
$('a[data-confirm], button[data-confirm], input[data-confirm]').live('click.rails', function() {
53-
var el = $(this);
54-
if (fire(el, 'confirm')) {
55-
if (!confirm(el.attr('data-confirm'))) {
56-
return false;
57-
}
58-
}
59-
});
60-
61-
/**
62-
* remote handlers
63-
*/
64-
$('form[data-remote]').live('submit.rails', function(e) {
65-
handleRemote($(this));
66-
e.preventDefault();
67-
});
68-
69-
$('a[data-remote],input[data-remote]').live('click.rails', function(e) {
70-
handleRemote($(this));
71-
e.preventDefault();
72-
});
73-
74-
/**
75-
* <%= link_to "Delete", user_path(@user), :method => :delete, :confirm => "Are you sure?" %>
76-
*
77-
* <a href="/users/5" data-confirm="Are you sure?" data-method="delete" rel="nofollow">Delete</a>
78-
*/
79-
$('a[data-method]:not([data-remote])').live('click.rails', function(e) {
80-
var link = $(this),
81-
href = link.attr('href'),
49+
// Handles "data-method" on links such as:
50+
// <a href="/users/5" data-method="delete" rel="nofollow" data-confirm="Are you sure?">Delete</a>
51+
function handleMethod(link) {
52+
var href = link.attr('href'),
8253
method = link.attr('data-method'),
8354
csrf_token = $('meta[name=csrf-token]').attr('content'),
8455
csrf_param = $('meta[name=csrf-param]').attr('content'),
@@ -90,9 +61,35 @@
9061
}
9162

9263
form.hide().append(metadata_input).appendTo('body');
93-
94-
e.preventDefault();
9564
form.submit();
65+
}
66+
67+
function allowAction(element) {
68+
var message = element.attr('data-confirm');
69+
return !message || (fire(element, 'confirm') && confirm(message));
70+
}
71+
72+
$('a[data-confirm], a[data-method], a[data-remote]').live('click.rails', function(e) {
73+
var link = $(this);
74+
if (!allowAction(link)) return false;
75+
76+
if (link.attr('data-remote')) {
77+
handleRemote(link);
78+
return false;
79+
} else if (link.attr('data-method')) {
80+
handleMethod(link);
81+
return false;
82+
}
83+
});
84+
85+
$('form').live('submit.rails', function(e) {
86+
var form = $(this);
87+
if (!allowAction(form)) return false;
88+
89+
if (form.attr('data-remote')) {
90+
handleRemote(form);
91+
return false;
92+
}
9693
});
9794

9895
/**

test/public/test/data-remote.js

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,6 @@ module('data-remote', {
88
'data-remote': 'true',
99
text: 'my address'
1010
}));
11-
12-
$('#fixtures').append($('<input />', {
13-
href: '/echo',
14-
'data-remote': 'true',
15-
name: 'submit',
16-
type: 'submit',
17-
value: 'Click me'
18-
}));
1911

2012
var form = $('<form />', {
2113
action: '/echo',
@@ -52,24 +44,6 @@ test('clicking on a link with data-remote attribute', function() {
5244
.trigger('click');
5345
});
5446

55-
test('clicking on Submit input tag with data-remote attribute', function() {
56-
expect(3);
57-
stop(App.ajax_timeout);
58-
59-
$('input[data-remote]')
60-
.live('ajax:success', function(e, data, status, xhr) {
61-
App.assert_callback_invoked('ajax:success');
62-
63-
var request_env = data.request_env;
64-
65-
App.assert_request_path(request_env, '/echo');
66-
App.assert_get_request(request_env);
67-
68-
start();
69-
})
70-
.trigger('click');
71-
});
72-
7347
test('Submitting form with data-remote attribute', function() {
7448
expect(4);
7549
stop(App.ajax_timeout);

0 commit comments

Comments
 (0)