简体   繁体   English

如何使用资产管道在 rails 中导入 javascript

[英]How to import javascript in rails with asset pipeline

in my app I have one controller supporting a quite complex object that has a lot of javascript, written in coffescript.在我的应用程序中,我有一个控制器支持一个非常复杂的对象,该对象有很多用 coffescript 编写的 javascript。

I would like to arrange the javascript on several separate files so to have the code arranged more nicely, although I can't figure out how to import these extra files.我想将 javascript 安排在几个单独的文件上,以便更好地安排代码,尽管我不知道如何导入这些额外的文件。

for example I have the file app/assets/javascripts/general_functions.js.coffee containing the following:例如,我的文件app/assets/javascripts/general_functions.js.coffee包含以下内容:

# rounds a number
roundNumber = (rnum, rlength = 5) ->
  pow = Math.pow( 10, rlength )
  newnumber = Math.round(rnum*pow)/pow
  parseFloat(newnumber)

# floors a number
floorNumber = (rnum, rlength = 5) ->
  pow = Math.pow( 10, rlength )
  newnumber = Math.floor(rnum*pow)/pow
  parseFloat(newnumber)

# returns true if the str ends with suffix
endsWith = (str, suffix) ->
  str.indexOf(suffix, str.length - suffix.length) !=   -1

# returns the absolute value of a number (always >= 0)
abs = (num) -> 
  if num < 0 then - num else num

How do I import it in my app/assets/javascripts/projects.js.coffee that needs these functions?如何将其导入到需要这些功能的app/assets/javascripts/projects.js.coffee中?

I've tried with adding我试过添加

//= require general_functions

to app/assets/javascripts/application.js , with no successapp/assets/javascripts/application.js ,没有成功

any ideas?有任何想法吗?

thanks,谢谢,

By no success I'm guessing that the browser is telling you that none of your general_functions.js.coffee functions exist and you're getting errors like:没有成功,我猜浏览器告诉你你的general_functions.js.coffee函数都不存在,你会收到如下错误:

ReferenceError: roundNumber is not defined参考错误:roundNumber 未定义

You have a simple scoping issue.您有一个简单的范围界定问题。 The compiled version of CoffeeScript files are wrapped in a self-executing function to prevent namespace pollution so this: CoffeeScript 文件的编译版本被包装在一个自执行函数中以防止命名空间污染,因此:

roundNumber = (rnum, rlength = 5) ->
  pow = Math.pow( 10, rlength )
  newnumber = Math.round(rnum*pow)/pow
  parseFloat(newnumber)

looks like this when it gets to the browser:当它到达浏览器时看起来像这样:

(function() {
  var roundNumber;
  roundNumber = function(rnum, rlength) {
    // ...
  };
})();

and all the functions you've defined are hidden.并且您定义的所有函数都是隐藏的。 If you want your functions to be global, then define them as window properties:如果你希望你的函数是全局的,那么将它们定义为window属性:

window.roundNumber = (rnum, rlength = 5) ->
  # ...

Or better, you can create an application-specific namespace somewhere before the main (Coffee|Java)Script is loaded:或者更好的是,您可以在加载主 (Coffee|Java)Script 之前的某处创建一个特定于应用程序的命名空间:

app = { }

and put your functions in there:并将您的功能放在那里:

app.roundNumber = (rnum, rlength = 5) ->
  # ...

Javascript should be automatically included in your rails application, every controller has its own js file. Javascript 应该自动包含在您的 Rails 应用程序中,每个控制器都有自己的 js 文件。 Using the instructions below will include them.使用下面的说明将包括它们。

Your app/assets/javascripts/application.js.coffee should have this line included:您的app/assets/javascripts/application.js.coffee应包含以下行:

#= require_tree .

Or app/assets/javascripts/application.js (plain javascript):或者app/assets/javascripts/application.js (纯 javascript):

//= require_tree .

When you are viewing your page's source in your browser you should see something like:当您在浏览器中查看页面的源代码时,您应该会看到如下内容:

<script src="/assets/projects.js?body=1"></script>

What you are describing is a helper or more global js file with generic features.您所描述的是具有通用功能的帮助程序或更全局的 js 文件。 You could add these to the application.js.您可以将这些添加到 application.js。 Moreover, using a structure like below will include vendor/assets/javascripts/some_generic_feature.js(.coffee)此外,使用如下结构将包括 vendor/assets/javascripts/some_generic_feature.js(.coffee)

//= require some_generic_feature

In application.js add both files in the correct order:application.js中以正确的顺序添加两个文件:

application.js应用程序.js

//= require general_functions
//= require projects

Hope this helps!希望这可以帮助!

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

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