Mode.parse and Model.toJSON

In notebook:
FrontEndMasters BackboneJS
Created at:
libraries JavaScript
Talks about common response coming back as stringified JSON.

Shows a custom parse function:
  var BookModel = Backbone.Model.extend({
  parse: function(attrs){
This will automatically unwrap the data. If it comes in like this:
  "book" : {
    "title": "The Long Goodbye",
    "author": "Raymond Chandler"
You can do this:
// = "Raymond Chandler"

Nested resources

Nested data (like MongoDB)
Works with Backbone, but…

No granular change events on the second level of nesting. In this case he would use a ​set​ to add the nested keys to the model:
  "content": "lorem ipsum",
  "author": {
    "email": "",
    "username": "ussss"
    "avatar": "image.jpg"
  var CommentModel = Backbone.Model.extend({
  initialise: function () { = new UserModel();
  parse: function (attrs){
    if ( {;
    return _.omit(attrs, 'author');
So now, the “subdocument” author is also a full Backbone model with events, etc.

Talks about underscores ​.omit​ and ​delete​ in JavaScript. Underscore is more performant than what a normal user could write.


The reverse direction
You have to call ​toJSON​ on the sub-model
  var CommentModel = Backbone.Model.extend({
  toJSON: function(options){
    var json =;
    if (options &&{ =;
  return JSON;
to use, pass the ​​ as ​true​:
​comment.toJSON({ author: true });

Talks about how it relates to Backbone’s ​sync​ method.