简体   繁体   中英

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

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>

I added coffeescript under 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. I have a temporary fix of just using plain old javascript in app/assets/javascripts/applications.js, but I wanted to begin using coffeescript. Any suggestions?

You need to add paintIt to the global namespace. See here for good explanation on how to: stackoverflow.com/questions/4214731/

Via @vee. Copied here so this question can be closed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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