Module: Mack::ViewHelpers::HtmlHelpers

Defined in:
lib/mack-javascript/view_helpers/html_helpers.rb

Instance Method Summary collapse

Instance Method Details

#button_to_remote(value = "Submit", options = {}, *original_args) ⇒ Object

Returns a button that links to a remote function.

Example:

button_to_remote('Create', :url => '/foo') # => <button onclick="new Ajax.Request('/foo', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this.form)}); return false" type="submit">Create</button>


48
49
50
51
52
53
# File 'lib/mack-javascript/view_helpers/html_helpers.rb', line 48

def button_to_remote(value = "Submit", options = {}, *original_args)
  with = options.delete(:with) || 'Form.serialize(this.form)'
  url = options.delete(:url) || '#'
  options[:onclick] = remote_function(:with => with, :url => url) + '; return false'
  submit_button(value, options, *original_args)
end

#jsObject



40
41
42
# File 'lib/mack-javascript/view_helpers/html_helpers.rb', line 40

def js
  Mack::JavaScript::ScriptGenerator.new
end


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] || ''
  options = args[1] || {}
  function = yield(js).chop if block_given?
  options[:onclick] = (options[:onclick] ? "#{options[:onclick]}; " : "") + "#{function}; return false;" 
  link_to(link_text, '#', options)
end


23
24
25
# File 'lib/mack-javascript/view_helpers/html_helpers.rb', line 23

def link_to_remote(link_text, ajax_options, options = {})
  link_to_function(link_text, remote_function(ajax_options), options)
end

#remote_function(options) ⇒ Object

Returns javascript that does an Ajax call



36
37
38
# File 'lib/mack-javascript/view_helpers/html_helpers.rb', line 36

def remote_function(options)
  Mack::JavaScript::ScriptGenerator.framework.remote_function(options)
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>

Yields:

  • (gs)


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