Class: Mack::JavaScript::Framework::PrototypeAjax

Inherits:
Object
  • Object
show all
Defined in:
lib/mack-javascript/helpers/prototype_helper.rb

Constant Summary collapse

@@callbacks =
[:uninitialized, :loading, :loaded, :interactive, :complete, :failure, :success ] + 
[100,101] + (200..206).to_a + (300..307).to_a + (400..417).to_a + (500..505).to_a

Class Method Summary collapse

Class Method Details

.remote_function(options) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mack-javascript/helpers/prototype_helper.rb', line 12

def remote_function(options)
  javascript_options = options_for_ajax(options)
  update = ''
  if options[:update] && options[:update].is_a?(Hash)
    update  = []
    update << "success:'#{options[:update][:success]}'" if options[:update][:success]
    update << "failure:'#{options[:update][:failure]}'" if options[:update][:failure]
    update  = '{' + update.join(',') + '}'
  elsif options[:update]
    update << "'#{options[:update]}'"
  end

  function = update.empty? ? 
  "new Ajax.Request(" :
  "new Ajax.Updater(#{update}, "

  url_options = options[:url]
  url_options = url_options.merge(:escape => false) if url_options.is_a?(Hash)
  function << "'#{url_options}'"
  function << ", #{javascript_options})"

  function = "#{options[:before]}; #{function}" if options[:before]
  function = "#{function}; #{options[:after]}"  if options[:after]
  function = "if (#{options[:condition]}) { #{function}; }" if options[:condition]
  function = "if (confirm('#{escape_javascript(options[:confirm])}')) { #{function}; }" if options[:confirm]

  return function
end