<script src="https://rawgit.com/jashkenas/underscore/1.5.2/underscore-min.js"></script>
<script src="https://rawgit.com/jashkenas/backbone/1.0.0/backbone-min.js"></script>
Backbone collections are simply an ordered set of models. Such that it can be used in situations such as: Model: Student, Collection: ClassStudents Model: Animal, Collection: Zoo
<hr>Typically your collection will only use one type of model but models themselves are not limited to a type of collection: Model: Student, Collection: Gym Class Model: Student, Collection: Art Class Model: Student, Collection: English Class
JavaScript
// backbone.js 7 collections
var alert = function (str) {
var st = document.createTextNode(str);
var p = document.createElement('p');
p.appendChild(st);
document.querySelector('body').appendChild(p);
}
var Song = Backbone.Model.extend({
defaults: {
name: "Not specified",
artist: "Not specified"
},
initialize: function () {
alert("Music is the answer");
}
});
var Album = Backbone.Collection.extend({
model: Song
});
var song1 = new Song({name: "How Bizarre",artist: "OMC"});
var song2 = new Song({name: "Sexual Healing",artist: "Marvin Gaye"});
var song3 = new Song({name: "Talk It Over In Bed",artist: "OMC"});
var myAlbum = new Album([song1, song2, song3]);
alert(JSON.stringify(myAlbum.models))
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.