本文介绍了资产管道:仅对一个控制器使用 javascript 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Ruby on Rails v4 中,我希望只为特定控制器加载一个 js 文件(或一组 js 文件).

In Ruby on Rails v4, I want a js file (or set of js files) to be only loaded for a particular controller.

执行此操作的标准方法是什么?

What is the standard way to do this?

在 application.js 中有 //= require tree . 行.我假设这需要删除,所以我并不总是加载每个文件.但是然后呢?使用 Mycontroller 时,是否总是加载名为 mycontroller.js 的文件?

In application.js there is the //= require tree . line. I'm assuming this would need to be removed so I'm not always loading every file. But then what? Will a file named mycontroller.js always be loaded when Mycontroller is used?

推荐答案

如果你做得对,资产管道可以非常强大:

The asset pipeline can be very powerful if you do it right:

清单

//= require tree . 的目的是创建一个清单"文件,Rails 将使用该文件来呈现您调用的文件.如果您不预编译"您的资产,这意味着每次您的浏览器加载您的应用程序时,它都会查找您的清单中包含的文件 &加载它们

The purpose of //= require tree . is to create a "manifest" file which Rails will use to render the files you call. If you don't "precompile" your assets, this means that each time your browser loads your app, it will look for the files contained in your manifest & load them

这意味着您可以定义在清单中调用的内容 &你不知道的.我们更喜欢在清单中调用任何基于 gem 的资产,但也指定特定的文件夹,如下所示:

This means that you can define what you call in your manifest & what you don't. We prefer to call any gem-based assets in the manifest, but also designate specific folders, like this:

//
//= require jquery
//= require jquery_ujs
//= require jquery.ui.draggable
//= require_tree ./jquery
//= require_tree ./extra
//= require turbolinks

我们使用这个设置来调用所有的 JQuery 插件 &任何额外的 JS,以及 gem 特定的文件

We use this setup to call all JQuery plugins & any extra JS, as well as the gem-specific files

预编译

如果您预编译您的资产,它基本上会构建不同的文件,这些文件在您加载浏览器时会一致加载.如果您使用 Heroku 或 CDN,您可能希望预编译您的资产,以减少延迟和减少延迟.依赖

If you pre-compile your assets, it basically builds different files which are loaded consistently when you load the browser. If you're using Heroku or a CDN, you may wish to precompile your assets, as to reduce latency & dependency

应用设计

要回答您的问题,您当然可以加载特定于控制器的 JS.我们这样做:

To answer your question, you can certainly load controller-specific JS. We do it like this:

#app/views/layouts/application.html.erb
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= stylesheet_link_tag controller_name, media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag controller_name, "data-turbolinks-track" => true %>

然后,您可以使用 Phoet 描述的内容来确保您的 JS 文件被拆分:

You can then ensure your JS files are split up by using what Phoet described:

#config/environments/production.rb
# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
config.assets.precompile += %w(mycontroller.js)

那么对于application.js,你只能调用你想在整个应用中持久化的文件:

Then for application.js, you can only call the files you want to persist throughout the app:

#app/assets/javascripts/application.js
//
//= require jquery
//= require jquery_ujs
//= require jquery.ui.draggable
//= require_tree ./jquery
//= require_tree ./extra
//= require turbolinks

这篇关于资产管道:仅对一个控制器使用 javascript 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 13:51
查看更多