From 341d34cd50ab1d807a9d515058d1da8fe75deb01 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sat, 31 Dec 2005 03:50:08 +0000 Subject: Added that JavaScriptHelper#link_to_function will honor existing :onclick definitions when adding the function call [DHH] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3364 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/CHANGELOG | 2 ++ actionpack/lib/action_view/helpers/javascript_helper.rb | 6 +++++- actionpack/test/template/javascript_helper_test.rb | 4 ++++ 3 files changed, 11 insertions(+), 1 deletion(-) (limited to 'actionpack') diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 1fa70c32ae..33eb676c25 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Added that JavaScriptHelper#link_to_function will honor existing :onclick definitions when adding the function call [DHH] + * Added :disable_with option to FormTagHelper#submit_tag to allow for easily disabled submit buttons with different text [DHH] * Make auto_link handle nil by returning quickly if blank? [Scott Barron] diff --git a/actionpack/lib/action_view/helpers/javascript_helper.rb b/actionpack/lib/action_view/helpers/javascript_helper.rb index 033aff5044..7dccecda13 100644 --- a/actionpack/lib/action_view/helpers/javascript_helper.rb +++ b/actionpack/lib/action_view/helpers/javascript_helper.rb @@ -48,9 +48,13 @@ module ActionView # link_to_function "Greeting", "alert('Hello world!')" # link_to_function(image_tag("delete"), "if confirm('Really?'){ do_delete(); }") def link_to_function(name, function, html_options = {}) + html_options.symbolize_keys! content_tag( "a", name, - {:href => "#", :onclick => "#{function}; return false;"}.merge(html_options.symbolize_keys) + html_options.merge({ + :href => html_options[:href] || "#", + :onclick => "#{function};#{html_options[:onclick] ? " #{html_options[:onclick]};" : ""} return false;" + }) ) end diff --git a/actionpack/test/template/javascript_helper_test.rb b/actionpack/test/template/javascript_helper_test.rb index 48d1e263c1..6a01bd14b2 100644 --- a/actionpack/test/template/javascript_helper_test.rb +++ b/actionpack/test/template/javascript_helper_test.rb @@ -26,4 +26,8 @@ class JavaScriptHelperTest < Test::Unit::TestCase link_to_function("Greeting", "alert('Hello world!')") end + def test_link_to_function_with_existing_onclick + assert_dom_equal %(Greeting), + link_to_function("Greeting", "alert('Hello world!')", :onclick => "confirm('Sanity!')") + end end -- cgit v1.2.3