Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 4235

Show
Ignore:
Timestamp:
04/19/06 18:08:15 (2 years ago)
Author:
sam
Message:

Change link_to_function and button_to_function to (optionally) take an update_page block instead of a JavaScript string. Closes #4804.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r4216 r4235  
    11*SVN* 
     2 
     3* 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] 
    24 
    35* 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] 
  • trunk/actionpack/lib/action_view/helpers/javascript_helper.rb

    r3425 r4235  
    11require File.dirname(__FILE__) + '/tag_helper' 
     2require File.dirname(__FILE__) + '/prototype_helper' 
    23 
    34module ActionView 
     
    4142        JAVASCRIPT_PATH = File.join(File.dirname(__FILE__), 'javascripts') 
    4243      end 
     44 
     45      include PrototypeHelper 
    4346       
    4447      # Returns a link that'll trigger a JavaScript +function+ using the  
    4548      # onclick handler and return false after the fact. 
    4649      # 
     50      # The +function+ argument can be omitted in favor of an +update_page+ 
     51      # block, which evaluates to a string when the template is rendered 
     52      # (instead of making an Ajax request first).       
     53      # 
    4754      # Examples: 
    4855      #   link_to_function "Greeting", "alert('Hello world!')" 
    49       #   link_to_function(image_tag("delete"), "if confirm('Really?'){ do_delete(); }") 
    50       def link_to_function(name, function, html_options = {}) 
     56      #   link_to_function(image_tag("delete"), "if (confirm('Really?')) do_delete()") 
     57      #   link_to_function("Show me more", nil, :id => "more_link") do |page| 
     58      #     page[:details].visual_effect  :toggle_blind 
     59      #     page[:more_link].replace_html "Show me less" 
     60      #   end 
     61      def link_to_function(name, function = '', html_options = {}, &block) 
    5162        html_options.symbolize_keys! 
     63        function = update_page(&block) if block_given? 
    5264        content_tag( 
    5365          "a", name,  
     
    5971      end 
    6072       
    61       # Returns a link that'll trigger a JavaScript +function+ using the  
     73      # Returns a button that'll trigger a JavaScript +function+ using the  
    6274      # onclick handler. 
     75      # 
     76      # The +function+ argument can be omitted in favor of an +update_page+ 
     77      # block, which evaluates to a string when the template is rendered 
     78      # (instead of making an Ajax request first).       
    6379      # 
    6480      # Examples: 
    6581      #   button_to_function "Greeting", "alert('Hello world!')" 
    66       #   button_to_function "Delete", "if confirm('Really?'){ do_delete(); }") 
    67       def button_to_function(name, function, html_options = {}) 
     82      #   button_to_function "Delete", "if (confirm('Really?')) do_delete()" 
     83      #   button_to_function "Details" do |page| 
     84      #     page[:details].visual_effect :toggle_slide 
     85      #   end 
     86      def button_to_function(name, function = '', html_options = {}, &block) 
    6887        html_options.symbolize_keys! 
     88        function = update_page(&block) if block_given? 
    6989        tag(:input, html_options.merge({  
    7090          :type => "button", :value => name,  
  • trunk/actionpack/test/template/javascript_helper_test.rb

    r3367 r4235  
    3232  end 
    3333 
     34  def test_link_to_function_with_rjs_block 
     35    html = link_to_function( "Greet me!" ) do |page| 
     36      page.replace_html 'header', "<h1>Greetings</h1>" 
     37    end 
     38    assert_dom_equal %(<a href="#" onclick="Element.update(&quot;header&quot;, &quot;&lt;h1&gt;Greetings&lt;/h1&gt;&quot;);; return false;">Greet me!</a>), html 
     39  end 
     40 
     41 
    3442  def test_button_to_function 
    3543    assert_dom_equal %(<input type="button" onclick="alert('Hello world!');" value="Greeting" />),  
    3644      button_to_function("Greeting", "alert('Hello world!')") 
    3745  end 
     46 
     47  def test_button_to_function_with_rjs_block 
     48    html = button_to_function( "Greet me!" ) do |page| 
     49      page.replace_html 'header', "<h1>Greetings</h1>" 
     50    end 
     51    assert_dom_equal %(<input type="button" onclick="Element.update(&quot;header&quot;, &quot;&lt;h1&gt;Greetings&lt;/h1&gt;&quot;);;" value="Greet me!" />), html 
     52  end 
    3853end