未加星标

Backbone.js & Inheritance

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二04 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

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 :


Backbone.js & Inheritance

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:


Backbone.js & Inheritance

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

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

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.

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: Head
分页:12
转载请注明
本文标题:Backbone.js & Inheritance
本站链接:http://www.codesec.net/view/482736.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(26)