Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Dec 17th, 2012  |  syntax: None  |  size: 1.36 KB  |  views: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. var Track = Backbone.Model.extend({
  2.  
  3.     defaults : {
  4.       title : ""
  5.     }
  6. })
  7.  
  8. var TrackCollection = Backbone.Collection.extend({
  9.  
  10.     model : Track,
  11. })
  12.  
  13. var Playlist = Backbone.Model.extend({
  14.  
  15.     defaults : {
  16.         name : "",
  17.         tracks : new TrackCollection,
  18.     }
  19. })
  20.  
  21. var PlaylistCollection = Backbone.Collection.extend({
  22.  
  23.     model : Playlist,
  24. })
  25.        
  26. var playlists = new PlaylistCollection;
  27.  
  28. // create and push the first playlist
  29. playlists.push({ name : "classic" });
  30. // create and push a track in the playlist just created
  31. playlists.last().get("tracks").push({ title : "fur elise" });
  32.  
  33. // create and push the second playlist
  34. playlists.push({ name : "c2c" });
  35. // create and push a track in the playlist just created
  36. playlists.last().get("tracks").push({ title : "fuya" });
  37.  
  38. // display first playlist
  39. console.log(JSON.stringify(playlists.at(0).toJSON()))
  40. // display second playlist
  41. console.log(JSON.stringify(playlists.at(1).toJSON()))
  42.        
  43. {"name":"classic","tracks":[{"title":"fur elise"},{"title":"fuya"}]}
  44. {"name":"c2c","tracks":[{"title":"fur elise"},{"title":"fuya"}]}
  45.        
  46. var Playlist = Backbone.Model.extend({
  47.     defaults : {
  48.         name : "",
  49.         tracks : new TrackCollection,
  50.     }
  51. });
  52.        
  53. var Playlist = Backbone.Model.extend({
  54.     defaults : function() {
  55.         return {
  56.             name : "",
  57.             tracks : new TrackCollection,
  58.         };
  59.     }
  60. });