module Apotomo::JavascriptMethods

Public Instance Methods

escape_js(script) click to toggle source

Returns the escaped script.

# File lib/apotomo/widget/javascript_methods.rb, line 4
def escape_js(script)
  Apotomo.js_generator.escape(script)
end
replace(*args) click to toggle source

Wraps the rendered content in a replace statement according to your Apotomo.js_framework setting. Received the same options as render plus an optional selector to change the selector.

Example (with Apotomo.js_framework = :jquery):

def hungry
  replace

will render the current state’s view and wrap it like

"jQuery(\"#mouse\").replaceWith(\"<div id=\\\"mouse\\\">hungry!<\\/div>\")"

You may pass a selector and pass options to render here, as well.

replace "#jerry h1", :view => :squeak 
#=> "jQuery(\"#jerry h1\").replaceWith(\"<div id=\\\"mouse\\\">squeak!<\\/div>\")"
# File lib/apotomo/widget/javascript_methods.rb, line 24
def replace(*args)
  wrap_in_javascript_for(:replace, *args)
end
update(*args) click to toggle source

Same as replace except that the content is wrapped in an update statement.

Example for :jquery:

update :view => :peek
#=> "jQuery(\"#mouse\").html(\"looking...")"
# File lib/apotomo/widget/javascript_methods.rb, line 34
def update(*args)
  wrap_in_javascript_for(:update, *args)
end

Private Instance Methods

wrap_in_javascript_for(mode, *args) click to toggle source
# File lib/apotomo/widget/javascript_methods.rb, line 39
def wrap_in_javascript_for(mode, *args)
  selector  = args.first.is_a?(String) ? args.shift : false
  content   = render(*args)
  
  selector ? 
    Apotomo.js_generator.send(mode, selector, content) :    # replace(:twitter)
    Apotomo.js_generator.send("#{mode}_id", name, content)  # replace_id(:twitter)
end