admin管理员组文章数量:1167241
I'm using Rails 3.1 with CoffeeScript and have run into a snag. How do I call a function from a .js.erb file that is located in a .js.coffee file?
Say the function in .js.coffee is the following:
myName = -> "Bob"
I would think I could just call it like any regular js function such as:
var theName = myName();
but that doesn't seem to work. Any ideas?
or is it possible to use coffeescript in my .js.erb file to make everything the same?
I'm using Rails 3.1 with CoffeeScript and have run into a snag. How do I call a function from a .js.erb file that is located in a .js.coffee file?
Say the function in .js.coffee is the following:
myName = -> "Bob"
I would think I could just call it like any regular js function such as:
var theName = myName();
but that doesn't seem to work. Any ideas?
or is it possible to use coffeescript in my .js.erb file to make everything the same?
Share Improve this question edited Feb 13, 2012 at 1:02 mu is too short 434k71 gold badges857 silver badges818 bronze badges asked Feb 13, 2012 at 0:48 BradBrad 2312 silver badges3 bronze badges 1- 2 You should really accept Flambino's answer. It's great! – Andreas Lyngstad Commented Apr 9, 2014 at 12:37
1 Answer
Reset to default 56The reason you can't call the CoffeeScript function directly, is that CoffeeScript is wrapped in an immediately-invoked function when compiled. This is done to keep your code from polluting the global namespace.
This is generally A Good Idea™, but of course you can get around it when you need to. If you want a function or other variable to be accessible everywhere (i.e. global scope), you can simply say
window.myName = -> "Bob"
That way, the function is added directly to the global scope, and you can call it from anywhere as window.myName()
(or simply as myName()
unless the function's being shadowed by a local one).
However, to keep the global namespace as clean as possible, it's best to define a namespace for yourself (like jQuery does, by putting everything into the $
object). For instance, in your first CoffeeScript or JavaScript file (i.e. the first file to be loaded), you can do something like this
window.myNamespace = {};
Then, whenever you want something to be available elsewhere, you can add it to that namespace:
window.myNamespace.myName = -> "Bob"
And then you can call it from anywhere, using window.myNamespace.myName()
or simply myNamespace.myName()
.
Alternatively, you can use CoffeeScript's "assign if undefined or null" operator at the top of all your files:
window.myNamespace ?= {} # create myNamespace if it doesn't already exist
Whichever file gets evaluated first will create the missing window.myNamespace
object. Subsequent code will just see that it already exists and skip the assignment. Point is, it'll always be available, regardless of evaluation order.
Edit: Made myNamespace
lower-camelcase since it's basically a variable; not a constructor/class
Addendum: You can avoid the function wrapper by using the -b/--bare
command line switch, but as mentioned the wrapper is a good thing.
本文标签: RailsCalling CoffeeScript from JavaScriptStack Overflow
版权声明:本文标题:Rails - Calling CoffeeScript from JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1737566022a1996840.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论