Module: ActionView::Helpers::PrototypeHelper

Defined in:
lib/jrails.rb

Defined Under Namespace

Classes: JavaScriptGenerator

Constant Summary collapse

JQCALLBACKS =
Set.new([ :beforeSend, :complete, :error, :success ] + (100..599).to_a)
AJAX_OPTIONS =
Set.new([ :before, :after, :condition, :url,
:asynchronous, :method, :insertion, :position,
:form, :with, :update, :script ]).merge(JQCALLBACKS)

Instance Method Summary collapse

Instance Method Details

#periodically_call_remote(options = {}) ⇒ Object



11
12
13
14
15
# File 'lib/jrails.rb', line 11

def periodically_call_remote(options = {})
  frequency = options[:frequency] || 10 # every ten seconds by default
  code = "setInterval(function() {#{remote_function(options)}}, #{frequency} * 1000)"
  javascript_tag(code)
end

#remote_function(options) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/jrails.rb', line 17

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 = "$.ajax(#{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