Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/rails.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,16 @@
return false;
}

function callFormSubmitBindings(form) {
var events = form.data('events'), continuePropagation = true;
if (events != undefined && events['submit'] != undefined) {
$.each(events['submit'], function(i, obj){
if (typeof obj.handler === 'function') return continuePropagation = obj.handler(obj.data);
})
}
return continuePropagation;
}

$('a[data-confirm], a[data-method], a[data-remote]').live('click.rails', function(e) {
var link = $(this);
if (!allowAction(link)) return stopEverything(e);
Expand All @@ -185,6 +195,10 @@
return fire(form, 'ajax:aborted:file');
}

// If browser does not support submit bubbling, then this live-binding will be called before direct
// bindings. Therefore, we should directly call any direct bindings before remotely submitting form.
if (!$.support.submitBubbles && callFormSubmitBindings(form) == false) return stopEverything(e)

if (remote) {
handleRemote(form);
return false;
Expand Down
51 changes: 51 additions & 0 deletions test/public/test/data-remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,54 @@ asyncTest('submitting form with data-remote attribute', 4, function() {
.bind('ajax:complete', function() { start() })
.trigger('submit');
});

asyncTest('form\'s submit bindings in browsers that don\'t support submit bubbling', 4, function() {
var form = $('form[data-remote]'), directBindingCalled = false;

ok(!directBindingCalled, 'nothing is called');

form
.append($('<input type="submit" />'))
.bind('submit', function(){
ok(true, 'binding handler is called');
directBindingCalled = true;
})
.bind('ajax:beforeSend', function(){
ok(true, 'form being submitted via ajax');
ok(directBindingCalled, 'binding handler already called');
})
.bind('ajax:complete', function(){
start();
});

if(!$.support.submitBubbles) {
// Must indrectly submit form via click to trigger jQuery's manual submit bubbling in IE
form.find('input[type=submit]')
.trigger('click');
} else {
form.trigger('submit');
}
});

asyncTest('returning false in form\'s submit bindings in non-submit-bubbling browsers', 1, function(){
var form = $('form[data-remote]');

form
.append($('<input type="submit" />'))
.bind('submit', function(){
ok(true, 'binding handler is called');
return false;
})
.bind('ajax:beforeSend', function(){
ok(false, 'form should not be submitted');
});

if (!$.support.submitBubbles) {
// Must indrectly submit form via click to trigger jQuery's manual submit bubbling in IE
form.find('input[type=submit]').trigger('click');
} else {
form.trigger('submit');
}

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