Skip to content

Commit

Permalink
Adding one line to Backbone.js to provide seamless CoffeeScript integ…
Browse files Browse the repository at this point in the history
…ration (inheritance from Backbone.Model, View, Collection) + tests
  • Loading branch information
jashkenas committed Oct 27, 2010
1 parent 4c5b74c commit 1d57168
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ end
desc "run JavaScriptLint on the source"
task :lint do
system "jsl -nofilelisting -nologo -conf docs/jsl.conf -process backbone.js"
end

desc "test the CoffeeScript integration"
task :test do
system "coffee test/*.coffee"
end
1 change: 1 addition & 0 deletions backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,7 @@
_.extend(child.prototype, protoProps);
if (classProps) _.extend(child, classProps);
child.prototype.constructor = child;
child.__super__ = parent.prototype;
return child;
};

Expand Down
43 changes: 43 additions & 0 deletions test/model.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Quick Backbone/CoffeeScript tests to make sure that inheritance
# works correctly.

{ok, equal, deepEqual} = require 'assert'
{Model, Collection, Events} = require '../backbone'


# Patch `ok` to store a count of passed tests...
count = 0
oldOk = ok
ok = ->
oldOk arguments...
count++


class Document extends Model

fullName: ->
@get('name') + ' ' + @get('surname')

tempest = new Document
id : '1-the-tempest',
title : "The Tempest",
name : "William"
surname : "Shakespeare"
length : 123

ok tempest.fullName() is "William Shakespeare"
ok tempest.get('length') is 123


class ProperDocument extends Document

fullName: ->
"Mr. " + super

properTempest = new ProperDocument tempest.attributes

ok properTempest.fullName() is "Mr. William Shakespeare"
ok properTempest.get('length') is 123


console.log "passed #{count} tests"

2 comments on commit 1d57168

@mrjjwright
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just wondering if I could use Coffee classes to declare Backbone objects yesterday. Nice!

@PlasticLizard
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

Please sign in to comment.