Skip to content
This repository
Browse code

Created test cases and solutions for correcting submit handler call o…

…rder for browsers that don't support event bubbling.

Signed-off-by: Neeraj Singh <neerajdotname@gmail.com>
  • Loading branch information...
commit d2abd6f9df4e4a426c17c218b7d5e05004c768d0 1 parent 2530214
Steve Schwartz JangoSteve authored neerajdotname committed

Showing 2 changed files with 65 additions and 0 deletions. Show diff stats Hide diff stats

  1. +14 0 src/rails.js
  2. +51 0 test/public/test/data-remote.js
14 src/rails.js
@@ -160,6 +160,16 @@
160 160 return false;
161 161 }
162 162
  163 + function callFormSubmitBindings(form) {
  164 + var events = form.data('events'), continuePropagation = true;
  165 + if (events != undefined && events['submit'] != undefined) {
  166 + $.each(events['submit'], function(i, obj){
  167 + if (typeof obj.handler === 'function') return continuePropagation = obj.handler(obj.data);
  168 + })
  169 + }
  170 + return continuePropagation;
  171 + }
  172 +
163 173 $('a[data-confirm], a[data-method], a[data-remote]').live('click.rails', function(e) {
164 174 var link = $(this);
165 175 if (!allowAction(link)) return stopEverything(e);
@@ -185,6 +195,10 @@
185 195 return fire(form, 'ajax:aborted:file');
186 196 }
187 197
  198 + // If browser does not support submit bubbling, then this live-binding will be called before direct
  199 + // bindings. Therefore, we should directly call any direct bindings before remotely submitting form.
  200 + if (!$.support.submitBubbles && callFormSubmitBindings(form) == false) return stopEverything(e)
  201 +
188 202 if (remote) {
189 203 handleRemote(form);
190 204 return false;
51 test/public/test/data-remote.js
@@ -37,3 +37,54 @@ asyncTest('submitting form with data-remote attribute', 4, function() {
37 37 .bind('ajax:complete', function() { start() })
38 38 .trigger('submit');
39 39 });
  40 +
  41 +asyncTest('form\'s submit bindings in browsers that don\'t support submit bubbling', 4, function() {
  42 + var form = $('form[data-remote]'), directBindingCalled = false;
  43 +
  44 + ok(!directBindingCalled, 'nothing is called');
  45 +
  46 + form
  47 + .append($('<input type="submit" />'))
  48 + .bind('submit', function(){
  49 + ok(true, 'binding handler is called');
  50 + directBindingCalled = true;
  51 + })
  52 + .bind('ajax:beforeSend', function(){
  53 + ok(true, 'form being submitted via ajax');
  54 + ok(directBindingCalled, 'binding handler already called');
  55 + })
  56 + .bind('ajax:complete', function(){
  57 + start();
  58 + });
  59 +
  60 + if(!$.support.submitBubbles) {
  61 + // Must indrectly submit form via click to trigger jQuery's manual submit bubbling in IE
  62 + form.find('input[type=submit]')
  63 + .trigger('click');
  64 + } else {
  65 + form.trigger('submit');
  66 + }
  67 +});
  68 +
  69 +asyncTest('returning false in form\'s submit bindings in non-submit-bubbling browsers', 1, function(){
  70 + var form = $('form[data-remote]');
  71 +
  72 + form
  73 + .append($('<input type="submit" />'))
  74 + .bind('submit', function(){
  75 + ok(true, 'binding handler is called');
  76 + return false;
  77 + })
  78 + .bind('ajax:beforeSend', function(){
  79 + ok(false, 'form should not be submitted');
  80 + });
  81 +
  82 + if (!$.support.submitBubbles) {
  83 + // Must indrectly submit form via click to trigger jQuery's manual submit bubbling in IE
  84 + form.find('input[type=submit]').trigger('click');
  85 + } else {
  86 + form.trigger('submit');
  87 + }
  88 +
  89 + setTimeout(function(){ start(); }, 13);
  90 +});

0 comments on commit d2abd6f

Please sign in to comment.
Something went wrong with that request. Please try again.