@@ -67,6 +67,46 @@ def javascript_tag(content_or_options_with_block = nil, html_options = {}, &bloc
67
67
def javascript_cdata_section ( content ) #:nodoc:
68
68
"\n //#{ cdata_section ( "\n #{ content } \n //" ) } \n " . html_safe
69
69
end
70
+
71
+ # Returns a button whose +onclick+ handler triggers the passed JavaScript.
72
+ #
73
+ # The helper receives a name, JavaScript code, and an optional hash of HTML options. The
74
+ # name is used as button label and the JavaScript code goes into its +onclick+ attribute.
75
+ # If +html_options+ has an <tt>:onclick</tt>, that one is put before +function+.
76
+ #
77
+ # button_to_function "Greeting", "alert('Hello world!')", :class => "ok"
78
+ # # => <input class="ok" onclick="alert('Hello world!');" type="button" value="Greeting" />
79
+ #
80
+ def button_to_function ( name , function = nil , html_options = { } )
81
+ message = "button_to_function is deprecated and will be removed from Rails 4.1. Use Unobtrusive JavaScript instead."
82
+ ActiveSupport ::Deprecation . warn message
83
+
84
+ onclick = "#{ "#{ html_options [ :onclick ] } ; " if html_options [ :onclick ] } #{ function } ;"
85
+
86
+ tag ( :input , html_options . merge ( :type => 'button' , :value => name , :onclick => onclick ) )
87
+ end
88
+
89
+ # Returns a link whose +onclick+ handler triggers the passed JavaScript.
90
+ #
91
+ # The helper receives a name, JavaScript code, and an optional hash of HTML options. The
92
+ # name is used as the link text and the JavaScript code goes into the +onclick+ attribute.
93
+ # If +html_options+ has an <tt>:onclick</tt>, that one is put before +function+. Once all
94
+ # the JavaScript is set, the helper appends "; return false;".
95
+ #
96
+ # The +href+ attribute of the tag is set to "#" unless +html_options+ has one.
97
+ #
98
+ # link_to_function "Greeting", "alert('Hello world!')", :class => "nav_link"
99
+ # # => <a class="nav_link" href="#" onclick="alert('Hello world!'); return false;">Greeting</a>
100
+ #
101
+ def link_to_function ( name , function , html_options = { } )
102
+ message = "link_to_function is deprecated and will be removed from Rails 4.1. Use Unobtrusive JavaScript instead."
103
+ ActiveSupport ::Deprecation . warn message
104
+
105
+ onclick = "#{ "#{ html_options [ :onclick ] } ; " if html_options [ :onclick ] } #{ function } ; return false;"
106
+ href = html_options [ :href ] || '#'
107
+
108
+ content_tag ( :a , name , html_options . merge ( :href => href , :onclick => onclick ) )
109
+ end
70
110
end
71
111
end
72
112
end
0 commit comments