13
The Billy D. Williams of Client Side Scripting Jeremy Woertink

Intro to CoffeeScript

Embed Size (px)

DESCRIPTION

This is

Citation preview

Page 1: Intro to CoffeeScript

The Billy D. Williams of Client Side Scripting

Jeremy Woertink

Page 2: Intro to CoffeeScript

The cool side of JavaScript

•1 to 1 with JavaScript•Cleaner Syntax•OOP style coding•Rubyish!•Built into Rails 3.1

Page 3: Intro to CoffeeScript

Venti mocha latte, please!

note: I have no idea what that actualy is

Page 4: Intro to CoffeeScript

JavaScript

var i, bean, beans, count; bean = 'columbian'; beans = []; beans.push(bean); count = beans.length; if(typeof bean !== 'undefined' && bean !== null) { alert('We have a '+bean+' bean'); } for(i = 0;i < count; i++) { alert(beans[i]); }

Page 5: Intro to CoffeeScript

CoffeeScript

bean = 'columbian' beans = [] beans.push bean count = beans.length # not needed alert "We have a #{bean} bean" if bean? alert bean for bean in beans

Page 6: Intro to CoffeeScript

Grande Chai Code Light

Installing:1.Install NodeJS2.http://nodejs.org/3.Install NPM4.http://npmjs.org/5.npm install coffee-script6.gem "barista"7.https://github.com/Sutto/barista

Page 7: Intro to CoffeeScript

Brewing an App

Rails 3 with Barista1.rails g barista:install2.Rails.root.join("app", "assets", "javascripts")3.app/assets/javascripts/application.coffee4.GO!

Page 8: Intro to CoffeeScript

Tell me more about Coffee

Page 9: Intro to CoffeeScript

Coffee Extras

Cool things about the CoffeeScript•Default argument values•no more braces•no more semi colons•a lot less parenthesis•array splats•english operators (and, is, isnt, until...)•existence operators (?, ?.)•class definitions•embedded oldschool JS•Interactive CLI (coffee to get in, quit() to exit)

Page 10: Intro to CoffeeScript

What about decaf?

Page 11: Intro to CoffeeScript

jQuery

$(function() { $('#link').click(function() { $(this.hash).show('fast', function() { //this fails. the scope of "this" is <div id="container"> $(this.hash).text("Here is a " + this.title); }); }); });

/* HTML <a id="link" href="#container" title="Big Box">Click</a>*/

Page 12: Intro to CoffeeScript

jQuery flavored CoffeeScript

$ -> $('#link').click -> $(@hash).show 'fast', => $(@hash).text "Here is a #{@title}"

/* HTML <a id="link" href="#container" title="Big Box">Click</a>*/

Page 13: Intro to CoffeeScript

Ready to pour a cup?