CoffeeScript Backbone Model Bug
by erikcw
HTML
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.0.0/backbone.js"></script>
<div id="test">Loading...</div>
CSS
#test {
width: 90%;
height: 100px;
background: #ffb;
padding: 10px;
border: 2px solid #999;
}
CoffeeScript
class MyModel extends Backbone.Model
defaults:
bug: true
name: null
class MyCollection extends Backbone.Collection
model: MyModel
class MyView extends Backbone.View
el: "#test"
initialize: (options) ->
@collection.on("add", @handleAdd)
handleAdd: (model, collection, options) =>
console.log(model)
@render()
render: ->
console.log("render")
@$el.html("Test")
for model in @collection.models
@$el.append("<li>model: #{model?.cid} - #{model.get('name')}</li>")
startApp = ->
my_collection = new MyCollection([])
view = new MyView({
collection: my_collection
})
view.render()
model = new MyModel({name: "erikcw"})
my_collection.add(model)
model = new MyModel({name: "model 2"})
console.assert(model instanceof Backbone.Model, "problem with the prototype chain")
my_collection.add(model)
$(document).ready(->
console.log('document ready')
startApp()
)