在backbone中定义的View类(见下面代码)非常简洁,给用户提供了无限扩展的可能!View主要做了几件事:
1、关联DOM 元素节点。View类为backbone MVC中的视觉层肯定会与DOM打交道!见构造函数中的_ensureElement方法,关联DOM有两种方式:如果实例化或扩展view类时设置了el属性就会关联页面上的DOM,如果没有,创建一个DOM元素默认为div!
2、事件代理。见方法 delegateEvents。这里的事件代理指的是事件不是直接注册在view对象上,而是对象属相$el上,如backbone依赖的是jQuery的$el为一个jQuery对象。
3、实例化VIew类时提供的一些便捷。见 var viewOptions = ['model', 'collection', 'el', 'id', 'attributes', 'className', 'tagName', 'events'];可以在实例化是设置这些属性,用于关联model、collection,events用于设置代理事件…… 这样就不通过扩展View类来做这些事!
4、还有混入backbone的Events
// Backbone.View // ------------- // Backbone Views are almost more convention than they are actual code. A View // is simply a JavaScript object that represents a logical chunk of UI in the // DOM. This might be a single item, an entire list, a sidebar or panel, or // even the surrounding frame which wraps your whole app. Defining a chunk of // UI as a **View** allows you to define your DOM events declaratively, without // having to worry about render order ... and makes it easy for the view to // react to specific changes in the state of your models. // Creating a Backbone.View creates its initial element outside of the DOM, // if an existing element is not provided... //backbone view 类 用于创建backbone view 对象 var View = Backbone.View = function(options) { this.cid = _.uniqueId('view');//创建cid options || (options = {}); _.extend(this, _.pick(options, viewOptions)); this._ensureElement(); this.initialize.apply(this, arguments); this.delegateEvents(); }; // Cached regex to split keys for `delegate`. var delegateEventSplitter = /^(\S+)\s*(.*)$/; // List of view options to be merged as properties. //初始化view时可以设置这些属性!!!!! var viewOptions = ['model', 'collection', 'el', 'id', 'attributes', 'className', 'tagName', 'events']; // Set up all inheritable **Backbone.View** properties and methods. _.extend(View.prototype, Events, { // The default `tagName` of a View's element is `"div"`. tagName: 'div', // jQuery delegate for element lookup, scoped to DOM elements within the // current view. This should be preferred to global lookups where possible. $: function(selector) { return this.$el.find(selector); }, // Initialize is an empty function by default. Override it with your own // initialization logic. initialize: function(){}, // **render** is the core function that your view should override, in order // to populate its element (`this.el`), with the appropriate HTML. The // convention is for **render** to always return `this`. render: function() { return this; }, // Remove this view by taking the element out of the DOM, and removing any // applicable Backbone.Events listeners. remove: function() { this.$el.remove(); this.stopListening(); return this; }, // Change the view's element (`this.el` property), including event // re-delegation. /** 设置view的el属性和$el属性!!! */ setElement: function(element, delegate) { if (this.$el) this.undelegateEvents(); this.$el = element instanceof Backbone.$ ? element : Backbone.$(element);//this.$el是jQuery对象,如果依赖的是jQuery的话! this.el = this.$el[0];//一个DOM元素节点 if (delegate !== false) this.delegateEvents(); return this; }, // Set callbacks, where `this.events` is a hash of // // *{"event selector": "callback"}* // // { // 'mousedown .title': 'edit', // 'click .button': 'save', // 'click .open': function(e) { ... } // } // // pairs. Callbacks will be bound to the view, with `this` set properly. // Uses event delegation for efficiency. // Omitting the selector binds the event to `this.el`. // This only works for delegate-able events: not `focus`, `blur`, and // not `change`, `submit`, and `reset` in Internet Explorer. delegateEvents: function(events) { //注册代理事件,所谓代理就是不是直接注册在view 对象上 而是注册在$el 中! if (!(events || (events = _.result(this, 'events')))) return this;//在扩展view类时可以定义event属性!!! this.undelegateEvents(); for (var key in events) { var method = events[key]; if (!_.isFunction(method)) method = this[events[key]];//如果method不是函数,获取对象中对应的方法! if (!method) continue;//跳出循环! var match = key.match(delegateEventSplitter); var eventName = match[1], selector = match[2]; method = _.bind(method, this);//将method 绑定到this 如:method=this.method; eventName += '.delegateEvents' + this.cid; if (selector === '') { this.$el.on(eventName, method); } else { this.$el.on(eventName, selector, method); } } return this; }, // Clears all callbacks previously bound to the view with `delegateEvents`. // You usually don't need to use this, but may wish to if you have multiple // Backbone views attached to the same DOM element. undelegateEvents: function() { //remove 代理的事件!!! this.$el.off('.delegateEvents' + this.cid); return this; }, // Ensure that the View has a DOM element to render into. // If `this.el` is a string, pass it through `$()`, take the first // matching element, and re-assign it to `el`. Otherwise, create // an element from the `id`, `className` and `tagName` properties. /*** 确保 有DOM 元素将view 渲染进里面 ***/ _ensureElement: function() { //设置或创建view的element if (!this.el) { var attrs = _.extend({}, _.result(this, 'attributes')); if (this.id) attrs.id = _.result(this, 'id'); if (this.className) attrs['class'] = _.result(this, 'className'); var $el = Backbone.$('<' + _.result(this, 'tagName') + '>').attr(attrs);//创建DOM元素,并设置元素的相关属性! this.setElement($el, false); } else { this.setElement(_.result(this, 'el'), false); } } });
以上为backbone View的相关代码,自己写了点注释,不好请指正!