Note that there are some explanatory texts on larger screens.

plurals
  1. POBackbone.js Collection of Collections
    text
    copied!<p>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:</p> <pre><code> +---------------+ +------------------+ | Playlists | | Playlist | |---------------| 0..* |------------------| | +--------------&gt;| Name | | | | | | | | | +---------------+ +-------+----------+ | | |0..* v +------------------+ | Track | |------------------| | Name | | Artist | | | +------------------+ </code></pre> <p>In code this looks similar to this:</p> <pre><code>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 }); </code></pre> <p>However I always receive an error in the js console saying:</p> <pre><code> Uncaught TypeError: Object [object Object] has no method '_validate' </code></pre> <p>when I try to execute a function that triggers the validate (like add, fetch, ...)</p> <p>It makes no difference if i add the <code>validate</code> or <code>_validate</code> function to any of the collections or models.</p> <p>I believe this is because backbone.js doesn't support collections in collections. Is there another way that works?</p> <p><strong>UPDATE:</strong></p> <p>This is how it looks right now</p> <pre><code>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 }); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload