Permalink
Browse files

Added back 'ajax:before' event, so that form fields can easily be mod…

…ified, added, and removed before ajax call.

Signed-off-by: Neeraj Singh <neeraj@railsdog.com>
  • Loading branch information...
JangoSteve authored and Neeraj Singh committed Feb 2, 2011
1 parent a284dd7 commit 5b23c3d49273ac682adde8427eb4e4dff1d4d6aa
Showing with 42 additions and 19 deletions.
  1. +20 −19 src/rails.js
  2. +22 −0 test/public/test/call-remote-callbacks.js
View
@@ -26,6 +26,7 @@
var method, url, data,
dataType = element.attr('data-type') || ($.ajaxSettings && $.ajaxSettings.dataType);
+ if (fire(element, 'ajax:before')) {
if (element.is('form')) {
method = element.attr('method');
url = element.attr('action');
@@ -41,26 +42,26 @@
url = element.attr('href');
data = null;
}
-
- $.ajax({
- url: url, type: method || 'GET', data: data, dataType: dataType,
- // stopping the "ajax:beforeSend" event will cancel the ajax request
- beforeSend: function(xhr, settings) {
- if (settings.dataType === undefined) {
- xhr.setRequestHeader('accept', '*/*;q=0.5, ' + settings.accepts.script);
+ $.ajax({
+ url: url, type: method || 'GET', data: data, dataType: dataType,
+ // stopping the "ajax:beforeSend" event will cancel the ajax request
+ beforeSend: function(xhr, settings) {
+ if (settings.dataType === undefined) {
+ 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:
@@ -22,6 +22,28 @@ function submit(fn) {
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() {
submit(function(form) {
form.bind('ajax:beforeSend', function() {

0 comments on commit 5b23c3d

Please sign in to comment.