Backbone 101: Collections
by kyllle
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-alpha1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.2.2/backbone-min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/3.0.3/normalize.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone-localstorage.js/1.1.16/backbone.localStorage-min.js"></script>
<h2 class="pg-heading">Creating a collection</h2>
CSS
@import url(http://fonts.googleapis.com/css?family=Roboto:300,400,500);
* {
-webkit-font-smoothing: antialiased;
}
body {
font-family: Roboto;
padding: 16px;
line-height: 1.5;
font-weight: normal;
color: #ccc;
background: #292929;
font-size: 18px;
}
h1, h2, h3 {
font-weight: normal;
}
h3 ~ p {
margin-top: 0;
}
p {
opacity: 0.86;
}
.pg-heading {
margin-bottom: 0;
}
a {
color: #95baf6;
transition: opacity .3s;
}
a:hover {
opacity: 0.75;
}
pre {
display: block;
padding: 9.5px;
margin: 0 0 10px;
font-size: 13px;
line-height: 1.428571429;
color: #333;
word-break: break-all;
word-wrap: break-word;
background-color: #f5f5f5;
/* border: 1px solid #ccc; */
border-radius: 4px;
}
pre.app {
opacity: 0.35;
transition: opacity .3s;
position: relative;
}
pre.app:after {
content: 'App Example';
padding: 3px 8px;
background: #95baf6;
position: absolute;
top: 0;
right: 0;
border-radius: 0 4px 0 0;
}
pre.app:hover {
opacity: 1;
}
JavaScript
console.clear();
var data = [
{
id: 1,
title: 'Pulp Fiction'
}, {
id: 2,
title: 'The Usual Suspects'
}
]
var MovieModel = Backbone.Model.extend({
defaults: {
watchlist: false
}
});
// Creating a Backbone Collection
var MovieCollection = Backbone.Collection.extend({
model: MovieModel,
initialize: function() {
this.on('add', function(model, collection, options) {
console.info(model.toJSON(), 'was added to', collection.toJSON());
});
this.on('remove', function(model, collection, options) {
console.info(model.toJSON(), 'was removed from', collection.toJSON());
});
this.on('reset', function(collection, options) {
console.info('~ Reset called', collection, options); //contains a previousModels array
});
this.on('change', function(model, options) {
console.info('~ Model changed', model.toJSON());
});
this.on('change:watchlist', function(model, options) {
console.log('~ Model watchlist attribute changed', model.toJSON());
});
},
// Collection Method to return all models with watchlist: true
getWatchlistMovies: function() {
return this.where('watchlist', true);
},
// Collection Method to return number of models with watchlist: true
getWatchlistLength: function() {
return this.where({'watchlist': true}).length;
}
});
// Create some instances
var movie = new MovieModel;
var movies = new MovieCollection;
// Adding model data to our Collection
// 1) Passing data straight in on instantiation
// var movies = new MovieCollection(data);
// 2) Adding the data to the instance
movies.add([{
id: 1,
title: 'Pulp Fiction'
}, {
id: 2,
title: 'The Usual Suspects'
}]);
// 3) Reset the collection with new data
//...