Sha256: e760bb7218902eab1a2caf9872702d44d2526aac29858ecc23ece227e96d80c6

Contents?: true

Size: 1.53 KB

Versions: 8

Compression:

Stored size: 1.53 KB

Contents

module Apotomo
  module JavascriptMethods
    # Returns the escaped script.
    def escape_js(script)
      Apotomo.js_generator.escape(script)
    end
    
    # 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 <tt>Apotomo.js_framework = :jquery</tt>):
    #
    #   def hungry
    #     replace 
    #
    # will render the current state's view and wrap it like
    #
    #   "$(\"#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 
    #     #=> "$(\"#jerry h1\").replaceWith(\"<div id=\\\"mouse\\\">squeak!<\\/div>\")"
    def replace(*args)
      wrap_in_javascript_for(:replace, *args)
    end
    
    # Same as #replace except that the content is wrapped in an update statement.
    #
    # Example for +:jquery+:
    #
    #   update :view => :peek
    #   #=> "$(\"#mouse\").html(\"looking...")"
    def update(*args)
      wrap_in_javascript_for(:update, *args)
    end
    
  private
    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
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
apotomo-1.2.3 lib/apotomo/widget/javascript_methods.rb
apotomo-1.2.2 lib/apotomo/widget/javascript_methods.rb
apotomo-1.2.1 lib/apotomo/widget/javascript_methods.rb
apotomo-1.2.0 lib/apotomo/widget/javascript_methods.rb
apotomo-1.1.4 lib/apotomo/widget/javascript_methods.rb
apotomo-1.1.3 lib/apotomo/widget/javascript_methods.rb
apotomo-1.1.2 lib/apotomo/widget/javascript_methods.rb
apotomo-1.1.1 lib/apotomo/widget/javascript_methods.rb