Skip to content

Commit

Permalink
Added back 'ajax:before' event, so that form fields can easily be mod…
Browse files Browse the repository at this point in the history
…ified, added, and removed before ajax call.

Signed-off-by: Neeraj Singh <neeraj@railsdog.com>
  • Loading branch information
JangoSteve authored and Neeraj Singh committed Mar 9, 2011
1 parent a284dd7 commit 5b23c3d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 19 deletions.
39 changes: 20 additions & 19 deletions src/rails.js
Expand Up @@ -26,6 +26,7 @@
var method, url, data, var method, url, data,
dataType = element.attr('data-type') || ($.ajaxSettings && $.ajaxSettings.dataType); dataType = element.attr('data-type') || ($.ajaxSettings && $.ajaxSettings.dataType);


if (fire(element, 'ajax:before')) {
if (element.is('form')) { if (element.is('form')) {
method = element.attr('method'); method = element.attr('method');
url = element.attr('action'); url = element.attr('action');
Expand All @@ -41,26 +42,26 @@
url = element.attr('href'); url = element.attr('href');
data = null; data = null;
} }

$.ajax({
$.ajax({ url: url, type: method || 'GET', data: data, dataType: dataType,
url: url, type: method || 'GET', data: data, dataType: dataType, // stopping the "ajax:beforeSend" event will cancel the ajax request
// stopping the "ajax:beforeSend" event will cancel the ajax request beforeSend: function(xhr, settings) {
beforeSend: function(xhr, settings) { if (settings.dataType === undefined) {
if (settings.dataType === undefined) { xhr.setRequestHeader('accept', '*/*;q=0.5, ' + settings.accepts.script);
xhr.setRequestHeader('accept', '*/*;q=0.5, ' + settings.accepts.script); }
return fire(element, 'ajax:beforeSend', [xhr, settings]);
},
success: function(data, status, xhr) {
element.trigger('ajax:success', [data, status, xhr]);
},
complete: function(xhr, status) {
element.trigger('ajax:complete', [xhr, status]);
},
error: function(xhr, status, error) {
element.trigger('ajax:error', [xhr, status, error]);
} }
return fire(element, 'ajax:beforeSend', [xhr, settings]); });
}, }
success: function(data, status, xhr) {
element.trigger('ajax:success', [data, status, xhr]);
},
complete: function(xhr, status) {
element.trigger('ajax:complete', [xhr, status]);
},
error: function(xhr, status, error) {
element.trigger('ajax:error', [xhr, status, error]);
}
});
} }


// Handles "data-method" on links such as: // Handles "data-method" on links such as:
Expand Down
22 changes: 22 additions & 0 deletions test/public/test/call-remote-callbacks.js
Expand Up @@ -22,6 +22,28 @@ function submit(fn) {
form.trigger('submit'); form.trigger('submit');
} }


asyncTest('modifying form fields with "ajax:before" sends modified data in request', 4, function(){
$('form[data-remote]')
.append($('<input type="text" name="user_name" value="john">'))
.append($('<input type="text" name="removed_user_name" value="john">'))
.live('ajax:before', function() {
var form = $(this);
form
.append($('<input />',{name: 'other_user_name',value: 'jonathan'}))
.find('input[name="removed_user_name"]').remove();
form
.find('input[name="user_name"]').val('steve');
});

submit(function(form) {
form.bind('ajax:success', function(e, data, status, xhr) {
equal(data.params.user_name, 'steve', 'modified field value should have been submitted');
equal(data.params.other_user_name, 'jonathan', 'added field value should have been submitted');
equal(data.params.removed_user_name, undefined, 'removed field value should be undefined');
});
});
});

asyncTest('stopping the "ajax:beforeSend" event aborts the request', 1, function() { asyncTest('stopping the "ajax:beforeSend" event aborts the request', 1, function() {
submit(function(form) { submit(function(form) {
form.bind('ajax:beforeSend', function() { form.bind('ajax:beforeSend', function() {
Expand Down

0 comments on commit 5b23c3d

Please sign in to comment.