Module: Mack::ViewHelpers::HtmlHelpers
- Defined in:
- lib/mack-javascript/view_helpers/html_helpers.rb
Instance Method Summary collapse
-
#ajax(options) ⇒ Object
Returns javascript that does an Ajax call.
-
#button_to_remote(value = "Submit", options = {}, *original_args) ⇒ Object
Returns a button that links to a remote function.
- #form_remote(action, options = {}, &block) ⇒ Object
- #js ⇒ Object
- #link_to_function(link_text, *args, &block) ⇒ Object
- #link_to_remote(link_text, ajax_options, options = {}) ⇒ Object
-
#update_page {|gs| ... } ⇒ Object
Renders javascript in an html file.
Instance Method Details
#ajax(options) ⇒ Object
Returns javascript that does an Ajax call
36 37 38 |
# File 'lib/mack-javascript/view_helpers/html_helpers.rb', line 36 def ajax() Mack::JavaScript::ScriptGenerator.new(session.id).ajax().to_s end |
#button_to_remote(value = "Submit", options = {}, *original_args) ⇒ Object
Returns a button that links to a remote function.
Example:
('Create', :url => '/foo') # => <button onclick="new Ajax.Request('/foo', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this.form)}); return false" type="submit">Create</button>
50 51 52 53 54 55 |
# File 'lib/mack-javascript/view_helpers/html_helpers.rb', line 50 def (value = "Submit", = {}, *original_args) with = .delete(:with) || 'Form.serialize(this.form)' url = .delete(:url) || '#' [:onclick] = ajax(:with => with, :url => url) + '; return false' (value, , *original_args) end |
#form_remote(action, options = {}, &block) ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/mack-javascript/view_helpers/html_helpers.rb', line 57 def form_remote(action, = {}, &block) [:form] = true [:url] = action [:html] ||= {} [:html][:onsubmit] = ([:html][:onsubmit] ? [:html][:onsubmit] + "; " : "") + "#{ajax()}; return false;" form(action, [:html], &block) end |
#js ⇒ Object
42 43 44 |
# File 'lib/mack-javascript/view_helpers/html_helpers.rb', line 42 def js Mack::JavaScript::ScriptGenerator.new(session.id) end |
#link_to_function(link_text, *args, &block) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/mack-javascript/view_helpers/html_helpers.rb', line 27 def link_to_function(link_text, *args, &block) function = args[0] || '' = args[1] || {} function = yield(js).chop if block_given? [:onclick] = ([:onclick] ? "#{[:onclick]}; " : "") + "#{function}; return false;" link_to(link_text, '#', ) end |
#link_to_remote(link_text, ajax_options, options = {}) ⇒ Object
23 24 25 |
# File 'lib/mack-javascript/view_helpers/html_helpers.rb', line 23 def link_to_remote(link_text, , = {}) link_to_function(link_text, ajax(), ) end |
#update_page {|gs| ... } ⇒ Object
Renders javascript in an html file
Example:
<%= update_page do |page|
page.alert('this gets translated to js!')
page.show('my_hidden_div')
end
%>
This returns (assuming Prototype is your Js Framework):
<script type='text/javascript'>
alert('this gets translated to js!');
$('my_hidden_div').show();
</script>
17 18 19 20 21 |
# File 'lib/mack-javascript/view_helpers/html_helpers.rb', line 17 def update_page(&block) gs = js yield(gs) "<script type='#{Mack::Utils::MimeTypes[:js]}'>\n" + gs.to_s.gsub(';', ";\n") + "</script>" end |