资产管道:仅将JavaScript文件用于一个控制器 [英] Asset pipeline: use javascript files for only one controller

查看:26
本文介绍了资产管道:仅将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文件用于一个控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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