使用javascript_include_tag和一个充满JS的子文件夹

时间:2011-08-04 20:34:25

标签: javascript ruby ruby-on-rails-3

我在/public/javascripts/test下的文件夹中有一堆Javascripts,我希望尽可能少地使用javascript_include_tag将它们包含在我的布局中。

目前我使用javascript_include_tag有25行代码,有没有办法更好地做到这一点?另外我不能使用javascript_include_tag :all :recursive,因为在其他目录中有一些我不想包含的JS文件。

感谢您的帮助。

更新 所以现在我有一个javascript.rb初始化程序,它有这一行代码(非常长):

ActionView::Helpers::AssetTagHelper.register_javascript_expansion :syntax_highlight => Dir["#{Rails.root.to_s}/public/javascripts/sh/*.js"].each {|js| js.gsub!("#{Rails.root.to_s}/public/javascripts/",'')}

现在我的布局中有以下内容:

<%= javascript_include_tag :syntax_highlight %>

现在,当我渲染页面时,我得到以下内容:

<script src="/javascripts/sh/shAutoloader.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushAppleScript.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushAS3.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushBash.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushColdFusion.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushCpp.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushCSharp.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushCss.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushDelphi.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushDiff.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushErlang.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushGroovy.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushJava.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushJavaFX.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushJScript.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushPerl.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushPhp.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushPlain.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushPowerShell.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushPython.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushRuby.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushSass.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushScala.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushSql.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushVb.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushXml.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shCore.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shLegacy.js?1278089940" type="text/javascript"></script> 

但由于某些原因,这一切都不起作用?是什么给了什么?

3 个答案:

答案 0 :(得分:1)

可能还有更好的解决方案,但这仍然可以解决问题:

Dir["#{Rails.root.to_s}/public/javascripts/test/*.js"].each {|js_file| javascript_include_tag js_file }

答案 1 :(得分:1)

试试这个

在config / application.rb

取消注释此行

config.action_view.javascript_expansions[:defaults] = %w(javascript1.js javascript2.js)

并添加文件的名称,例如javascrip1.js和javascrip2t.js等。

然后在views / layouts中添加此行

<%= javascript_include_tag :defaults %>

Vlad Khomich的答案也很棒,也会奏效。

答案 2 :(得分:1)

考虑到你包含了这么多js文件,我假设你要在公共目录下包含所有js文件。如果是这种情况,您可以更改javascript_include_tag,如下所示,它将帮助您将整个文件作为生产文件提供,从而节省大量请求:

javascript_include_tag :all, :recursive => true, :cache => true

如果您想单独处理测试文件,可以register_javascript_expansion:defaults,也可以使用Vlad Khomich的答案代码。

# config/init/javascript.rb
ActionView::Helpers::AssetTagHelper.register_javascript_expansion :tests => [array_of_js_paths_from_previous_answer]

# application.html.erb
<%= javascript_include_tag :tests, :cache => :tests %>