I am trying to write a Backbone view for an object browser that is designed to be implemented in several places with different types of objects and a slightly different job. How can I make good use of the code.
I tried just expanding the spine in my browser and then expanding the browser in my implementation, but this leaves me with some properties that are common. This is an undesirable effect because data is added to all implementations every time you create a browser.
Can someone shed light on a way to solve this problem, or perhaps an alternative solution?
Here are some sample code to help you better understand how it currently stands:
var BrowserView = Backbone.View; _.extend(BrowserView.prototype, Backbone.View.prototype, { className: 'browser', collections: [], events: { }, _events:{ }, initialize: function () { this._initialize(); }, render: function () { this._render(); }, _initialize: function () { this.container = $( this.make('div', {class: 'container'} ) ); this.$el.append(this.container); if ( this.options.collections ) { this.collections = []; _.each(this.options.collections, this.add, this); } _.extend(this.events, this._events); this.delegateEvents(); }, _render: function () { this.container.empty(); _.each(this.collections, function (view) { this.container.append(view.el); view.render(); }, this); } }); BrowserView.extend = Backbone.View.extend; var ContactBrowserView = BrowserView.extend({ });
As always, I more than welcome feedback on any aspect of the code snippet I have provided.
Thanks Alex
EDIT My problem is that the subclass shares the collections property. The following is an example of my own solution that initializes the collection property through an inherited method. jsfiddle.net/JhZXh/3
source share