Module: ActionView::Helpers::PrototypeHelper

Defined in:
lib/jrails.rb

Defined Under Namespace

Classes: JavaScriptGenerator

Constant Summary collapse

USE_PROTECTION =
const_defined?(:DISABLE_JQUERY_FORGERY_PROTECTION) ? !DISABLE_JQUERY_FORGERY_PROTECTION : true
JQUERY_VAR =
'jQuery'
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



48
49
50
51
52
# File 'lib/jrails.rb', line 48

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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/jrails.rb', line 54

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 = "#{JQUERY_VAR}.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