9951 explained code solutions for 126 technologies


backbone.jsHow can I extend a Backbone.js view?


You can extend a Backbone.js view by creating a new view which inherits from the existing view.

var MyView = Backbone.View.extend({
  // ...
});

// Extend MyView
var MyExtendedView = MyView.extend({
  // ...
});

The MyExtendedView will inherit all of the properties and methods of MyView. You can then add additional properties and methods to MyExtendedView to customize it.

For example, you could add a custom initialize method to MyExtendedView:

var MyExtendedView = MyView.extend({
  initialize: function() {
    console.log('My extended view has been initialized');
  }
});

The output of this code would be:

My extended view has been initialized

You can also override existing properties and methods in MyExtendedView:

var MyExtendedView = MyView.extend({
  render: function() {
    console.log('My extended view has been rendered');
  }
});

The output of this code would be:

My extended view has been rendered

For more information on extending Backbone.js views, see the Backbone.js documentation.

Edit this code on GitHub