Permalink
Browse files

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
  • Loading branch information...
1 parent 6186a85 commit 8b5c544e533505154822de7fa40489f3b0cbd7a8 @ronalchn ronalchn committed Feb 25, 2012
Showing with 31 additions and 4 deletions.
  1. +3 −4 src/rails.js
  2. +28 −0 test/public/test/call-remote-callbacks.js
View
@@ -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');
@@ -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() {

0 comments on commit 8b5c544

Please sign in to comment.