Skip to content

Commit 1d57168

Browse files
committed
Adding one line to Backbone.js to provide seamless CoffeeScript integration (inheritance from Backbone.Model, View, Collection) + tests
1 parent 4c5b74c commit 1d57168

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

Rakefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,9 @@ end
1818
desc "run JavaScriptLint on the source"
1919
task :lint do
2020
system "jsl -nofilelisting -nologo -conf docs/jsl.conf -process backbone.js"
21+
end
22+
23+
desc "test the CoffeeScript integration"
24+
task :test do
25+
system "coffee test/*.coffee"
2126
end

backbone.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,7 @@
695695
_.extend(child.prototype, protoProps);
696696
if (classProps) _.extend(child, classProps);
697697
child.prototype.constructor = child;
698+
child.__super__ = parent.prototype;
698699
return child;
699700
};
700701

test/model.coffee

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Quick Backbone/CoffeeScript tests to make sure that inheritance
2+
# works correctly.
3+
4+
{ok, equal, deepEqual} = require 'assert'
5+
{Model, Collection, Events} = require '../backbone'
6+
7+
8+
# Patch `ok` to store a count of passed tests...
9+
count = 0
10+
oldOk = ok
11+
ok = ->
12+
oldOk arguments...
13+
count++
14+
15+
16+
class Document extends Model
17+
18+
fullName: ->
19+
@get('name') + ' ' + @get('surname')
20+
21+
tempest = new Document
22+
id : '1-the-tempest',
23+
title : "The Tempest",
24+
name : "William"
25+
surname : "Shakespeare"
26+
length : 123
27+
28+
ok tempest.fullName() is "William Shakespeare"
29+
ok tempest.get('length') is 123
30+
31+
32+
class ProperDocument extends Document
33+
34+
fullName: ->
35+
"Mr. " + super
36+
37+
properTempest = new ProperDocument tempest.attributes
38+
39+
ok properTempest.fullName() is "Mr. William Shakespeare"
40+
ok properTempest.get('length') is 123
41+
42+
43+
console.log "passed #{count} tests"

0 commit comments

Comments
 (0)