Ruby-on-rails 如何将 Javascript 文件导入 Haml 视图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11455551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 03:41:10  来源:igfitidea点击:

How do import a Javascript file into a Haml view?

ruby-on-railsruby-on-rails-3

提问by Leila Hamon

I want to include some Javascript functionality in a Haml view, but I don't want it inserted into every view. Do I have to put the Javascript file in public/javascripts or can I put it in app/assets/javascripts to keep it hidden from user, and still reference from within the haml view file?

我想在 Haml 视图中包含一些 Javascript 功能,但我不希望它插入到每个视图中。我是否必须将 Javascript 文件放在 public/javascripts 中,或者我可以将它放在 app/assets/javascripts 中以使其对用户隐藏,并且仍然从 haml 视图文件中引用?

How would you do both these options if they are possible?

如果可能,您将如何执行这两个选项?

回答by Draiken

You should just use

你应该只使用

!!!
%html
  %head
    = javascript_include_tag "my_js_file"

if it's specific to one place, you should use content_for

如果它特定于一个地方,你应该使用 content_for

!!!
%html
  %head
    = yield(:javascripts)

And then in your view

然后在你看来

- content_for :javascripts do
  = javascript_include_tag "my_js_file"

回答by Ben Taitelbaum

Include Directly

直接包含

If you want the javascript included directly into the haml, you can use :javascript

如果您希望将 javascript 直接包含在 haml 中,您可以使用 :javascript

:javascript
  $(function() { alert("js inside haml"); }

You can put this into a partial and then just render the partial to keep your views clean.

您可以将其放入局部,然后仅渲染局部以保持视图清洁。

Reference It

参考它

If you want to just reference javascript and have the browser pull it in, you should use javascript_include_taglike always. Here, you'll need to make the javascript file a manifest, instead of requiring it into the application.jsmanifest. Remember to add the manifest to config.assets.precompilein your application.rb, according to http://guides.rubyonrails.org/asset_pipeline.html

如果你只想引用 javascript 并让浏览器把它拉进来,你应该javascript_include_tag像往常一样使用。在这里,您需要将 javascript 文件设为清单,而不是要求将其放入application.js清单。config.assets.precompile根据http://guides.rubyonrails.org/asset_pipeline.html,记得在 application.rb 中添加清单

(in your haml):

(在你的哈姆里):

= javascript_include_tag 'somefile'

(in config/application.rb):

(在 config/application.rb 中):

config.assets.precompile += ['somefile.js']

回答by Wei

If your javascript is small and dealy simple, I would suggest include javascript directly in the HAML:

如果您的 javascript 很小而且很简单,我建议直接在 HAML 中包含 javascript:

:javascript
  alert('hi hi!')

Otherwise you should use asset pipeline. It makes sure that your javascripts are pre-processed, compressed and minified. It also helps to keep your javascripts well organized (e.g. separation between your scripts and vendor scripts) and easily testable (with testing frameworks like jasmine/jasminerice/evergreen). If you are new to asset pipeline, hereis a good read =)

否则,您应该使用资产管道。它确保您的 javascript 被预处理、压缩和缩小。它还有助于使您的 javascript 井井有条(例如,您的脚本和供应商脚本之间的分离)和易于测试(使用jasmine/ jasminerice/ evergreen等测试框架)。如果您不熟悉资产管道,这里是一个很好的阅读 =)