【问题标题】:Putting Javascript at the end of the file using Rails 3.1 asset pipeline使用 Rails 3.1 资产管道将 Javascript 放在文件末尾
【发布时间】:2011-11-07 15:17:45
【问题描述】:

有时在繁重的客户端 Javascript 中,我们将标签放在 HTML 文件的末尾,以便先显示内容,然后再加载 Javascript。

是否可以使用 Rails 3.1 资产管道做到这一点?

编辑:

<html>
<head>
   <%= javascript_include_tag "application" %>
</head>

<body>
<!-- all the page content goes here -->


<!-- we include these at the bottom to ensure the html loads first and the javascript is loaded afterwards. How can we achieve this through rails asset pipelining? -->

<script src="/some_other_assets/first_file.js"></script>
<script src="/some_other_assets/second_file.js"></script>
<script src="/some_other_assets/third_file.js"></script>

</body>

</html>

【问题讨论】:

    标签: javascript performance ruby-on-rails-3.1 asset-pipeline


    【解决方案1】:

    是的,您可以使用 Rails 3.1 做到这一点——在您的视图/布局中放置 javascript_include_tag 行并没有任何区别(就 Rails 而言)。

    【讨论】:

    • 我没有使用 javascript_include_tag。它是资产管道,它将结合所有的 js 文件。有没有办法告诉它在顶部包含某些文件,在底部包含其余文件?
    • 你对我没有任何意义。
    • 问题中的添加有帮助吗?
    • 是的:你做错了。这些文件不会与资产管道一起加载。为什么不对所有资源使用资产管道?你可以有多个基础文件;例如,我通常将所有供应商库放入vendor/assets/javascripts,并将资产管道连接并压缩到单个vendor.js 文件中,该文件可以包含在我的application.js 之前。
    • 对 nEEbz 的最后评论提供更详细和明确的答案会有所帮助。
    【解决方案2】:

    不确定您是否得到了答案,但您可以包含多个清单文件并在页面底部设置您想要的文件。

    换句话说,我可以创建一个 footer.js 清单文件,告诉它包含您想要的脚本,然后在您的视图模板的底部,以与您的 application.js 文件相同的方式包含它

    <%= javascript_include_tag("footer") %>
    

    标签。

    你可以在这里阅读更多-

    http://coderberry.me/blog/2012/04/24/asset-pipeline-for-dummies/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-19
      • 2011-11-23
      • 1970-01-01
      • 2012-02-04
      相关资源
      最近更新 更多