简体   繁体   English

Coffeescript未捕获参考

[英]Coffeescript Uncaught Reference

I'm following the first steps of the rails guide http://edgeguides.rubyonrails.org/working_with_javascript_in_rails.html#unobtrusive-javascript 我正在遵循Rails指南的第一步,网址是http://edgeguides.rubyonrails.org/working_with_javascript_in_rails.html#unobtrusive-javascript

The index.html.erb looks like <a href="#" onclick="paintIt(this, '#990000')">Paint it red</a> <a href="#" onclick="paintIt(this, '#009900', '#FFFFFF')">Paint it green</a> <a href="#" onclick="paintIt(this, '#000099', '#FFFFFF')">Paint it blue</a> index.html.erb看起来像<a href="#" onclick="paintIt(this, '#990000')">Paint it red</a> <a href="#" onclick="paintIt(this, '#009900', '#FFFFFF')">Paint it green</a> <a href="#" onclick="paintIt(this, '#000099', '#FFFFFF')">Paint it blue</a>

I added coffeescript under app/assets/javascripts/welcome.js.coffee paintIt = (element, backgroundColor, textColor) -> element.style.backgroundColor = backgroundColor if textColor? element.style.color = textColor 我在app / assets / javascripts / welcome.js.coffee paintIt = (element, backgroundColor, textColor) -> element.style.backgroundColor = backgroundColor if textColor? element.style.color = textColor paintIt = (element, backgroundColor, textColor) -> element.style.backgroundColor = backgroundColor if textColor? element.style.color = textColor

I get this error: 我收到此错误:
Uncaught ReferenceError: paintIt is not defined

I tried chancing paintIt to @paintIt and window.paintIt to no avail. 我尝试将@paintIt paintIt@paintItwindow.paintIt无济于事。 I have a temporary fix of just using plain old javascript in app/assets/javascripts/applications.js, but I wanted to begin using coffeescript. 我有一个临时解决方案,只是在app / assets / javascripts / applications.js中使用普通的旧javascript,但是我想开始使用coffeescript。 Any suggestions? 有什么建议么?

You need to add paintIt to the global namespace. 您需要将paintIt添加到全局名称空间。 See here for good explanation on how to: stackoverflow.com/questions/4214731/ 参见此处,了解有关如何操作的详细说明: stackoverflow.com/questions/4214731/

Via @vee. 通过@vee。 Copied here so this question can be closed. 在此处复制,因此可以关闭此问题。

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

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