如果 javascript 仅用于特定页面,为什么不应该将其置于视图中? [英] Why shouldn't javascript be placed in view if it is only used on specific pages?

查看:36
本文介绍了如果 javascript 仅用于特定页面,为什么不应该将其置于视图中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

他的回答 Richard Peck 写道:

不显眼的 JS

还有一点需要考虑(您已经这样做了),您确实需要在您的应用程序中使用不显眼的 javascript.

Something else to consider (you've already done this), is that you really need to use unobtrusive javascript in your application.

Unobtrusive JS 基本上意味着你能够抽象你的绑定";从您的页面到资产管道中的 Javascript 文件.这有几个重要原因:

Unobtrusive JS basically means that you're able to abstract your "bindings" from your page to your Javascript files in the asset pipeline. There are several important reasons for this:

  • 您的 JS 可以加载到您想要的任何页面(它是 DRY)

  • Your JS can be loaded on any page you want (it's DRY)

您的 JS 将驻留在后端"中的应用程序(不会污染视图)

Your JS will reside in the "backend" of your app (won't pollute views)

您将能够使用 JS 在屏幕上填充您想要的各种元素/对象

You'll be able to use the JS to populate the various elements / objects you want on screen

总是建议你把你的 JS 放在单独的文件中——包括在视图中,让你一团糟

It's always recommended you put your JS into separate files - including in the views sets you up for a big mess down the line

这让我想到以下问题:

如果我只在某个页面上使用脚本,我为什么要在每个页面上加载它?这不违背 DRY 吗?也许我没有正确理解 Rails 管道的工作原理.

If I am only using a script on a certain page, why would I want to have it load on every page? Doesn't that go against the DRY? Maybe I am not properly understanding how the Rails pipeline works.

推荐答案

Rails 管道

Rails pipeline

这不是 Rails 管道,unobtrusive JS 是标准的编程模式.

It's not Rails pipeline, unobtrusive JS is a standard programming pattern.

从页面(内联")中提取 JS 到外部文件会清理页面,仅此而已.

Extracting JS from the page ("inline") to an external file cleans up the page, nothing more.

如果你想快速加载页面,你需要将 JS 拆分成单独的文件.这可以是类的形式,但主要用于页面特定的功能.例如,您可能有 admin.jsapplication.js.

If you want to get the page loaded quickly, you need to split up the JS into separate files. This could be in the form of classes but is mostly for page-specific functionality. For example, you may have admin.js and application.js.

导轨

就 Rails 而言,处理不引人注目的 JS 的方式归结为您如何预编译您的资产.标准的方法是将所有的功能放到application.js中——这显然会变得臃肿.

In regard to Rails specifically, the way to handle unobtrusive JS comes down to how you precompile your assets. The standard way is to put all the functionality into application.js - which will obviously become bloated.

解决这个问题的方法是使用 config.assets.precompile 钩子 - 允许您指定要包含为单独预编译元素的文件:

The way around this is to make use of the config.assets.precompile hook - allowing you to specify files you want to include as separately precompiled elements:

# config/application.rb
config.assets.precompile << %w(admin.js cart.js etc.js)

这将更改为由 manifest.js处理Sprockets 4+ (如果需要,我可以在更新中解释这一点).我向他们的 repo 提交了commit.

This is going to change to being handled by manifest.js in Sprockets 4+ (I can explain this in an update if required). I made a commit to their repo about it.

这意味着当你预编译资产时(或者当你在开发中运行它们时——它们被缓存),你会得到 admin.js 或任何你定义为单独预编译的东西.这本身没有任何意义;这些文件只会出现在 public/assets 中.

This means that when you precompile the assets (or when you run them in dev -- they're cached), you get admin.js or whatever you defined to be precompiled separately. This means nothing on its own; the files will just appear in public/assets.

作用的意思是,您可以在布局中引用这些文件:

What it does mean is that you can then reference the files in your layout:

# app/views/layouts/application.html.erb
<%= javascript_include_tag :application, (:admin if [[condition]]) %>

or

# app/views/layouts/admin.html.erb
<%= javascript_include_tag :admin %>

因此,您将能够在需要时调用所需的文件.

Thus, you will be able to call the files you need when you need them.

我可以更深入,但这应该可以回答直接的问题.

I can go into more depth but this should answer the immediate question.

这篇关于如果 javascript 仅用于特定页面,为什么不应该将其置于视图中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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