Created
September 27, 2008 01:53
-
-
Save geoffgarside/13241 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Put this into your app/helpers directory and provided | |
# you have `helper :all` in your app/controllers/application.rb | |
# it will be automatically included, otherwise include it | |
# into app/helpers/application_helper.rb or wherever you | |
# want to use it. | |
module JavascriptHelper | |
# Use in your application layout like | |
# - yield_javascript_onready | |
# couldn't be simpler. | |
def yield_javascript_onready | |
return if @content_for_onready.nil? | |
javascript_tag do | |
puts 'jQuery(document).ready(function($) {' | |
entab do | |
@content_for_onready.each_line do |line| | |
puts line.chomp | |
end | |
end | |
puts '});' | |
end | |
end | |
# HAML javascript_tag helper | |
def javascript_tag | |
haml_tag(:script, :type => 'text/javascript') do | |
puts '//<![CDATA[' | |
entab do | |
yield | |
end | |
puts '//]]>' | |
end | |
end | |
# Use in your various templates to add javascript | |
# to be run in the onready. Use it like | |
# - javascript_onready do | |
# :plain | |
# jQuery('body').text("hello world"); | |
# | |
# it will then be put inside the onready event | |
# when yield_javascript_onready is called. | |
def javascript_onready | |
content_for :onready do | |
yield | |
end | |
end | |
# HAML helper to en/detab a block. | |
def entab | |
tab_up | |
yield | |
tab_down | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment