If you don't know about CoffeeScript yet, you need to check it out. Now. -- I love it. It feels as fun and clean as writing in Ruby, only it compiles to JavaScript. That's what you would call "all good." If you were one to use such expressions.
Here's some coffee I created so I could use Backbone classes as native CoffeeScript classes:
This file contains 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
# Backbone CoffeeScript Helpers by M@ McCray. | |
# Source: http://gist.github.com/625893 | |
# | |
# Use Backbone classes as native CoffeeScript classes: | |
# | |
# class TaskController extends Events | |
# | |
# class TaskView extends View | |
# tagName: 'li' | |
# @SRC: '<div class="icon">!</div><div class="name"><%= name %></div>' | |
# | |
# constructor: -> | |
# super | |
# @template= _.template(TaskView.SRC) | |
# @render() if @model? | |
# | |
# render: -> | |
# $(@el).html @template( @model.toJSON() ) | |
# | |
# Etc... | |
# | |
class Events | |
_.extend(Events::, Backbone.Events) | |
this.Events = Events | |
class Model | |
constructor: -> | |
Backbone.Model.apply(this, arguments) | |
_.extend(Model::, Backbone.Model.prototype) | |
this.Model = Model | |
class Collection | |
constructor: -> | |
Backbone.Collection.apply(this, arguments) | |
_.extend(Collection::, Backbone.Collection.prototype) | |
this.Collection = Collection | |
class View | |
constructor: -> | |
Backbone.View.apply(this, arguments) | |
_.extend(View::, Backbone.View.prototype) | |
this.View = View |
Thanks for the shim! As of this commit, you should no longer need to do any special setup to use Backbone with CoffeeScript classes:
ReplyDeletehttp://github.com/documentcloud/backbone/commit/1d57168c8f436f1f9b20a5fa7f13495610931b22
Cheers,
Jeremy