提交 987d9dbf 编写于 作者: D David Heinemeier Hansson

Added JavaScriptHelper#button_to_function that works just like...

Added JavaScriptHelper#button_to_function that works just like JavaScriptHelper#link_to_function but uses a button instead of a href [DHH]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3367 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 d7487fff
*SVN*
* Added JavaScriptHelper#button_to_function that works just like JavaScriptHelper#link_to_function but uses a button instead of a href [DHH]
* 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]
......
......@@ -58,6 +58,20 @@ def link_to_function(name, function, html_options = {})
)
end
# Returns a link that'll trigger a JavaScript +function+ using the
# onclick handler.
#
# Examples:
# button_to_function "Greeting", "alert('Hello world!')"
# button_to_function "Delete", "if confirm('Really?'){ do_delete(); }")
def button_to_function(name, function, html_options = {})
html_options.symbolize_keys!
tag(:input, html_options.merge({
:type => "button", :value => name,
:onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + "#{function};"
}))
end
# Includes the Action Pack JavaScript libraries inside a single <script>
# tag. The function first includes prototype.js and then its core extensions,
# (determined by filenames starting with "prototype").
......
......@@ -30,4 +30,9 @@ def test_link_to_function_with_existing_onclick
assert_dom_equal %(<a href="#" onclick="confirm('Sanity!'); alert('Hello world!'); return false;">Greeting</a>),
link_to_function("Greeting", "alert('Hello world!')", :onclick => "confirm('Sanity!')")
end
def test_button_to_function
assert_dom_equal %(<input type="button" onclick="alert('Hello world!');" value="Greeting" />),
button_to_function("Greeting", "alert('Hello world!')")
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册