Skip to content

Commit be6f905

Browse files
committed
Add support for data-confirm attribute on button elements.
This should be supported as the `button_to` rails helper produces the expected `data-*` attributes. Closes #352.
1 parent 4f8fc46 commit be6f905

File tree

2 files changed

+95
-3
lines changed

2 files changed

+95
-3
lines changed

src/rails.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
linkClickSelector: 'a[data-confirm], a[data-method], a[data-remote], a[data-disable-with]',
2626

2727
// Button elements bound by jquery-ujs
28-
buttonClickSelector: 'button[data-remote]',
28+
buttonClickSelector: 'button[data-remote], button[data-confirm]',
2929

3030
// Select elements bound by jquery-ujs
3131
inputChangeSelector: 'select[data-remote], input[data-remote], textarea[data-remote]',

test/public/test/data-confirm.js

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ module('data-confirm', {
77
text: 'my social security number'
88
}));
99

10+
$('#qunit-fixture').append($('<button />', {
11+
'data-url': '/echo',
12+
'data-remote': 'true',
13+
'data-confirm': 'Are you absolutely sure?',
14+
text: 'Click me'
15+
}));
16+
1017
this.windowConfirm = window.confirm;
1118
},
1219
teardown: function() {
@@ -35,6 +42,28 @@ asyncTest('clicking on a link with data-confirm attribute. Confirm yes.', 6, fun
3542
.trigger('click');
3643
});
3744

45+
asyncTest('clicking on a button with data-confirm attribute. Confirm yes.', 6, function() {
46+
var message;
47+
// auto-confirm:
48+
window.confirm = function(msg) { message = msg; return true };
49+
50+
$('button[data-confirm]')
51+
.bind('confirm:complete', function(e, data) {
52+
App.assertCallbackInvoked('confirm:complete');
53+
ok(data == true, 'confirm:complete passes in confirm answer (true)');
54+
})
55+
.bind('ajax:success', function(e, data, status, xhr) {
56+
console.log(xhr);
57+
App.assertCallbackInvoked('ajax:success');
58+
App.assertRequestPath(data, '/echo');
59+
App.assertGetRequest(data);
60+
61+
equal(message, 'Are you absolutely sure?');
62+
start();
63+
})
64+
.trigger('click');
65+
});
66+
3867
asyncTest('clicking on a link with data-confirm attribute. Confirm No.', 3, function() {
3968
var message;
4069
// auto-decline:
@@ -56,8 +85,28 @@ asyncTest('clicking on a link with data-confirm attribute. Confirm No.', 3, func
5685
}, 50);
5786
});
5887

88+
asyncTest('clicking on a button with data-confirm attribute. Confirm No.', 3, function() {
89+
var message;
90+
// auto-decline:
91+
window.confirm = function(msg) { message = msg; return false };
92+
93+
$('button[data-confirm]')
94+
.bind('confirm:complete', function(e, data) {
95+
App.assertCallbackInvoked('confirm:complete');
96+
ok(data == false, 'confirm:complete passes in confirm answer (false)');
97+
})
98+
.bind('ajax:beforeSend', function(e, data, status, xhr) {
99+
App.assertCallbackNotInvoked('ajax:beforeSend');
100+
})
101+
.trigger('click');
102+
103+
setTimeout(function() {
104+
equal(message, 'Are you absolutely sure?');
105+
start();
106+
}, 50);
107+
});
59108

60-
asyncTest('binding to confirm event and returning false', 1, function() {
109+
asyncTest('binding to confirm event of a link and returning false', 1, function() {
61110
// redefine confirm function so we can make sure it's not called
62111
window.confirm = function(msg) {
63112
ok(false, 'confirm dialog should not be called');
@@ -78,7 +127,28 @@ asyncTest('binding to confirm event and returning false', 1, function() {
78127
}, 50);
79128
});
80129

81-
asyncTest('binding to confirm:complete event and returning false', 2, function() {
130+
asyncTest('binding to confirm event of a button and returning false', 1, function() {
131+
// redefine confirm function so we can make sure it's not called
132+
window.confirm = function(msg) {
133+
ok(false, 'confirm dialog should not be called');
134+
};
135+
136+
$('button[data-confirm]')
137+
.bind('confirm', function() {
138+
App.assertCallbackInvoked('confirm');
139+
return false;
140+
})
141+
.bind('confirm:complete', function() {
142+
App.assertCallbackNotInvoked('confirm:complete');
143+
})
144+
.trigger('click');
145+
146+
setTimeout(function() {
147+
start();
148+
}, 50);
149+
});
150+
151+
asyncTest('binding to confirm:complete event of a link and returning false', 2, function() {
82152
// auto-confirm:
83153
window.confirm = function(msg) {
84154
ok(true, 'confirm dialog should be called');
@@ -99,3 +169,25 @@ asyncTest('binding to confirm:complete event and returning false', 2, function()
99169
start();
100170
}, 50);
101171
});
172+
173+
asyncTest('binding to confirm:complete event of a button and returning false', 2, function() {
174+
// auto-confirm:
175+
window.confirm = function(msg) {
176+
ok(true, 'confirm dialog should be called');
177+
return true;
178+
};
179+
180+
$('button[data-confirm]')
181+
.bind('confirm:complete', function() {
182+
App.assertCallbackInvoked('confirm:complete');
183+
return false;
184+
})
185+
.bind('ajax:beforeSend', function() {
186+
App.assertCallbackNotInvoked('ajax:beforeSend');
187+
})
188+
.trigger('click');
189+
190+
setTimeout(function() {
191+
start();
192+
}, 50);
193+
});

0 commit comments

Comments
 (0)