本文介绍了如何使用资产管道在导轨中导入javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 在我的应用程序中我有一个控制器支持一个非常复杂的对象,有很多javascript,用coffescript。 我想安排javascript在几个单独的文件,使代码布置得更好,虽然我不知道如何导入这些额外的文件。 例如,我有一个文件 app / assets / javascripts / general_functions.js.coffee 包含以下内容: / p> #舍入数字 roundNumber =(rnum,rlength = 5) - > pow = Math.pow(10,rlength) newnumber = Math.round(rnum * pow)/ pow parseFloat(newnumber) # floorNumber =(rnum,rlength = 5) - > pow = Math.pow(10,rlength) newnumber = Math.floor(rnum * pow)/ pow parseFloat(newnumber) # str结尾有后缀 endsWith =(str,suffix) - > str.indexOf(suffix,str.length - suffix.length)!= -1 #返回数字的绝对值(总是> = 0) abs =(num) - > if num 如何将其导入我的需要这些功能的资产/ javascripts / projects.js.coffee 我尝试添加 // = require general_functions 到 app / assets / javascripts / application.js ,但没有成功 感谢,解决方案 em>我猜想浏览器告诉你,你的 general_functions.js.coffee 函数不存在,你会得到错误,如: ReferenceError:roundNumber未定义 问题。 CoffeeScript文件的编译版本包装在自动执行的函数中,以防止命名空间污染,因此: roundNumber =(rnum, rlength = 5) - > pow = Math.pow(10,rlength) newnumber = Math.round(rnum * pow)/ pow parseFloat(newnumber) / pre> 到浏览器时看起来像这样: (function(){ var roundNumber; roundNumber = function(rnum,rlength){ // ... }; }) 并且隐藏您定义的所有函数。如果你想要你的函数是全局的,然后将它们定义为 window 属性: window.roundNumber =(rnum,rlength = 5) - > #... 或者更好地,您可以在之前创建一个特定于应用程序的命名空间加载main(Coffee | Java)脚本: app = {} 并将您的函数放在其中: .roundNumber =(rnum,rlength = 5) - > #... in my app I have one controller supporting a quite complex object that has a lot of javascript, written in coffescript.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. for example I have the file app/assets/javascripts/general_functions.js.coffee containing the following: # rounds a numberroundNumber = (rnum, rlength = 5) -> pow = Math.pow( 10, rlength ) newnumber = Math.round(rnum*pow)/pow parseFloat(newnumber)# floors a numberfloorNumber = (rnum, rlength = 5) -> pow = Math.pow( 10, rlength ) newnumber = Math.floor(rnum*pow)/pow parseFloat(newnumber)# returns true if the str ends with suffixendsWith = (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 numHow do I import it in my app/assets/javascripts/projects.js.coffee that needs these functions?I've tried with adding //= require general_functionsto app/assets/javascripts/application.js, with no successany 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: ReferenceError: roundNumber is not definedYou have a simple scoping issue. The compiled version of CoffeeScript files are wrapped in a self-executing function to prevent namespace pollution so this: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.roundNumber = (rnum, rlength = 5) -> # ...Or better, you can create an application-specific namespace somewhere before the main (Coffee|Java)Script is loaded:app = { }and put your functions in there:app.roundNumber = (rnum, rlength = 5) -> # ... 这篇关于如何使用资产管道在导轨中导入javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-12 21:45