Module: Helperful::JavascriptHelper

Defined in:
lib/helperful/javascript_helper.rb

Overview

Javascript Helper

Provides a set of helpers for working with JavaScript in your views.

Requires

The following requirements are mandatory for this module. Including this helper will automatically include them unless already included.

  • ActionView::Helpers::CaptureHelper

  • ActionView::Helpers::JavaScriptHelper

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



35
36
37
38
39
40
# File 'lib/helperful/javascript_helper.rb', line 35

def self.included(base)
  base.class_eval do
#        base.included_modules.include?(ActionView::Helpers::CaptureHelper) || include(ActionView::Helpers::CaptureHelper)
#        base.included_modules.include?(ActionView::Helpers::JavaScriptHelper) || include(ActionView::Helpers::JavaScriptHelper)
  end
end

Instance Method Details

#javascript_content_for(name, content_or_options_with_block = nil, html_options = {}, &block) ⇒ Object

Mixes the features of content_for and javascript_tag into a single helper.

Supports all original method options and features, including the ability to use write the content in a block and javascript tag options.

Examples

<% javascript_content_for :head do %>
  $("#id").hide();
<% end %>

The block is passed as it is to javascript_tag, then the result is stored as content_for :head. Now you can call yield and output your javascript content.

<%= yield :head %>

<script>$("#id").hide();</script>

This example is equal to the following statements.

<% javascript_content_for :head do; javascript_tag do %>
  $("#id").hide();
<% end; end %>

<% javascript_content_for :head do %>
  <script>$("#id").hide();</script>
<% end %>

You can make subsequent calls to javascript_content_for and content_for, the result for each call will be appended to the value currently stored for name.



75
76
77
78
79
80
81
# File 'lib/helperful/javascript_helper.rb', line 75

def javascript_content_for(name, content_or_options_with_block = nil, html_options = {}, &block)
  if block_given?
    content_for(name, javascript_tag(capture(&block), content_or_options_with_block || {}))
  else
    content_for(name, javascript_tag(content_or_options_with_block, html_options))
  end
end