End-to-End
  CoffeeScript
Presented by Trevor Burnham
     at RailsConf 2011
Part I
CoffeeScript!?!?
What is CoffeeScript?
Do you have to use it?
But seriously...
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
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)
CoffeeScript crash course



 “Dash rocket” instead of function

 Semantic whitespace instead of {}

 Implicit parentheses, implicit var scoping
Awesome goodies for
      Rubyists


Postfix if/unless

Implicit returns

String interpolations
Things that feel weird to
        Rubyists



Parentheses are not optional on functions
with no arguments
It’s just JavaScript™
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 ===
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...)
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.
Part II
CoffeeScript in Rails 3.1
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)
JavaScripts are assets
New in Rails 3.1, generate
controller gives
us a CoffeeScript file automatically:
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
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-specific
functionality.
Building a sign-up box
Adding JS validations
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!
Server-side JavaScript!
  (And CoffeeScript)
If you’ve got coffee‐script in your
Gemfile, 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.
No need to rewrite the
 same validation code!
ExecJS lets us turn our CoffeeScript
validation code into a Ruby object:
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.
How can you become a
  CoffeeScript ninja?
Official site, great overview:
http://coffeescript.org
Nice screencast:
http://peepcode.com/products/coffeescript
Totally unbiased book recommendation:
http://coffeescript-book.com
Thanks
        Questions?
trevorburnham@gmail.com

End to-End CoffeeScript

  • 1.
    End-to-End CoffeeScript Presentedby Trevor Burnham at RailsConf 2011
  • 2.
  • 3.
  • 4.
    Do you haveto use it?
  • 5.
  • 6.
    Things I’ll covertoday: 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 Iwon’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
  • 9.
    Awesome goodies for Rubyists Postfix if/unless Implicit returns String interpolations
  • 10.
    Things that feelweird to Rubyists Parentheses are not optional on functions with no arguments It’s just JavaScript™
  • 11.
    Things that feelweird 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 getbetter, 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 onlya 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.
  • 14.
  • 15.
    Let’s make aRails 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 Newin Rails 3.1, generate
controller gives us a CoffeeScript file 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 andJavaScripts 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-specific functionality.
  • 19.
  • 20.
  • 21.
    Great, but... Client-side validationsare 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 Gemfile, 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 torewrite the same validation code! ExecJS lets us turn our CoffeeScript validation code into a Ruby object:
  • 24.
    A small tasteof 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 youbecome a CoffeeScript ninja? Official site, great overview: http://coffeescript.org Nice screencast: http://peepcode.com/products/coffeescript Totally unbiased book recommendation: http://coffeescript-book.com
  • 26.
    Thanks Questions? trevorburnham@gmail.com