Rails 的 javascript_include_tag 可以忽略以前加载的脚本吗? [英] Can Rails' javascript_include_tag ignore previously-loaded scripts?

查看:37
本文介绍了Rails 的 javascript_include_tag 可以忽略以前加载的脚本吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用这一行:

= javascript_include_tag :all, :recursive =>真,:cache =>真的

在 Rails 应用的页脚中执行以下操作:

  • 递归加载public/javascripts下的所有脚本
  • 在生产中,将它们全部合并到一个文件中(:cache 部分)

但是,我想在头部加载这些脚本之一,因为某些内联 JS 依赖于它.

javascript_include_tag 是否有排除已加载文件的选项?

我知道默认情况下它不会这样做,因为现在我看到一个脚本在源 HTML 中被引用了两次.

解决方案

javascript_include_tag 是一个简单的 html 帮助器,不跟踪天气或页面上是否已包含某些内容,所以不,没有选择,糟透了.

取而代之的是,您可以通过以下几种方式解决问题:

  1. 使用诸如 Jammit 之类的东西,我现在正在使用它是一种绝对的快乐.它将生产中的所有 javascript 打包到一个文件中,允许您压缩它们,并且对 CSS 执行相同的操作.您可以使用 config 目录中的 .yml 文件对其进行配置,使其可移植且易于递归包含目录中的脚本,同时仍然控制它们包含在页面上或附加到包的顺序.Jammit 的另一种选择(不太好,恕我直言)是 Asset Packager..>

  2. 在头部使用 javascript_include_tag.真的不是世界末日.稍后优化.

  3. 并非真正特定于 Rails,而是使用 head.js.head.js 是相当新的,但允许您快速、并行地加载许多脚本,同时保留执行顺序并在脚本加载后但在 dom 准备好之前触发回调.它包括一堆其他好东西,如旧浏览器中的 HTML5 友好性.这个选项很有趣,因为您将脚本分开,或者至少在不同的包中,以获得最大的并行下载速度.我不确定天气或是否在 <head> 中或什至在页脚处连续下载一个巨大的压缩包,或者使用 head.js 并行加载内容会更快,但可以肯定使管理这些事情变得更容易.

我最近同时使用了 head.js 和 Jammit,我必须说,对于繁重的 Javascript 应用程序,它们让事情变得非常简单.

I'm using this line:

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

in the footer of a Rails app to do the following:

  • Load all the scripts under public/javascripts recursively
  • In production, lump them all into one file (the :cache part)

However, I want to load one of these scripts in the head, because some inline JS depends on it.

Is there an option for javascript_include_tag to exclude files that have already been loaded?

I know it doesn't do this by default, because right now I see a script referenced twice in the source HTML.

解决方案

javascript_include_tag is a simple html helper and doesn't track weather or not something has already been included on the page, so no, there isn't an option, sucks.

In place of this, you can solve your problem in a few ways:

  1. Use something like Jammit, which I use right now and is an absolute joy. It packages all your javascripts up in production into one file, allows you to compress the shit out of them, and does the same for CSS. You configure it using a .yml file in your config directory, making it portable and easy to recursively include scripts from directories while still controlling the order in which they are included on the page or appended to the package. An alternative (less awesome IMHO) to Jammit is Asset Packager.

  2. Use javascript_include_tag in the head. Not really the end of the world. Optimize later.

  3. Not really Rails specific, but use head.js. head.js is pretty new but allows you to load many scripts quickly, in parallel, while preserving the execution order and taking callbacks to fire once your scripts are loaded but before the dom is ready. It includes a bunch of other goodies like HTML5 friendliness in older browsers. This option is interesting because you keep your scripts separate, or at least in different packages for maximum parallel download speed. I'm not sure weather or not downloading one huge compressed package serially in the <head> or even right at the footer, or loading things in parallel using head.js will be faster, but it sure makes managing these things easier.

I've used both head.js and Jammit recently and I must say, for heavy Javascript apps, they make things wonderfully easy.

这篇关于Rails 的 javascript_include_tag 可以忽略以前加载的脚本吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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