Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone.js Collection of Collections

I'm trying to figure out how to make a Collection of collections with backbone.js. I'm pretty new to backbone. I have something like the following situation:

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

In code this looks similar to 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 receive an error in the js console saying:

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

when I try to execute a function that triggers the validate (like add, fetch, ...)

It makes no difference if i add the validate or _validate function to any of the collections or models.

I believe this is because backbone.js doesn't support collections in collections. Is there another way that works?

UPDATE:

This is how it looks 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  }); 
like image 393
krial Avatar asked Apr 30 '12 17:04

krial


People also ask

What are collections in Backbone JS?

Collections are ordered sets of Models. We just need to extend the backbone's collection class to create our own collection. Any event that is triggered on a model in a collection will also be triggered on the collection directly.

Is Backbone JS still relevant?

Backbone. Backbone has been around for a long time, but it's still under steady and regular development. It's a good choice if you want a flexible JavaScript framework with a simple model for representing data and getting it into views.

How do you override the model property of the collection class backbone JS?

model() You have to override the model property of the collection class to specify the model class that the collection contains. You can pass raw attributes objects to add, create and reset and the attributes will be converted into a model of the proper type.


1 Answers

You'd solve your problem by turning your Playlist from a collection into a model. If you think about it, a Playlist would probably have other attributes anyway (e.g. name) that wouldn't be settable on a collection.

Playlists would then be a collection of Playlist models (instead of collections), which should work without error.

var Track = Backbone.Model.extend({     //trackdata });  var Playlist = Backbone.Model.extend({     model : Track });  var Playlists = Backbone.Collection.extend({     url   : "playlists",     model : Playlist }); 
like image 138
Rob Hruska Avatar answered Oct 10 '22 19:10

Rob Hruska