Extending Backbone.js constructors, the easy way

by Cedric Dugas on October 20, 2013

One thing that really is really annonying with backbone 1.1 is the fact that the options are not passed has this.options anymore in the Backbone.View.

Well simply enough we can add this (or anything else) in the view constructor.

// Compatibility override - Backbone 1.1 got rid of the 'options' binding
// automatically to views in the constructor - we need to keep that.
Backbone.View = (function(View) {
   return View.extend({
        constructor: function(options) {
            this.options = options;
            View.apply(this, arguments);
        }
    });
})(Backbone.View);

You can also augment the constructor with anything you need. This is useful, for example, if all views need to call some functions when initialized, or if you need to set some data on all views.

You could also use this pattern to augment any Backbone constructor, the possibilities are endless!