If I wanted to port an existing project from Javascript to Coffeescript (in my case, within a Rails application), would I need to convert existing Javascript files? I'm worried about converting really large and CDN hosted files like jQuery and jQueryUI. How would I work around that?
-
4What are you talking about? Why in the world would you need to rewrite jQuery/Ui into coffeescript?asawyer– asawyer2012-10-23 21:48:28 +00:00Commented Oct 23, 2012 at 21:48
-
2CoffeeScript is for your own code, not third-party libraries...Šime Vidas– Šime Vidas2012-10-23 21:50:34 +00:00Commented Oct 23, 2012 at 21:50
-
See the comments on @Chris Peters answer.cereallarceny– cereallarceny2012-10-23 22:16:46 +00:00Commented Oct 23, 2012 at 22:16
2 Answers
As @asawyer stated, you do not need to port existing JavaScript libraries and such to CoffeeScript.
CoffeeScript exists to add convenience for writing your own custom code. Because the CoffeeScript compiles into JavaScript, it plays nicely with other JavaScript libraries like jQuery without your needing to convert those libraries into CoffeeScript.
If you have your own code that you want to convert into CoffeeScript, I've found js2coffee.org very helpful. It also serves as a great learning tool for "thinking in JavaScript" and seeing how it would be done in CoffeeScript.
4 Comments
Within rails, coffeescript is run through a compiler and into javascript anyway. You can include pure javascript files if you want (.js), or coffeescript (.js.coffee), which is compiled into js. If you're just looking to include legacy files, you won't need to do any conversion at all -- Might not make sense to convert to coffeescript, just to have rails compiler convert back to javascript in the asset pipeline... See http://guides.rubyonrails.org/asset_pipeline.html#coding-links-to-assets for more from the rails team on how to use js in the asset pipeline.
jQuery is already included in newer versions of Rails (3.1 onward), so that shouldn't be an issue. http://weblog.rubyonrails.org/2011/4/21/jquery-new-default/
Also, many other jQuery addons have a gem that you can simply include -- no need to reinvent the wheel there.