我有shared.js.coffee
Shared =
shared_method: (msg) ->
alert(msg)
我有test.js.coffee
Shared.shared_method("this doesn't work")
我按照https://stackoverflow.com/a/20563242/656510
所述,首先在application.js中加载共享代码 //= require ./shared
//= require_tree .
但是它在开发中不起作用,我得到:未捕获的ReferenceError:未定义共享
当我查看JS时,它将作为单独的文件提供,而不是像生产中那样编译到application.js中。
正如我认为可能与Asset Pipeline配置有关,我创建了一个干净的Rails 4.1应用程序来演示这个问题。
https://github.com/itinsley/asset_pipeline_weirdness
非常感谢。
答案 0 :(得分:1)
您需要使用全局命名空间:
@Shared =
shared_method: (msg) ->
alert(msg)
如果您愿意,可以为您的应用使用命名空间:
@myApp ||= {}
myApp.Shared =
shared_method: (msg) ->
alert(msg)