Created
October 22, 2010 14:01
-
-
Save kyleslattery/640581 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For this, I have a bunch of playlists that contain videos | |
playlists = [ | |
{ | |
id: 1 | |
name: "Some playlist", | |
videos: [ | |
{ | |
id: 1, | |
name: "Some video" | |
}, | |
{ | |
id: 2, | |
name: "Another video" | |
} | |
] | |
} | |
// And so on | |
]; | |
// My Playlist model checks if "videos" is set when initialized. If so, it sets | |
// this.videos | |
var Playlist = Backbone.Model.extend({ | |
initialize: function() { | |
this.videos = new PlaylistVideoStore(this.unset('videos')); | |
this.videos.playlist = this; | |
} | |
}); | |
// Video can reference its playlist by using this.playlist(); | |
var Video = Backbone.Model.extend({ | |
playlist: function() { | |
return this.collection.playlist; | |
} | |
}); | |
var PlaylistVideoStore = Backbone.Collection.extend({ | |
model: Video, | |
url: function() { | |
return this.playlist.url() + '/videos'; | |
} | |
}); | |
var PlaylistStore = Backbone.Collection.extend({ | |
model: Playlist, | |
url : '/playlists' | |
}); | |
// Now, to load in the initial data, I can use the refresh function like normal | |
var playlists = PlaylistVideoStore.refresh(playlists); | |
var playlist = playlists[0]; | |
// Since videos are a collection, I can refresh and fetch them like any other collection | |
playlist.videos.fetch(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment