Module: Mack::ViewHelpers::HtmlHelpers

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

Instance Method Summary collapse

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(options)
  Mack::JavaScript::ScriptGenerator.new(session.id).ajax(options).to_s
end

#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>


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

def button_to_remote(value = "Submit", options = {}, *original_args)
  with = options.delete(:with) || 'Form.serialize(this.form)'
  url = options.delete(:url) || '#'
  options[:onclick] = ajax(:with => with, :url => url) + '; return false'
  submit_button(value, options, *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, options = {}, &block)
  options[:form] = true
  options[:url] = action
  options[:html] ||= {}
  options[:html][:onsubmit] = 
    (options[:html][:onsubmit] ? options[:html][:onsubmit] + "; " : "") + 
    "#{ajax(options)}; return false;"

  form(action, options[:html], &block)
end

#jsObject



42
43
44
# File 'lib/mack-javascript/view_helpers/html_helpers.rb', line 42

def js
  Mack::JavaScript::ScriptGenerator.new(session.id)
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, ajax(ajax_options), 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