Skip to content

Instantly share code, notes, and snippets.

@ream88
Created December 16, 2010 11:38
Show Gist options
  • Save ream88/743309 to your computer and use it in GitHub Desktop.
Save ream88/743309 to your computer and use it in GitHub Desktop.

Usage:

javascript_include_tag('jquery.js', 'app.js', :use_head_loader => true) will result in:

<script src="/javascripts/head.load.min.js?1292496592" type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
head.js('/javascripts/jquery.js?1277379629', '/javascripts/app.js?1291631927');
//]]>
</script>

javascript_include_tag('jquery.js', 'app.js', :use_head_loader => 'my_custom_name_head.load.min.js') will result in:

<script src="/javascripts/my_custom_name_head.load.min.js?1292496592" type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
head.js('/javascripts/jquery.js?1277379629', '/javascripts/app.js?1291631927');
//]]>
</script>

javascript_include_tag('jquery.js') javascript_include_tag('app.js') will result in (head.js only included once):

<script src="/javascripts/head.load.min.js?1292496592" type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
head.js('/javascripts/jquery.js?1277379629');
//]]>
</script>
<script type="text/javascript">
//<![CDATA[
head.js('/javascripts/app.js?1291631927');
//]]>
</script>
def javascript_include_tag(*sources)
options = sources.extract_options!.stringify_keys
concat = options.delete("concat")
cache = concat || options.delete("cache")
recursive = options.delete("recursive")
use_head_loader = options.delete("use_head_loader")
if concat || (config.perform_caching && cache)
joined_javascript_name = (cache == true ? "all" : cache) + ".js"
joined_javascript_path = File.join(joined_javascript_name[/^#{File::SEPARATOR}/] ? config.assets_dir : config.javascripts_dir, joined_javascript_name)
unless config.perform_caching && File.exists?(joined_javascript_path)
write_asset_file_contents(joined_javascript_path, compute_javascript_paths(sources, recursive))
end
if use_head_loader
concat(javascript_src_tag(use_head_loader == true ? 'head.load.min.js' : use_head_loader, options)) unless @use_head_loader
@use_head_loader = true
javascript_tag("head.js('#{joined_javascript_name}');")
else
javascript_src_tag(joined_javascript_name, options)
end
else
sources = expand_javascript_sources(sources, recursive)
ensure_javascript_sources!(sources) if cache
if use_head_loader
concat(javascript_src_tag(use_head_loader == true ? 'head.load.min.js' : use_head_loader, options)) unless @use_head_loader
@use_head_loader = true
javascript_tag("head.js(#{sources.collect { |source| "'#{path_to_javascript(source)}'" }.join(', ')});")
else
sources.collect { |source| javascript_src_tag(source, options) }.join("\n").html_safe
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment