I extend base backbone views all the time and have a base view per section so that I can extend on multiple levels. My question is, what’s the most effective way of doing view mixins: reusable view partials that can be mixed in to any view. For example:
var BaseProfile = Backbone.View.extend({ ...});
var UserProfile = BaseProfile.extend({ ...});
var VideoSupport = Backbone.View.extend({ ...});
What’s the best way to mixin VideoSupport view (an event object and a few methods) with UserProfile view?
The underscore.js library provides an
extendmethod that does what you want. You can define functionality on any object, and then quite literally copy & paste all of the methods and attributes from that object to another.Backbone’s
extendmethods on Views, Models, and Routers are a wrapper around underscore’sextend.