Rails - 从JavaScript调用CoffeeScript [英] Rails - Calling CoffeeScript from JavaScript

查看:88
本文介绍了Rails - 从JavaScript调用CoffeeScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Rails 3.1和CoffeeScript,并遇到了障碍。如何从位于.js.coffee文件中的.js.erb文件调用函数?

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?

说明.js.coffee中的函数如下:

Say the function in .js.coffee is the following:

myName = -> "Bob"



我想我可以像任何常规js函数一样调用它, p>

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?

还是可以在我的.js.erb文件中使用coffeescript使一切都一样?

or is it possible to use coffeescript in my .js.erb file to make everything the same?

推荐答案

无法直接调用CoffeeScript函数的原因是,编译时CoffeeScript包含在立即调用的函数中。这是为了防止你的代码污染全局命名空间。

The 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"

这样,函数直接添加到全局作用域,从任何地方 window.myName()(或简称为 myName(),除非函数被本地)。

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).

然而,为了保持全局命名空间尽可能干净,最好为自己定义一个命名空间(像jQuery一样) em>进入 $ 对象)。例如,在您的第一个CoffeeScript或JavaScript文件(即要加载的第一个文件)中,您可以执行这样的操作

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"

然后你可以使用窗口从任何地方调用它。 myNamespace.myName()或简单地 myNamespace.myName()

And then you can call it from anywhere, using window.myNamespace.myName() or simply myNamespace.myName().

您可以在所有文件的顶部使用CoffeeScript的assign if undefined or null运算符:

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

无论哪个文件首先被评估,将创建缺少的 window.myNamespace 对象。后续代码将只看到它已经存在并跳过分配。

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.

编辑:Made myNamespace 下骆驼壳,因为它基本上是一个变量;不是构造函数/类

Made myNamespace lower-camelcase since it's basically a variable; not a constructor/class

附录:您可以使用 -b / - bare 命令行开关,但如上所述,包装器是一件好事。

Addendum: You can avoid the function wrapper by using the -b/--bare command line switch, but as mentioned the wrapper is a good thing.

这篇关于Rails - 从JavaScript调用CoffeeScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆