简体   繁体   English

在Rails应用程序中加载自定义javascript文件

[英]Load custom javascript files in Rails app

I developed a javascript diagram tool (using kineticjs), and need to load some different js files. 我开发了一个javascript图表工具(使用kineticjs),并且需要加载一些不同的js文件。 But they need to be load in order. 但他们需要按顺序加载。 I manage to do that this way: 我设法这样做:

inside a specific view (in the top of the file): 在特定视图内(在文件顶部):

<%= javascript_include_tag "kinetic" %>
<%= javascript_include_tag "diagramtool/diagramtool0" %>
<%= javascript_include_tag "diagramtool/diagramtool1" %>
<%= javascript_include_tag "diagramtool/diagramtool2" %>
<%= javascript_include_tag "diagramtool/diagramtool3" %>
<%= javascript_include_tag "diagramtool/diagramtool5" %>
<%= javascript_include_tag "diagramtool/diagramtool6" %>

this in assets.rb: 这在assets.rb中:

# Be sure to restart your server when you modify this file.

# Version of your assets, change this if you want to expire all your assets.
Rails.application.config.assets.version = '1.0'

# Precompile additional assets.
# application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
# Rails.application.config.assets.precompile += %w( search.js )

Rails.application.config.assets.precompile += %w( kinetic.js )
Rails.application.config.assets.precompile += %w( diagramtool/diagramtool0.js )
Rails.application.config.assets.precompile += %w( diagramtool/diagramtool1.js )
Rails.application.config.assets.precompile += %w( diagramtool/diagramtool2.js )
Rails.application.config.assets.precompile += %w( diagramtool/diagramtool3.js )
Rails.application.config.assets.precompile += %w( diagramtool/diagramtool5.js )
Rails.application.config.assets.precompile += %w( diagramtool/diagramtool6.js )

And the files are located in assets/javascripts/diagramtool 这些文件位于assets/javascripts/diagramtool

The problem is that they are not load in order when I move to the view that has the code above to load the files. 问题是,当我移动到具有上述代码的视图来加载文件时,它们不会按顺序加载。 But if I refresh the page they are load in order! 但是,如果我刷新页面,他们按顺序加载!

So, my questions are 所以,我的问题是

How can I assure the js files are load in order? 如何确保js文件按顺序加载? Why the files are load in order when I refresh the page? 为什么在刷新页面时按顺序加载文件? And why they are not load in order when I navigate through the application? 当我浏览应用程序时,为什么它们没有按顺序加载?

There is another way to do this? 还有另一种方法吗? I tried to do it in application.js using the asset pipeline with the "require" key word. 我尝试使用带有“require”关键字的资产管道在application.js中执行此操作。 But I don't know how to do it there. 但我不知道该怎么做。

I also tried to put all the code in one file, but the file is load before the kinetic file! 我还尝试将所有代码放在一个文件中,但文件在kinetic文件之前加载! And I get errors because I need to load the library file first. 我得到错误,因为我需要先加载库文件。 But if I refresh the page it works again. 但是,如果我刷新页面它再次工作。

Any help? 有帮助吗? I don't really know what to do, to have this working properly. 我真的不知道该怎么做,让它正常工作。 I'm new at Ruby on Rails, and I'm still learning how to do this kind of "configurations". 我是Ruby on Rails的新手,我还在学习如何做这种“配置”。

Edit: I'm not using the application.js to load the js files, because I wasn't able to do it that way. 编辑:我没有使用application.js加载js文件,因为我无法这样做。 But here it is: 但这里是:

//= require jquery
//= require bootstrap-sprockets
//= require jquery
//= require jquery_ujs
//= require turbolinks

/* 
 require kinetic
 require diagramtool
 require_tree . 
 */

Just solved it this way: 刚刚解决这个问题:

created a manifest file diagrams.js: 创建了一个清单文件diagrams.js:

//= require diagramtool/kinetic
//= require diagramtool/diagramtool0
//= require diagramtool/diagramtool1
//= require diagramtool/diagramtool2

Added this in the view: 在视图中添加了以下内容:

<%= javascript_include_tag "diagrams" %>

Using the asset pipeline with a manifest file. 将资产管道与清单文件一起使用。

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

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