Tips for using CoffeeScript with SproutCore
CoffeeScript is all the rage these days. SproutCore is one of the prominent client-side JS frameworks (and my favorite). Here’s how to use them together.
Create a SproutCore app
sc-init Todos --templateGet the sproutcore-coffeescript gem and add settings to your Buildfile.
begin gem "sproutcore-coffeescript", "~> 0.1.4" require "sproutcore-coffeescript" rescue LoadError puts "sproutcore-coffeescript not installed, please run:\n\n gem install sproutcore coffeescript\n\n" exit endRename todos.js to todos.coffee
Modify the SC boilerplate code to be CoffeeScript-compliant
Get hacking!
CoffeeScript syntax tips:
SproutCore’s computed property definitions require you to call .property on a function object. This is the best way I’ve figured out how to do this with CoffeeScript:
( -> contents of function ).property('dependent properties')
You just need to wrap the function declaration in parenthesis in order to call the property function on it. The parenthesis can span multiple lines too:
( ->
multiple()
lines()
ofCode()
).property('dependent properties')
Hope that helps!