Created
October 25, 2011 06:46
-
-
Save matthieua/1311614 to your computer and use it in GitHub Desktop.
CoffeeScript Presentation Examples - Envato - 25/10/2011
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ::::::::: :::::::::: ::: ::: :::::::: | |
# :+: :+: :+: :+:+: :+:+: :+: :+: | |
# +:+ +:+ +:+ +:+ +:+:+ +:+ +:+ +:+ | |
# +#+ +:+ +#++:++# +#+ +:+ +#+ +#+ +:+ | |
# +#+ +#+ +#+ +#+ +#+ +#+ +#+ | |
# #+# #+# #+# #+# #+# #+# #+# | |
# ######### ########## ### ### ######## | |
# ::::::::::: ::::::::::: ::: ::: :::::::::: | |
# :+: :+: :+:+: :+:+: :+: | |
# +:+ +:+ +:+ +:+:+ +:+ +:+ | |
# +#+ +#+ +#+ +:+ +#+ +#++:++# | |
# +#+ +#+ +#+ +#+ +#+ | |
# #+# #+# #+# #+# #+# | |
# ### ########### ### ### ########## | |
############ BASIC EXAMPLES ############### | |
# if (typeof elvis !== "undefined" && elvis !== null) { | |
# alert("I knew it!"); | |
# } | |
alert "I knew it" if elvis? | |
# if (status !== 'fail') { | |
# console.log('success'); | |
# } | |
console.log 'success' unless status is 'fail' | |
# var list = [1, 2, 3, 4,5]; | |
list = [1...5] | |
# var square = function(num) { | |
# return num * num; | |
# }; | |
square = (num) -> num * num | |
# var add = function(num1, num2) { | |
# return num1 + " + " + num2 + " = " + (num1 + num2); | |
# }; | |
add = (num1, num2) -> | |
"#{num1} + #{num2} = #{num1 + num2}" | |
# var args_to_s = function(a, b) { | |
# return 'a, b'; | |
# } | |
args_to_s = (args...) -> | |
args.join ', ' | |
# var bestLanguage = function(language) { | |
# if (typeof language === 'undefined') { | |
# language = "CoffeeScript"; | |
# } | |
# alert ("the best language is " + language); | |
# }; | |
bestLanguage = (language = 'CoffeeScript') -> | |
alert "the best language is #{language}" | |
# var kids = { | |
# brother: { | |
# name: "Max", | |
# age: 11 | |
# } | |
# sister: { | |
# name: "Ida", | |
# age: 9 | |
# } | |
kids = | |
brother: | |
name: "Max" | |
age: 11 | |
sister: | |
name: "Ida" | |
age: 9 | |
console.log info.name for type, info of kids | |
# Row = (function() { | |
# function Row(index, name) { | |
# if (name == null) { | |
# name = 'row'; | |
# } | |
# this.index = index != null ? index : 'no index'; | |
# this.name = name; | |
# } | |
# Row.prototype.info = function() { | |
# return this.name + " clicked with an index of " + this.index + "."; | |
# }; | |
# Row.to_s = function(objects) { | |
# var o, txt, _i, _len; | |
# txt = ""; | |
# for (_i = 0, _len = objects.length; _i < _len; _i++) { | |
# o = objects[_i]; | |
# txt += "" + (o.info()) + " \n"; | |
# } | |
# return txt; | |
# }; | |
# })(); | |
# forget about inheritance | |
########### CLASSES ############### | |
class Row | |
constructor: (index, name = 'row') -> | |
@index = if index? then index else 'no index' | |
@name = name | |
info: -> | |
"#{@name} clicked with an index of #{@index}." | |
this.to_s = (objects) -> | |
txt = "" | |
txt += "#{o.info()} \n" for o in objects | |
txt | |
class Cell extends Row | |
constructor: (index, name = 'cell') -> | |
super index, name | |
$ -> | |
elements = [] | |
$('table').delegate 'td', 'click', -> | |
index = $(this).index() + 1 | |
td = new Cell index | |
elements.push td | |
alert td.info() | |
$('#tracker').click -> | |
alert Row.to_s elements | |
# HTML | |
<html> | |
<head> | |
<title>CoffeeSctipt Demo</title> | |
<style> | |
#container { width: 600px; margin: 20px auto; } | |
td { padding: 20px; background: #999; border: 5px solid #fff; color: #fff; } | |
td:hover { background: #444; cursor: pointer;} | |
</style> | |
</head> | |
<body> | |
<div id="container"> | |
<h1>CoffeeScript</h1> | |
<table> | |
<tbody> | |
<tr> | |
<td>data</td> | |
<td>data</td> | |
<td>data</td> | |
<td>data</td> | |
</tr> | |
<tr> | |
<td>data</td> | |
<td>data</td> | |
<td>data</td> | |
<td>data</td> | |
</tr> | |
<tr> | |
<td>data</td> | |
<td>data</td> | |
<td>data</td> | |
<td>data</td> | |
</tr> | |
<tr> | |
<td>data</td> | |
<td>data</td> | |
<td>data</td> | |
<td>data</td> | |
</tr> | |
</tbody> | |
</table> | |
<a href="#" title="indexes" id="tracker">tracker</a> | |
</div> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.js"></script> | |
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.1.7/underscore-min.js"></script> | |
<script src="/javascripts/coffeescripts/demo.js" ></script> | |
</body> | |
</html> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment