From 8b5c544e533505154822de7fa40489f3b0cbd7a8 Mon Sep 17 00:00:00 2001 From: Ronald Chan Date: Sat, 25 Feb 2012 18:56:11 +1300 Subject: [PATCH] handleRemote - move crossDomain and dataType statements to read from data attribute after 'ajax:before' event, to give it an opportunity to change these values dynamically added tests for dataType and crossDomain modifiable in ajax:before --- src/rails.js | 7 +++--- test/public/test/call-remote-callbacks.js | 28 +++++++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/rails.js b/src/rails.js index 06b4e0b5..b16d284b 100644 --- a/src/rails.js +++ b/src/rails.js @@ -101,12 +101,11 @@ // Submits "remote" forms and links with ajax handleRemote: function(element) { - var method, url, data, - crossDomain = element.data('cross-domain') || null, - dataType = element.data('type') || ($.ajaxSettings && $.ajaxSettings.dataType), - options; + var method, url, data, crossDomain, dataType, options; if (rails.fire(element, 'ajax:before')) { + crossDomain = element.data('cross-domain') || null; + dataType = element.data('type') || ($.ajaxSettings && $.ajaxSettings.dataType); if (element.is('form')) { method = element.attr('method'); diff --git a/test/public/test/call-remote-callbacks.js b/test/public/test/call-remote-callbacks.js index 939f523f..0a160f0d 100644 --- a/test/public/test/call-remote-callbacks.js +++ b/test/public/test/call-remote-callbacks.js @@ -47,6 +47,34 @@ asyncTest('modifying form fields with "ajax:before" sends modified data in reque }); }); +asyncTest('modifying data("type") with "ajax:before" requests new dataType in request', 2, function(){ + $('form[data-remote]').data('type','html') + .live('ajax:before', function() { + var form = $(this); + form.data('type','xml') + }); + + submit(function(form) { + form.bind('ajax:beforeSend', function(e, xhr, settings) { + equal(settings.dataType, 'xml', 'modified dataType should have been requested'); + }); + }); +}); + +asyncTest('setting data("cross-domain",true) with "ajax:before" uses new setting in request', 2, function(){ + $('form[data-remote]').data('cross-domain',false) + .live('ajax:before', function() { + var form = $(this); + form.data('cross-domain',true) + }); + + submit(function(form) { + form.bind('ajax:beforeSend', function(e, xhr, settings) { + equal(settings.crossDomain, true, 'setting modified in ajax:before should have forced cross-domain request'); + }); + }); +}); + asyncTest('stopping the "ajax:beforeSend" event aborts the request', 1, function() { submit(function(form) { form.bind('ajax:beforeSend', function() {