Skip to content

Commit abfd485

Browse files
author
Jason Moon
committed
Don't fire ajax:send if ajax:beforeSend was cancelled. Don't disable form elements until ajax:send.
1 parent 7317021 commit abfd485

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

src/rails.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,11 @@
129129
if (settings.dataType === undefined) {
130130
xhr.setRequestHeader('accept', '*/*;q=0.5, ' + settings.accepts.script);
131131
}
132-
return rails.fire(element, 'ajax:beforeSend', [xhr, settings]);
132+
if (rails.fire(element, 'ajax:beforeSend', [xhr, settings])) {
133+
element.trigger('ajax:send', xhr);
134+
} else {
135+
return false;
136+
}
133137
},
134138
success: function(data, status, xhr) {
135139
element.trigger('ajax:success', [data, status, xhr]);
@@ -154,9 +158,7 @@
154158
// Only pass url to `ajax` options if not blank
155159
if (url) { options.url = url; }
156160

157-
var jqxhr = rails.ajax(options);
158-
element.trigger('ajax:send', jqxhr);
159-
return jqxhr;
161+
return rails.ajax(options);
160162
} else {
161163
return false;
162164
}
@@ -382,7 +384,7 @@
382384
button.closest('form').data('ujs:submit-button', data);
383385
});
384386

385-
$document.delegate(rails.formSubmitSelector, 'ajax:beforeSend.rails', function(event) {
387+
$document.delegate(rails.formSubmitSelector, 'ajax:send.rails', function(event) {
386388
if (this == event.target) rails.disableFormElements($(this));
387389
});
388390

test/public/test/call-remote-callbacks.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module('call-remote-callbacks', {
99
teardown: function() {
1010
$(document).undelegate('form[data-remote]', 'ajax:beforeSend');
1111
$(document).undelegate('form[data-remote]', 'ajax:before');
12+
$(document).undelegate('form[data-remote]', 'ajax:send');
1213
$(document).undelegate('form[data-remote]', 'ajax:complete');
1314
$(document).undelegate('form[data-remote]', 'ajax:success');
1415
$(document).unbind('ajaxStop');
@@ -97,6 +98,9 @@ asyncTest('stopping the "ajax:beforeSend" event aborts the request', 1, function
9798
ok(true, 'aborting request in ajax:beforeSend');
9899
return false;
99100
});
101+
form.unbind('ajax:send').bind('ajax:send', function() {
102+
ok(false, 'ajax:send should not run');
103+
});
100104
form.unbind('ajax:complete').bind('ajax:complete', function() {
101105
ok(false, 'ajax:complete should not run');
102106
});
@@ -315,6 +319,9 @@ asyncTest('"ajax:beforeSend" can be observed and stopped with event delegation',
315319
});
316320

317321
submit(function(form) {
322+
form.unbind('ajax:send').bind('ajax:send', function() {
323+
ok(false, 'ajax:send should not run');
324+
});
318325
form.unbind('ajax:complete').bind('ajax:complete', function() {
319326
ok(false, 'ajax:complete should not run');
320327
});
@@ -324,12 +331,15 @@ asyncTest('"ajax:beforeSend" can be observed and stopped with event delegation',
324331
});
325332
});
326333

327-
asyncTest('"ajax:beforeSend", "ajax:success" and "ajax:complete" are triggered', 8, function() {
334+
asyncTest('"ajax:beforeSend", "ajax:send", "ajax:success" and "ajax:complete" are triggered', 9, function() {
328335
submit(function(form) {
329336
form.bind('ajax:beforeSend', function(e, xhr, settings) {
330337
ok(xhr.setRequestHeader, 'first argument to "ajax:beforeSend" should be an XHR object');
331338
equal(settings.url, '/echo', 'second argument to "ajax:beforeSend" should be a settings object');
332339
});
340+
form.bind('ajax:send', function(e, xhr) {
341+
ok(xhr.abort, 'first argument to "ajax:send" should be an XHR object');
342+
});
333343
form.bind('ajax:success', function(e, data, status, xhr) {
334344
ok(data.REQUEST_METHOD, 'first argument to ajax:success should be a data object');
335345
equal(status, 'success', 'second argument to ajax:success should be a status string');
@@ -342,10 +352,11 @@ asyncTest('"ajax:beforeSend", "ajax:success" and "ajax:complete" are triggered',
342352
});
343353
});
344354

345-
asyncTest('"ajax:beforeSend", "ajax:error" and "ajax:complete" are triggered on error', 6, function() {
355+
asyncTest('"ajax:beforeSend", "ajax:send", "ajax:error" and "ajax:complete" are triggered on error', 7, function() {
346356
submit(function(form) {
347357
form.attr('action', '/error');
348358
form.bind('ajax:beforeSend', function(arg) { ok(true, 'ajax:beforeSend') });
359+
form.bind('ajax:send', function(arg) { ok(true, 'ajax:send') });
349360
form.bind('ajax:error', function(e, xhr, status, error) {
350361
ok(xhr.getResponseHeader, 'first argument to "ajax:error" should be an XHR object');
351362
equal(status, 'error', 'second argument to ajax:error should be a status string');
@@ -358,11 +369,14 @@ asyncTest('"ajax:beforeSend", "ajax:error" and "ajax:complete" are triggered on
358369
});
359370

360371
// IF THIS TEST IS FAILING, TRY INCREASING THE TIMEOUT AT THE BOTTOM TO > 100
361-
asyncTest('binding to ajax callbacks via .delegate() triggers handlers properly', 3, function() {
372+
asyncTest('binding to ajax callbacks via .delegate() triggers handlers properly', 4, function() {
362373
$(document)
363374
.delegate('form[data-remote]', 'ajax:beforeSend', function() {
364375
ok(true, 'ajax:beforeSend handler is triggered');
365376
})
377+
.delegate('form[data-remote]', 'ajax:send', function() {
378+
ok(true, 'ajax:send handler is triggered');
379+
})
366380
.delegate('form[data-remote]', 'ajax:complete', function() {
367381
ok(true, 'ajax:complete handler is triggered');
368382
})

test/public/test/data-disable.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ asyncTest('a[data-remote][data-disable-with] disables and re-enables', 6, functi
159159
checkEnabledState(link, 'Click me');
160160

161161
link
162-
.bind('ajax:beforeSend', function() {
162+
.bind('ajax:send', function() {
163163
checkDisabledState(link, 'clicking...');
164164
})
165165
.bind('ajax:complete', function() {
@@ -213,7 +213,7 @@ asyncTest('a[data-remote][data-disable-with] re-enables when `ajax:error` event
213213
checkEnabledState(link, 'Click me');
214214

215215
link
216-
.bind('ajax:beforeSend', function() {
216+
.bind('ajax:send', function() {
217217
checkDisabledState(link, 'clicking...');
218218
})
219219
.trigger('click');

0 commit comments

Comments
 (0)