Collection of Backbone.js collections

I am trying to figure out how to create a collection of collections with backbone.js. I'm new to the spine. I have something like the following situation:

+---------------+ +------------------+ | Playlists | | Playlist | |---------------| 0..* |------------------| | +-------------->| Name | | | | | | | | | +---------------+ +-------+----------+ | | |0..* v +------------------+ | Track | |------------------| | Name | | Artist | | | +------------------+ 

In code, it looks something like this:

 var trackModel = Backbone.Model.extend({ //trackdata }); var playlistModel = Backbone.Collection.extend({ model : trackModel, url : "playlist" }); var playlistsModel = Backbone.Collection.extend({ url : "playlists", model : playlistModel //This pretty sure doesn't work like I want, because there is no model attribute for collections :S }); 

However, I always get an error in the js console:

  Uncaught TypeError: Object [object Object] has no method '_validate' 

when I try to execute a function that starts validation (e.g. add, fetch, ...)

It doesn't matter if _validate add a validate or _validate to any of the collections or models.

I believe this is because backbone.js does not support collections in collections. Is there any other way that works?

UPDATE:

Here's what it looks like right now

 var Track = Backbone.Model.extend({ //trackdata }); var Tracks = Backbone.Collection.extend({ model:Track; }); var Playlist = Backbone.Model.extend({ //name : ... tracks: new Tracks () }); var Playlists = Backbone.Collection.extend({ url : "playlists", model : Playlist }); 
+47
javascript
Apr 30 '12 at 17:51
source share
1 answer

You will solve your problem by turning your Playlist from a collection into a model. If you think about it, the Playlist will probably have other attributes in any case (for example, name) that will not be set in the collection.

Playlists will then be a set of Playlist models (not collections) that should work without errors.

 var Track = Backbone.Model.extend({ //trackdata }); var Playlist = Backbone.Model.extend({ model : Track }); var Playlists = Backbone.Collection.extend({ url : "playlists", model : Playlist }); 
+29
Apr 30 '12 at 18:16
source share



All Articles