Module: JQueryOnRails::Helpers::JQueryHelper

Defined in:
lib/jquery_on_rails/helpers/jquery_helper.rb

Defined Under Namespace

Classes: JavaScriptGenerator

Constant Summary collapse

CALLBACKS =
{ :beforeSend=>'request', :complete=>'request,status',
:error=>'request,status,exception', :success=>'data,status,request' }
INSERT_POSITIONS =
{ :before=>'before', :after=>'after', :top=>'prepend', :bottom=>'append' }
AJAX_OPTIONS =
Set.new([:cache, :contentType, :data, :dataType, :dataFilter, :global, :ifModified,
:jsonp, :jsonpCallback, :password, :processData, :scriptCharset,
:timeout, :traditional, :type, :username, :xhr])
CONFIRM_FUNCTION =
'confirm'.freeze
TOGGLE_EFFECTS =
Set.new([:toggle_appear, :toggle_slide, :toggle_blind])
RENAME_EFFECTS =
{ :appear=>'fadeIn', :fade=>'fadeOut' }

Instance Method Summary collapse

Instance Method Details

#remote_function(options) ⇒ Object

Returns the JavaScript needed for a remote function. Takes the same arguments as link_to_remote.

Example:

# Generates: <select id="options" onchange="jQuery.ajax({method : 'GET',
# url : '/testing/update_options', processData:false, async:true, evalScripts:true,
# complete : function(data,status,request){$('#options').html(request.responseText)}})">
<select id="options" onchange="<%= remote_function(:update => "options",
    :url => { :action => :update_options }) %>">
  <option value="0">Hello</option>
  <option value="1">World</option>
</select>


51
52
53
54
55
56
57
58
# File 'lib/jquery_on_rails/helpers/jquery_helper.rb', line 51

def remote_function(options)
  function = "jQuery.ajax(#{options_for_ajax(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_for_javascript(options[:confirm])}) { #{function}; }" if options[:confirm]
  function
end

#update_page(&block) ⇒ Object



251
252
253
# File 'lib/jquery_on_rails/helpers/jquery_helper.rb', line 251

def update_page(&block)
  JavaScriptGenerator.new(view_context, &block).to_s.html_safe
end

#update_page_tag(html_options = {}, &block) ⇒ Object



255
256
257
# File 'lib/jquery_on_rails/helpers/jquery_helper.rb', line 255

def update_page_tag(html_options = {}, &block)
  javascript_tag update_page(&block), html_options
end

#visual_effect(name, element_id = false, js_options = {}) ⇒ Object

Basic effects, limited to those supported by core jQuery 1.4 Additional effects are supported by jQuery UI.



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/jquery_on_rails/helpers/jquery_helper.rb', line 62

def visual_effect(name, element_id = false, js_options = {})
  element = element_id ? ActiveSupport::JSON.encode("##{element_id}") : "element"
  element = "jQuery(#{element})"
  js_options = (options_for_javascript js_options unless js_options.empty?)
  case name = name.to_sym
  when :toggle_slide
    "#{element}.slideToggle(#{js_options});"
  when :toggle_appear
    "(function(state){ return (function() { state=!state; return #{element}['fade'+(state?'In':'Out')](#{js_options}); })(); })(#{element}.css('visiblity')!='hidden');"
  else
    "#{element}.#{RENAME_EFFECTS[name] || name.to_s.camelize(false)}(#{js_options});"
  end
end