简体   繁体   English

在Rails 4中加载JavaScript文件

[英]Loading javascript files in rails 4

I have a structure in my rails assets directory which is shown below. 我的rails资产目录中有一个结构,如下所示。

--javascripts
 -utils // directory
   helper.js
session.js
app.js
application.js
home.js

This above is my directory structure in rails. 上面是我在rails中的目录结构。 When i load my application i get the below error which i feel is the JS is not getting loaded properly . 当我加载我的应用程序时,出现以下错误,我感觉是JS is not getting loaded properly

Uncaught TypeError: Cannot read property 'addMethod' of undefined additional-methods.min.js?body=1:3
Uncaught ReferenceError: Helper is not defined login.js?body=1:22
Uncaught TypeError: Cannot read property 'validate' of undefined 

Below is my application.js 下面是我的application.js

//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require vendor
//= require_tree .

Is there anything specific if we have to do for loading files from a specific directory?. 是否需要执行特定操作以从特定目录加载文件? How to load the Utils directory and other files in my application.js 如何在我的application.js加载Utils目录和其他文件

From the docs: 从文档:

Directives are processed top to bottom, but the order in which files are included by require_tree is unspecified. 指令从上到下进行处理,但是未指定require_tree包含文件的顺序。 You should not rely on any particular order among those. 您不应该依赖其中的任何特定顺序。 If you need to ensure some particular JavaScript ends up above some other in the concatenated file, require the prerequisite file first in the manifest. 如果您需要确保某些特定的JavaScript在串联文件中排在其他某些JavaScript之上,请在清单中首先要求必备文件。

So if you need something from Utils to be defined before anything in, say, home.js I'd add require_directory utils above the require_tree . 因此,如果您需要在Utils中定义一些内容,例如在home.js定义任何内容,我会在require_directory utils上方添加require_directory utils require_tree . statement. 声明。 I suppose this could be what's going wrong with your javascript and throwing the errors you posted. 我想这可能是您的JavaScript出了什么问题并引发了您发布的错误。

This is what the docs say about require_directory : 这就是文档对require_directory

You can also use the require_directory directive which includes all JavaScript files only in the directory specified, without recursion. 您还可以使用require_directory指令,该指令仅在指定目录中包含所有JavaScript文件,而无需递归。

Is there anything specific if we have to do for loading files from a specific directory? 是否需要执行特定操作以从特定目录加载文件?

The way to do that is to use the require_directory or require_tree . 做到这一点的方法是使用require_directoryrequire_tree . directives on the other directory: 另一个目录上的指令:

#app/assets/javascripts/application.js
#= require_tree ./utils 

However, if you're using require_tree . 但是,如果您使用的是require_tree . , this will basically include all the directories under your parent (it's called recursion ) ,这基本上将包括您parent目录下的所有目录(称为recursion


As Nicolay has mentioned, you're basically looking to include your JS in the correct order. 正如Nicolay所提到的,您基本上是希望以正确的顺序包含JS。 Although I'm unsure as to why you're getting errors for login.js and additional-methods.min.js 尽管我不确定为什么您会在login.jslogin.js additional-methods.min.js遇到错误

It means your JS is trying to call them, but they are not loaded. 这意味着您的JS正在尝试调用它们,但未加载它们。 This would only cause an error in the likes of precompilation (load order shouldn't be a problem if you're just running in dev) 这只会导致类似预编译的错误(如果您只是在dev中运行,则加载顺序应该不是问题)

The answer is to define your manifest as to include the various files when they're needed: 答案是将清单定义为在需要时包括各种文件:

#app/assets/javascripts/application.js
//= require jquery
//= require jquery_ujs
//= require_tree .
//= require vendor
//= require bootstrap

But more importantly - how are the functions being called? 但更重要的是- 函数如何调用?

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

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