mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Change link_to_function and button_to_function to (optionally) take an update_page block instead of a JavaScript string. Closes #4804.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4235 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
2a2afca095
commit
9d8e34808f
3 changed files with 42 additions and 5 deletions
|
@ -1,5 +1,7 @@
|
|||
*SVN*
|
||||
|
||||
* Change link_to_function and button_to_function to (optionally) take an update_page block instead of a JavaScript string. Closes #4804. [zraii@comcast.net, Sam Stephenson]
|
||||
|
||||
* Fixed that remote_form_for can leave out the object parameter and default to the instance variable of the object_name, just like form_for [DHH]
|
||||
|
||||
* Modify routing so that you can say :require => { :method => :post } for a route, and the route will never be selected unless the request method is POST. Only works for route recognition, not for route generation. [Jamis Buck]
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
require File.dirname(__FILE__) + '/tag_helper'
|
||||
require File.dirname(__FILE__) + '/prototype_helper'
|
||||
|
||||
module ActionView
|
||||
module Helpers
|
||||
|
@ -40,15 +41,26 @@ module ActionView
|
|||
unless const_defined? :JAVASCRIPT_PATH
|
||||
JAVASCRIPT_PATH = File.join(File.dirname(__FILE__), 'javascripts')
|
||||
end
|
||||
|
||||
include PrototypeHelper
|
||||
|
||||
# Returns a link that'll trigger a JavaScript +function+ using the
|
||||
# onclick handler and return false after the fact.
|
||||
#
|
||||
# The +function+ argument can be omitted in favor of an +update_page+
|
||||
# block, which evaluates to a string when the template is rendered
|
||||
# (instead of making an Ajax request first).
|
||||
#
|
||||
# Examples:
|
||||
# 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 = {})
|
||||
# link_to_function(image_tag("delete"), "if (confirm('Really?')) do_delete()")
|
||||
# link_to_function("Show me more", nil, :id => "more_link") do |page|
|
||||
# page[:details].visual_effect :toggle_blind
|
||||
# page[:more_link].replace_html "Show me less"
|
||||
# end
|
||||
def link_to_function(name, function = '', html_options = {}, &block)
|
||||
html_options.symbolize_keys!
|
||||
function = update_page(&block) if block_given?
|
||||
content_tag(
|
||||
"a", name,
|
||||
html_options.merge({
|
||||
|
@ -58,14 +70,22 @@ module ActionView
|
|||
)
|
||||
end
|
||||
|
||||
# Returns a link that'll trigger a JavaScript +function+ using the
|
||||
# Returns a button that'll trigger a JavaScript +function+ using the
|
||||
# onclick handler.
|
||||
#
|
||||
# The +function+ argument can be omitted in favor of an +update_page+
|
||||
# block, which evaluates to a string when the template is rendered
|
||||
# (instead of making an Ajax request first).
|
||||
#
|
||||
# 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 = {})
|
||||
# button_to_function "Delete", "if (confirm('Really?')) do_delete()"
|
||||
# button_to_function "Details" do |page|
|
||||
# page[:details].visual_effect :toggle_slide
|
||||
# end
|
||||
def button_to_function(name, function = '', html_options = {}, &block)
|
||||
html_options.symbolize_keys!
|
||||
function = update_page(&block) if block_given?
|
||||
tag(:input, html_options.merge({
|
||||
:type => "button", :value => name,
|
||||
:onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + "#{function};"
|
||||
|
|
|
@ -31,8 +31,23 @@ class JavaScriptHelperTest < Test::Unit::TestCase
|
|||
link_to_function("Greeting", "alert('Hello world!')", :onclick => "confirm('Sanity!')")
|
||||
end
|
||||
|
||||
def test_link_to_function_with_rjs_block
|
||||
html = link_to_function( "Greet me!" ) do |page|
|
||||
page.replace_html 'header', "<h1>Greetings</h1>"
|
||||
end
|
||||
assert_dom_equal %(<a href="#" onclick="Element.update("header", "<h1>Greetings</h1>");; return false;">Greet me!</a>), html
|
||||
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
|
||||
|
||||
def test_button_to_function_with_rjs_block
|
||||
html = button_to_function( "Greet me!" ) do |page|
|
||||
page.replace_html 'header', "<h1>Greetings</h1>"
|
||||
end
|
||||
assert_dom_equal %(<input type="button" onclick="Element.update("header", "<h1>Greetings</h1>");;" value="Greet me!" />), html
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue