Models:
class Device extends Backbone.Model
url: '/device'
initialize: ->
console.log "Device model created..."
@set
controllers: new Controllers
class Controller extends Backbone.Model
Collection:
class Controllers extends Backbone.Collection
model: Controller
Code:
device = new Device
controller = new Controller
controller1 = new Controller
controller.set
name: "state1"
value: "on"
controller1.set
name: "state2"
value: "on"
device.set
id: 1
name: "foo"
controllers: [controller, controller1]
Error message (when using .each on “controllers”):
TypeError: Object [object Object],[object Object] has no method ‘each’
The idea is that each Device holds a sub collection of controllers. My simple question is how to add models to this sub collection? The code above seems to override the collection with an array…
You’re replacing the attribute
controllerswith anarray. Since its a collection, you need to use eitheraddorreset, depending on what you want to do.