6. Things Iâll cover today:
A brief primer on the CoffeeScript language
How it works with the Rails 3.1 asset
pipeline
Running the same JS/CS code on both the
client and the server
7. And things I wonât:
The CoffeeScript language in depth (that
would take a whole book... available at
http://coffeescript-book.com)
Sprockets 2 (#=âŠrequireâŠfoo)
jQuery UJS ($.rails)
8. CoffeeScript crash course
âDash rocketâ instead of function
Semantic whitespace instead of {}
Implicit parentheses, implicit var scoping
10. Things that feel weird to
Rubyists
Parentheses are not optional on functions
with no arguments
Itâs just JavaScriptâ˘
11. Things that feel weird to
JavaScripters
CoffeeScriptâs of is JavaScriptâs in
CoffeeScriptâs in is Pythonâs in
CoffeeScriptâs == is JavaScriptâs ===
12. Tradeoffs
Debugging (will get better, but currently
kind of a pain)
Changing versions (though itâs been
pretty stable for the last 5 months)
Less documentation (but there is a
book from PragProg...)
13. Even so!
After only a week of using CoffeeScript,
I felt more productive than I ever had in
JavaScript.
The community is awesome.
Learning CoffeeScript might make you think
more deeply about JavaScript.
15. Letâs make a Rails app!
railsâŠnewâŠEndToEnd
cdâŠEndToEnd
railsâŠgenerateâŠcontrollerâŠUserâŠsignup
Clone the repo if you want to follow along...
http://github.com/TrevorBurnham/EndToEnd)
16. JavaScripts are assets
New in Rails 3.1, generateâŠcontroller gives
us a CoffeeScript ďŹle automatically:
17. Compound extensions
.html.erb & .html.haml â HTML
.css.scss & .css.sass â CSS
.js.coffee â JavaScript
Want to use a different format? Just rename!
application.js â application.js.coffee
18. Compound extensions
Stylesheets and JavaScripts are automatically
concatenated by Sprockets 2, even in development:
All the CSS and JS is loaded on every page, so itâs
still up to you to isolate controller-speciďŹc
functionality.
21. Great, but...
Client-side validations are just there to
provide a friendlier user experience. What
really matters are server-side validations.
Traditionally, weâd do this by using
ActiveRecord, repeating much of the work
we just did...
But thereâs another way!
22. Server-side JavaScript!
(And CoffeeScript)
If youâve got coffeeâscript in your
GemďŹle, then youâve already got ExecJS:
https://github.com/sstephenson/execjs
It uses the best available JS environment on
your system. Use therubyracer (or
therubyracerâheroku) for maximum
portability.
23. No need to rewrite the
same validation code!
ExecJS lets us turn our CoffeeScript
validation code into a Ruby object:
24. A small taste of
whatâs to come
Future plugins will make the server-side JS
process smoother in Rails
With jsdom, you can do practically anything
that you could do with client-side JS
(including manipulating HTML with jQuery!)
Validation, templating... anything youâre
duplicating can be done as end-to-end JS.
25. How can you become a
CoffeeScript ninja?
OfďŹcial site, great overview:
http://coffeescript.org
Nice screencast:
http://peepcode.com/products/coffeescript
Totally unbiased book recommendation:
http://coffeescript-book.com