使用javascript_include_tag和一个充满JS的子文件夹 [英] Using javascript_include_tag with a Subfolder full of JS

查看:83
本文介绍了使用javascript_include_tag和一个充满JS的子文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a bunch of Javascripts in a folder under /public/javascripts/test and I want to include them in my layout using javascript_include_tag in as few lines as possible.

目前我有25行代码使用 javascript_include_tag ,有什么办法可以做得更好吗?另外我不能使用 javascript_include_tag:all:recursive 因为其他目录中有一些我不想包含的JS文件。

Currently I have 25 lines of code using javascript_include_tag, is there any way to do this better? Also I cannot use javascript_include_tag :all :recursive since there are some JS files in other directories that I do not want included.

感谢您的帮助。

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

UPDATE: So now I have a javascript.rb initializer that has this single line of code (very long):

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/",'')}

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

Now in my layout I have the follwing:

<%= javascript_include_tag :syntax_highlight %>

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

Now when I render the page I get the following:

<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> 

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

But for some reason none of this works? What gives?

推荐答案

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

Considering that you are including so many js files, I am assuming, that you want to include all the js files under public directory. If this is the case, you can change in your javascript_include_tag as following, it will help you serve the whole file as one in production, saving lots of requests:

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

如果你想单独处理测试文件,你可以 register_javascript_expansion ,如:默认,并使用Vlad Khomich的回答代码。

If you want to treat the test files saperately, you can register_javascript_expansion, like :defaults, and also use the code from Vlad Khomich's answer.

# 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 %>

这篇关于使用javascript_include_tag和一个充满JS的子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆