jm2
jm2

Reputation: 763

Simple Backbone.JS in CoffeeScript Errors

I have a very simple Backbone.JS app developed, for learning CoffeeScript + Backbone.JS:

class Todo extends Backbone.Model 
    defaults:
        title: ''
        priority: 0
        done: false

class Todo extends Backbone.Collection
    model: Todo
    localStorage: new Backbone.LocalStorage("Todos")

t = new Todo({ title: 'todo 1' })
console.log t

But I am getting (looks very much like an infinite loop)

<error>
b.extend
f.extend.reset
g.Collection
Todo
f.extend._prepareModel
f.extend.add
f.extend.reset
g.Collection
Todo
f.extend._prepareModel
f.extend.add
f.extend.reset
g.Collection
Todo
f.extend._prepareModel
f.extend.add
f.extend.reset
g.Collection
Todo
f.extend._prepareModel
f.extend.add
f.extend.reset
g.Collection
Todo
f.extend._prepareModel
f.extend.add
f.extend.reset
g.Collection
Todo
f.extend._prepareModel
f.extend.add
f.extend.reset
g.Collection
Todo
f.extend._prepareModel
f.extend.add
f.extend.reset
g.Collection
Todo
f.extend._prepareModel
f.extend.add
f.extend.reset
g.Collection
Todo
f.extend._prepareModel
f.extend.add
f.extend.reset
g.Collection
Todo
f.extend._prepareModel
f.extend.add
f.extend.reset
g.Collection
Todo
f.extend._prepareModel
f.extend.add
f.extend.reset
g.Collection
Todo
f.extend._prepareModel
f.extend.add
f.extend.reset
g.Collection
Todo
f.extend._prepareModel
f.extend.add
f.extend.reset
g.Collection
Todo
f.extend._prepareModel
f.extend.add
f.extend.reset
g.Collection
Todo
f.extend._prepareModel
f.extend.add
f.extend.reset
g.Collection
Todo
f.extend._prepareModel
f.extend.add
f.extend.reset
g.Collection
Todo
f.extend._prepareModel
f.extend.add
f.extend.reset
g.Collection
Todo
f.extend._prepareModel
f.extend.add
f.extend.reset
g.Collection
Todo
f.extend._prepareModel
f.extend.add
f.extend.reset
g.Collection
Todo
f.extend._prepareModel
f.extend.add
f.extend.reset
g.Collection
Todo
f.extend._prepareModel
f.extend.add
f.extend.reset
g.Collection
Todo
f.extend._prepareModel
f.extend.add
f.extend.reset
g.Collection
Todo
f.extend._prepareModel
f.extend.add
f.extend.reset
g.Collection
Todo
f.extend._prepareModel
f.extend.add
f.extend.reset
g.Collection
Todo
f.extend._prepareModel
f.extend.add
f.extend.reset
g.Collection
Todo
f.extend._prepareModel
f.extend.add
f.extend.reset
g.Collection
Todo
f.extend._prepareModel
f.extend.add
f.extend.reset
g.Collection
Todo
f.extend._prepareModel
f.extend.add
f.extend.reset
g.Collection
Todo
f.extend._prepareModel
f.extend.add
f.extend.reset
g.Collection
Todo
f.extend._prepareModel
f.extend.add
f.extend.reset
g.Collection
Todo
f.extend._prepareModel
f.extend.add
f.extend.reset
g.Collection
Todo
f.extend._prepareModel
f.extend.add
f.extend.reset
g.Collection
Todo
f.extend._prepareModel
f.extend.add
f.extend.reset
g.Collection
Todo
f.extend._prepareModel
f.extend.add
f.extend.reset
g.Collection
Todo
f.extend._prepareModel
f.extend.add
f.extend.reset
g.Collection
Todo
f.extend._prepareModel
f.extend.add
f.extend.reset
g.Collection
Todo
f.extend._prepareModel
f.extend.add
f.extend.reset
g.Collection
Todo
f.extend._prepareModel
f.extend.add
f.extend.reset
g.Collection
Todo
f.extend._prepareModel
f.extend.add
f.extend.reset
g.Collection
Todo
f.extend._prepareModel

With normal JS I define class and inheritance like

Todo = Backbone.Model.extend({})

But in CoffeeScript its

class Todo extends Backbone.Model

are they the same? I dont think so, is this the cause of the problem?

Upvotes: 0

Views: 245

Answers (1)

mu is too short
mu is too short

Reputation: 434665

You have a typo in your collection, you want to call it Todos, not Todo:

class Todos extends Backbone.Collection
    model: Todo
    localStorage: new Backbone.LocalStorage("Todos")

If I do this:

class Todo extends Backbone.Model 
class Todo extends Backbone.Collection
    model: Todo

t = new Todo(title: 'todo 1')
console.log t​​​

I get a "Maximum call stack size exceeded." error: http://jsfiddle.net/ambiguous/FTCr2/

But if the collection is called Todos, things work: http://jsfiddle.net/ambiguous/RrA2D/

Your problem seems to be that your collection's model property is the collection itself so you end up with infinite recursion as the collection tries to create a model which is actually the collection ...

Upvotes: 2

Related Questions