← all posts

Backbone.js & Inheritance

Monday, April 15, 2013

Backbone's inheritance model is actually quite simple; it's basically Crockford's Object.create shim with the ability to specify your own constructor and static "class" properties. It's important to understand that when you're defining a new view, model, or collection with extend you're actually defining the prototype for the newly created object, so when you start to create instances of that object they're all going to share those properties. And if one of those properties holds a non-primitive value like an array, and you push onto the array, every other instance is going to immediately inherit those changes.

This probably isn't what you want to happen.

Example

I'm going to use Backbone.View from here on out for example's sake, but the same applies to models and collections. Given this code:

var BaseView = Backbone.View.extend({
  // Create a property where we can hold references to subviews
  subviews: {}
});

var ContentView = BaseView.extend({
  initialize: function() {
    this.subviews.sidebar = new SidebarView();
  }
});

var HeaderView = BaseView.extend({
  initialize: function() {
    this.subviews.menu = new MenuView();
  }
});

var content = new ContentView();
var header = new HeaderView();

Both content and header instances will have the exact same this.subviews property with two of its own properties, menu and sidebar:

This happens because ContentView and HeaderView do not have their own subviews property; it is defined on BaseView.prototype which both subviews inherit from. Therefore, lookups for this.views on any instance of ContentView or HeaderView, or within these views themselves, go back up to the same shared object. You can verify this by logging out the value of BaseView.prototype.subviews and observing that it too has sidebar and menu views.

Solution: override the constructor

The solution is to prevent lookups from going all the way up to BaseView.prototype.subviews by providing a subviews property in each subclass of BaseView. HeaderView and ContentView need to have their own subviews which is not shared and thus stops the lookup from reaching BaseView. Not to say that you can't or shouldn't access the subviews property defined on BaseView.protoype if you need it, you just don't want your subviews to share this property by default.

One approach to this end, and my favorite, is to create a custom constructor method for BaseView and initialize this.views as a property on the subclasses prototype. Applying this to the previous example we get:

var BaseView = Backbone.View.extend({
  constructor: function() {
    // Define the subviews object off of the prototype chain
    this.subviews = {};

    // Call the original constructor
    Backbone.View.apply(this, arguments);
  }
});

// Notice that we haven't changed any code for ContentView or HeaderView

var ContentView = BaseView.extend({
  initialize: function() {
    this.subviews.sidebar = new SidebarView()
  }
});

var HeaderView = BaseView.extend({
  initialize: function() {
    this.subviews.menu = new MenuView();
  }
});

var content = new ContentView();
var header = new HeaderView();

And now if you inspect the subviews property of content and header you'll see that they're different:

A couple things are going on in the constructor function that are worth mentioning:

  1. If you don't provide a custom constructor method then Backbone defaults to Backbone.View. There is no default method named constructor.

  2. In this example it is important that we call Backbone.View after initializing the this.subviews property because part of Backbone.View's responsibility is to call the initialize method. If you do this out of order then this.subviews will not be defined in initialize, which is where we're using it.

Alternative Solutions

Another solution is to simply give each subclass their own subviews property:

var BaseView = Backbone.View.extend();

var ContentView = BaseView.extend({
  subviews: {},

  initialize: function() {
    this.subviews.sidebar = new SidebarView()
  }
});

var HeaderView = BaseView.extend({
  subviews: {},

  initialize: function() {
    this.subviews.menu = new MenuView();
  }
});

Or a variant of this, set this.subviews inside initialize and call super:

var BaseView = Backbone.View.extend({
  initialize: function() {
    this.views = {};
  }
});

var ContentView = BaseView.extend({
  initialize: function() {
    BaseView.prototype.initialize.call(this);
    this.subviews.sidebar = new SidebarView()
  }
});

var HeaderView = BaseView.extend({
  initialize: function() {
    BaseView.prototype.initialize.call(this);
    this.subviews.menu = new MenuView();
  }
});

Both feel like a maintenance nightmare to me because a) you have to remember to give each subclass the property, b) the code is no longer isolated to one spot, and c) you're missing out on the beauty of inheritance.

comments powered by Disqus