Backbone 101: Models
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 model</h2>
<p>Backbone Models are the basic data object in the framework – frequently representing a row in a table in a database on your server. A discrete chunk of data and a bunch of useful, related methods for performing computations and transformations on that data.</p>
<a href="http://backbonejs.org/docs/backbone.html#section-51" target="_blank">Models - Annotated Source</a>
<br>
<a href="http://addyosmani.github.io/backbone-fundamentals/#models">Models - Addy Osmani Backbone Fundamentals</a>
<br><br>
<h3>Creating a Backbone model</h3>
<p>To create a Model class of your own, you extend Backbone.Model and provide instance properties, as well as optional classProperties to be attached directly to the constructor function.</p>
<pre>
var Movie = Backbone.Model.extend();
</pre>
<br>
<h3>Creating a Backbone model with default properties</h3>
<p>There are times when you want your model to have a set of default values (e.g., in a scenario where a complete set of data isn’t provided by the user). This can be set using a property called defaults in your model.</p>
<pre>
var Movie = Backbone.Model.extend({
defaults: {
watchlist: false,
rated: false
}
});
</pre>
<br>
<h3>Creating an instance of the model</h3>
<pre>
var movie = new Movie();
</pre>
<br>
<h3>Creating an instance of the model with attributes</h3>
<pre>
var movie = new Movie({
...
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();
// Creating a Backbone model
// & Do something when the model instance is created
var Movie = Backbone.Model.extend({
localStorage: new Store('movie'),
defaults: {
watchlist: false,
rated: false
},
initialize: function() {
console.log('New movie model created');
// Listening for model `change` events
this.on('change', function(model, options){
console.log('- Values for this model have changed.', model.toJSON(), options);
});
// Listening for model `change` event on the watchlist attribute
this.on('change:watchlist', function(model, options){
console.log('- Watchlist attribute value for this model has changed.', model.toJSON(), options);
});
this.on('invalid', function(model, error) {
console.log('- Validation failed for model', model, error);
});
// other events available `destroy`, `sync`, `all`
},
validate: function(attrs) {
var errors = [];
/*if(attrs.watchlist) {
errors.push({
name: 'watchlist',
message: 'Movie not in watchlist'
});
}*/
return errors.length > 0 ? errors : null;
}
});
// Creating an instance of the model with attributes
var movie = new Movie({
id: 1,
title: 'Pulp Fiction'
});
console.log('Movie model', movie);
// Setting model attributes
movie.set('watchlist', true);
// If you want to unset an attribute
movie.unset('watchlist');
// Check if model has a specific attribute
if(movie.has('title')) {
console.log('Movie has title', movie.get('title'));
}
// Get object hash of changed model properties since last set
console.log('Movie attributes that have changed', movie.changedAttributes());
console.log('Setting watchlist', movie);
console.log('Read/access a models attributes',...