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
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/mjs/helper.rb', line 19 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.
54 55 56 57 58 59 60 |
# File 'lib/mjs/helper.rb', line 54 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
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/mjs/helper.rb', line 32 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 |
# File 'lib/mjs/helper.rb', line 9 def remote_function(opts) build_href(opts) if opts[:submit] "$.ajax(%s);" % (opts) else "$.getScript('#{opts[:href]}')" end end |