Module: Mjs::Helper
- Defined in:
- lib/mjs/helper.rb
Constant Summary collapse
- JS_ESCAPE_MAP =
derived from actionpack
{ '\\' => '\\\\', '</' => '<\/', "\r\n" => '\n', "\n" => '\n', "\r" => '\n', '"' => '\\"', "'" => "\\'" }
Instance Method Summary collapse
-
#button_to(name, url = '', opts = {}) ⇒ Object
experimental: not tested yet.
-
#escape_javascript(javascript) ⇒ Object
Escape carrier returns and single and double quotes for JavaScript segments.
-
#link_to(name, url = '', opts = {}) ⇒ Object
override! :link_to # for Ajax.
- #page ⇒ Object
- #remote_function(opts) ⇒ Object
Instance Method Details
#button_to(name, url = '', opts = {}) ⇒ Object
experimental: not tested yet
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/mjs/helper.rb', line 22 def (name, url='', opts={}) ajax = remote_function(opts) opts[:type] = 'button' opts[:value] = name opts[:remote] ||= true if opts[:submit] if opts.delete(:remote) ajax = remote_function(opts) opts[:onclick] = "#{opts.delete(:onclick)}; #{ajax}; return false;" end %{<input #{ opts.to_xml_attributes }>} end |
#escape_javascript(javascript) ⇒ Object
Escape carrier returns and single and double quotes for JavaScript segments.
57 58 59 60 61 62 63 |
# File 'lib/mjs/helper.rb', line 57 def escape_javascript(javascript) if javascript javascript.gsub(/(\\|<\/|\r\n|[\n\r"'])/) { JS_ESCAPE_MAP[$1] } else '' end end |
#link_to(name, url = '', opts = {}) ⇒ Object
override! :link_to # for Ajax
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/mjs/helper.rb', line 35 def link_to(name, url='', opts={}) opts[:href] ||= url opts[:remote] ||= true if opts[:submit] return super unless opts.delete(:remote) ajax = remote_function(opts) opts[:onclick] = "#{opts.delete(:onclick)}; #{ajax}; return false;" opts[:href] = '#' %{<a #{ opts.to_xml_attributes }>#{name}</a>} end |
#page ⇒ Object
5 6 7 |
# File 'lib/mjs/helper.rb', line 5 def page @page ||= Mjs::JavaScriptContext.new end |
#remote_function(opts) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/mjs/helper.rb', line 9 def remote_function(opts) build_href(opts) unless opts[:submit] opts[:url] ||= opts[:href] opts[:dataType] = "script" end function = "jQuery.ajax(%s);" % (opts) confirm = opts.delete(:confirm) function = "if (confirm('#{escape_javascript(confirm)}')) { #{function}; }" if confirm return function end |