简体   繁体   English

使用Rails资产助手来运行Javascript插件

[英]Use Rails asset helper to run a Javascript plug in

dealing with an interesting problem. 处理一个有趣的问题。 I'm using a third party plug in, Galleria. 我正在使用第三方插件Galleria。 It runs from the body of the view like so 它像这样从视图主体运行

<script>
Galleria.loadTheme('/assets/galleria/themes/classic/galleria.classic.min.js');
Galleria.run('.galleria'...
</script>

I've realized that with the Rails asset pipeline, I have to use helper tags, otherwise the asset doesn't load. 我已经意识到,在Rails资产管道中,我必须使用helper标签,否则资产不会加载。 Therefore I subbed the first line out like so: 因此,我像这样对第一行进行了替换:

Galleria.loadTheme("<%= javascript_include_tag 'galleria.classic.min.js', '/galleria/themes/classic' %>");

But I'm not sure what to do about the Galleria.run('.galleria' part, since that's not an asset, that's telling it to run the thing that it should have loaded. Leaving it as is has not worked, so wanted to see if there were any other ideas. 但是我不确定该如何处理Galleria.run('。galleria'部分,因为这不是资产,而是要告诉它运行应该加载的东西。看看是否还有其他想法。

EDIT: What ended up working was this lovely post from someone else, + a few bits that I had to add on myself: How to use Galleria plugin with Rails 4 Pipeline 编辑:最终成功的是其他人的这篇可爱的帖子,还有我不得不补充的几点: 如何在Rails 4 Pipeline中使用Galleria插件

I think you want 我想你要

Galleria.loadTheme("<%= javascript_include_tag '/galleria/themes/classicgalleria.classic.min.js'%>")

Assuming you've put the js in assets/javascripts? 假设您已将js放在资产/ javascript中?

You may need to add something to the assets path for compilation. 您可能需要在资产路径中添加一些内容以进行编译。 I think you can't do the javascript_include_tag directly in line. 我认为您不能直接在线执行javascript_include_tag That should generate a full <script></script> tag. 那应该生成一个完整的<script></script>标记。 What you really want is just the asset compiled filename. 您真正想要的只是资产编译的文件名。 If you've put the javascript files in vendor/assets/javascripts then add them to the precompile list 如果您已将javascript文件放置在vendor/assets/javascripts则将它们添加到预编译列表中

# in environments/production.rb
config.assets.precompile += %w( galleria.classic.min.js )

then I think you can include it like this: 那么我认为您可以这样包含它:

Galleria.loadTheme('/assets/galleria.classic.min.js');

You may need to use a different path helper to get the digest version if the non-digest version is not available. 如果非摘要版本不可用,则可能需要使用其他路径助手来获取摘要版本。 I should look something like: 我应该看起来像:

Galleria.loadTheme('<%= asset_path 'galleria.classic.min.js' %>');

Good luck. 祝好运。 Galleria is a nice slideshow. 环球免税店是一个不错的幻灯片演示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM