From 06d80d0e574f50d02e966a39ac45a488a3952f7c Mon Sep 17 00:00:00 2001 From: "Thomas R. Koll" Date: Tue, 28 Dec 2010 23:46:34 +0100 Subject: [PATCH 1/2] Issue #729: Failed posts and comments don't retain text The forms were cleared on a submit event instead of a ajax:success --- public/javascripts/rails.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/public/javascripts/rails.js b/public/javascripts/rails.js index 99e20ea8e..da50746dd 100644 --- a/public/javascripts/rails.js +++ b/public/javascripts/rails.js @@ -97,10 +97,14 @@ jQuery(function ($) { $('form[data-remote]').live('submit', function (e) { $(this).callRemote(); e.preventDefault(); - $(this).clearForm(); - $(this).focusout(); }); + $('form[data-remote]').live('ajax:success', function (e) { + $(this).clearForm(); + $(this).focusout(); + }); + + $('a[data-remote],input[data-remote]').live('click', function (e) { $(this).callRemote(); e.preventDefault(); From 5e1f5ff5b1ef1031cd79d367588df25bb82bdca0 Mon Sep 17 00:00:00 2001 From: "Thomas R. Koll" Date: Wed, 29 Dec 2010 17:43:50 +0100 Subject: [PATCH 2/2] Starting spec for rails.js --- spec/javascripts/helpers/SpecHelper.js | 5 +++-- spec/javascripts/rails-spec.js | 22 ++++++++++++++++++++++ spec/javascripts/support/jasmine.yml | 1 + 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 spec/javascripts/rails-spec.js diff --git a/spec/javascripts/helpers/SpecHelper.js b/spec/javascripts/helpers/SpecHelper.js index f7aa90333..2426c0a47 100644 --- a/spec/javascripts/helpers/SpecHelper.js +++ b/spec/javascripts/helpers/SpecHelper.js @@ -11,12 +11,13 @@ beforeEach(function() { $('#jasmine_content').empty(); - spec.clearLiveEventBindings(); + // NOTE Commented (as well as in afterEach) to keep the listeners from rails.js alive. + //spec.clearLiveEventBindings(); jasmine.Clock.useMock(); }); afterEach(function() { - spec.clearLiveEventBindings(); + //spec.clearLiveEventBindings(); expect(spec.loadFixtureCount).toBeLessThan(2); spec.loadFixtureCount = 0; }); diff --git a/spec/javascripts/rails-spec.js b/spec/javascripts/rails-spec.js new file mode 100644 index 000000000..c79917319 --- /dev/null +++ b/spec/javascripts/rails-spec.js @@ -0,0 +1,22 @@ +describe("rails", function() { + describe("remote forms", function() { + beforeEach(function() { + $("#jasmine_content").html( + '
' + + '' + + '' + + '
' + ); + }); + it("should retain form values if ajax fails", function() { + $('#form').trigger('ajax:failure'); + expect($('#status_message_message').val()).not.toEqual(""); + }); + it("should clear form on ajax:success", function() { + $('#form').trigger('ajax:success'); + + expect($('#status_message_message').val()).toEqual(""); + + }); + }); +}); \ No newline at end of file diff --git a/spec/javascripts/support/jasmine.yml b/spec/javascripts/support/jasmine.yml index f023e116b..75a4b0d42 100644 --- a/spec/javascripts/support/jasmine.yml +++ b/spec/javascripts/support/jasmine.yml @@ -25,6 +25,7 @@ src_files: - public/javascripts/view.js - public/javascripts/stream.js - public/javascripts/validation.js + - public/javascripts/rails.js # stylesheets # # Return an array of stylesheet filepaths relative to src_dir to include before jasmine specs.