From de7011fef55911cb157624996430848fff02a5d4 Mon Sep 17 00:00:00 2001 From: Quin Hoxie Date: Tue, 9 Feb 2010 23:31:38 -0800 Subject: [PATCH 01/12] Throw exception if no url is provided in the remote call --- src/rails.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/rails.js b/src/rails.js index d9285c93..e122e6dd 100644 --- a/src/rails.js +++ b/src/rails.js @@ -27,8 +27,9 @@ jQuery(function ($) { method = el.attr('method') || el.attr('data-method') || 'GET', url = el.attr('action') || el.attr('href'); - // TODO: should let the developer know no url was found - if (url !== undefined) { + if (url === undefined) { + throw "No URL specified for remote call (action or href must be present)."; + } else { if (el.triggerAndReturn('ajax:before')) { $.ajax({ url: url, From 2e8f578825fe388f357722241ba65519f1dc44a9 Mon Sep 17 00:00:00 2001 From: Quin Hoxie Date: Tue, 9 Feb 2010 23:32:16 -0800 Subject: [PATCH 02/12] Utilize dataType option to manage Accept header --- src/rails.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rails.js b/src/rails.js index e122e6dd..204508e6 100644 --- a/src/rails.js +++ b/src/rails.js @@ -34,9 +34,9 @@ jQuery(function ($) { $.ajax({ url: url, data: data, + dataType: 'script', type: method.toUpperCase(), beforeSend: function (xhr) { - xhr.setRequestHeader("Accept", "text/javascript"); el.trigger('ajax:loading', xhr); }, success: function (data, status, xhr) { From 5ae3f93d66bde731876840b68481525719ec3625 Mon Sep 17 00:00:00 2001 From: Quin Hoxie Date: Wed, 10 Feb 2010 00:04:05 -0800 Subject: [PATCH 03/12] Better symmetry in negation of data-remote selector --- src/rails.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rails.js b/src/rails.js index 204508e6..edc3659d 100644 --- a/src/rails.js +++ b/src/rails.js @@ -82,7 +82,7 @@ jQuery(function ($) { e.preventDefault(); }); - $('a[data-method][data-remote!=true]').live('click',function(e){ + $('a[data-method]:not([data-remote="true"])').live('click',function(e){ var link = $(this), href = link.attr('href'), method = link.attr('data-method'), From 0999420167a48769383f2025f68b528fa2eae1ee Mon Sep 17 00:00:00 2001 From: Quin Hoxie Date: Wed, 10 Feb 2010 00:06:14 -0800 Subject: [PATCH 04/12] Treat data-remote as a boolean attribute in selectors --- src/rails.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/rails.js b/src/rails.js index edc3659d..d4cd8116 100644 --- a/src/rails.js +++ b/src/rails.js @@ -72,17 +72,17 @@ jQuery(function ($) { /** * remote handlers */ - $('form[data-remote="true"]').live('submit', function (e) { + $('form[data-remote]').live('submit', function (e) { $(this).callRemote(); e.preventDefault(); }); - $('a[data-remote="true"],input[data-remote="true"]').live('click', function (e) { + $('a[data-remote],input[data-remote]').live('click', function (e) { $(this).callRemote(); e.preventDefault(); }); - $('a[data-method]:not([data-remote="true"])').live('click',function(e){ + $('a[data-method]:not([data-remote])').live('click',function(e){ var link = $(this), href = link.attr('href'), method = link.attr('data-method'), @@ -102,7 +102,7 @@ jQuery(function ($) { /** * disable_with handlers */ - $('form[data-remote="true"]').live('ajax:before', function () { + $('form[data-remote]').live('ajax:before', function () { $(this).children('input[data-disable-with]').each(function () { var input = $(this); input.data('enable_with', input.val()) @@ -111,7 +111,7 @@ jQuery(function ($) { }); }); - $('form[data-remote="true"]').live('ajax:after', function () { + $('form[data-remote]').live('ajax:after', function () { $(this).children('input[data-disable-with]').each(function () { var input = $(this); input.removeAttr('disabled') From 16363f7cf44ce9aaca138ee1d07d5443f3439342 Mon Sep 17 00:00:00 2001 From: Quin Hoxie Date: Wed, 10 Feb 2010 00:20:37 -0800 Subject: [PATCH 05/12] Simplify metadata inputs to strings and a single call to append() --- src/rails.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/rails.js b/src/rails.js index d4cd8116..f09c2c94 100644 --- a/src/rails.js +++ b/src/rails.js @@ -87,13 +87,12 @@ jQuery(function ($) { href = link.attr('href'), method = link.attr('data-method'), form = $('
'), - input = $(''), - csrf_input = $(''); + input = '', + csrf_input = ''; form.hide() - .append(input) - .append(csrf_input) - .appendTo('body'); // redundant? + .append(input + csrf_input) + .appendTo('body'); e.preventDefault(); form.submit(); From b17c7bb3a1a1a73c8eb2e0a7f0804ffb23190285 Mon Sep 17 00:00:00 2001 From: Quin Hoxie Date: Wed, 10 Feb 2010 00:22:20 -0800 Subject: [PATCH 06/12] Only create csrf input if the data is provided --- src/rails.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/rails.js b/src/rails.js index f09c2c94..6b09b8cc 100644 --- a/src/rails.js +++ b/src/rails.js @@ -87,11 +87,14 @@ jQuery(function ($) { href = link.attr('href'), method = link.attr('data-method'), form = $(''), - input = '', - csrf_input = ''; + metadata_input = ''; + + if (csrf_param != null && csrf_token != null) { + metadata_input += ''; + } form.hide() - .append(input + csrf_input) + .append(metadata_input) .appendTo('body'); e.preventDefault(); From be329c5d37dfb5acafe994fa52ed39bb87317c7c Mon Sep 17 00:00:00 2001 From: Quin Hoxie Date: Wed, 10 Feb 2010 00:25:43 -0800 Subject: [PATCH 07/12] Whitespace consistency --- src/rails.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rails.js b/src/rails.js index 6b09b8cc..d03c462b 100644 --- a/src/rails.js +++ b/src/rails.js @@ -82,7 +82,7 @@ jQuery(function ($) { e.preventDefault(); }); - $('a[data-method]:not([data-remote])').live('click',function(e){ + $('a[data-method]:not([data-remote])').live('click', function (e){ var link = $(this), href = link.attr('href'), method = link.attr('data-method'), From bfaa4c4e5bd4dc196d09405d419e26d077288120 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Wed, 10 Feb 2010 11:46:22 +0000 Subject: [PATCH 08/12] replace .children() with .form() to fully search form for inputs. --- src/rails.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/rails.js b/src/rails.js index d9285c93..f74beb72 100644 --- a/src/rails.js +++ b/src/rails.js @@ -102,7 +102,7 @@ jQuery(function ($) { * disable_with handlers */ $('form[data-remote="true"]').live('ajax:before', function () { - $(this).children('input[data-disable-with]').each(function () { + $(this).find('input[data-disable-with]').each(function () { var input = $(this); input.data('enable_with', input.val()) .attr('value', input.attr('data-disable-with')) @@ -111,7 +111,7 @@ jQuery(function ($) { }); $('form[data-remote="true"]').live('ajax:after', function () { - $(this).children('input[data-disable-with]').each(function () { + $(this).find('input[data-disable-with]').each(function () { var input = $(this); input.removeAttr('disabled') .val(input.data('enable_with')); From 84d8dfa35f8e30cec29126eada0ddad0280d99b0 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Wed, 10 Feb 2010 12:20:05 +0000 Subject: [PATCH 09/12] clear disabled on ajax:complete (rails/prototype compatible) --- src/rails.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rails.js b/src/rails.js index f74beb72..1f3449e2 100644 --- a/src/rails.js +++ b/src/rails.js @@ -110,7 +110,7 @@ jQuery(function ($) { }); }); - $('form[data-remote="true"]').live('ajax:after', function () { + $('form[data-remote="true"]').live('ajax:complete', function () { $(this).find('input[data-disable-with]').each(function () { var input = $(this); input.removeAttr('disabled') From 7a0443bd6d7efaf9c9d3db1ea0dcc42ee41673b2 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Sun, 14 Feb 2010 22:30:16 +0000 Subject: [PATCH 10/12] contentType fix for safari4.0.4/jquery1.4.1 setting contentType to "application/xml" which triggers error in rails 2.3.5 --- src/rails.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/rails.js b/src/rails.js index 93ce8074..1c07d327 100644 --- a/src/rails.js +++ b/src/rails.js @@ -36,6 +36,7 @@ jQuery(function ($) { data: data, dataType: 'script', type: method.toUpperCase(), + contentType: "application/x-www-form-urlencoded", beforeSend: function (xhr) { el.trigger('ajax:loading', xhr); }, From d39afa4bce053a3ba3d03f2593de454fbe1756f6 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Tue, 16 Feb 2010 11:06:17 +0000 Subject: [PATCH 11/12] clear disabled on ajax:complete --- src/rails.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rails.js b/src/rails.js index 1c07d327..c3d4c09c 100644 --- a/src/rails.js +++ b/src/rails.js @@ -114,7 +114,7 @@ jQuery(function ($) { }); }); - $('form[data-remote]').live('ajax:after', function () { + $('form[data-remote]').live('ajax:complete', function () { $(this).find('input[data-disable-with]').each(function () { var input = $(this); input.removeAttr('disabled') From fac2bd0027beca38bc2eae8dce945c96c18c91f1 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Tue, 16 Feb 2010 22:25:10 +0000 Subject: [PATCH 12/12] jquery .live() simple selector --- src/rails.js | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/rails.js b/src/rails.js index 62b55c11..cb3b5d07 100644 --- a/src/rails.js +++ b/src/rails.js @@ -105,11 +105,8 @@ jQuery(function ($) { /** * disable-with handlers */ - var disable_with_input_selector = 'input[data-disable-with]'; - var disable_with_form_selector = 'form[data-remote]:has(' + disable_with_input_selector + ')'; - - $(disable_with_form_selector).live('ajax:before', function () { - $(this).find(disable_with_input_selector).each(function () { + $('form[data-remote]').live('ajax:before', function () { + $(this).find('input[data-disable-with]').each(function () { var input = $(this); input.data('enable-with', input.val()) .attr('value', input.attr('data-disable-with')) @@ -117,8 +114,8 @@ jQuery(function ($) { }); }); - $(disable_with_form_selector).live('ajax:after', function () { - $(this).find(disable_with_input_selector).each(function () { + $('form[data-remote]').live('ajax:complete', function () { + $(this).find('input[data-disable-with]').each(function () { var input = $(this); input.removeAttr('disabled') .val(input.data('enable-with'));