博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
关于backbone中的view
阅读量:6328 次
发布时间:2019-06-22

本文共 5896 字,大约阅读时间需要 19 分钟。

在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的相关代码,自己写了点注释,不好请指正!

转载于:https://www.cnblogs.com/htmlwall/p/3711358.html

你可能感兴趣的文章
爪哇国新游记之十二----线程创建的两种形式
查看>>
64. Minimum Path Sum
查看>>
简单JNI使用demo
查看>>
Windows Live Writer 使用指南
查看>>
分析iOS Crash文件,使用命令符号化iOS Crash文件
查看>>
Android studio 如何查看模拟器里面的文件
查看>>
Java编译命令整理
查看>>
Java数据结构——链表-单链表
查看>>
mesos
查看>>
Sun Grid Engine (SGE)大型集群作业调度系统
查看>>
信号处理——生成给定分布随机数
查看>>
2014年上半年软件设计师考试之绝密答案--有待大家完好
查看>>
Java动态代理学习【Spring AOP基础之一】
查看>>
在cmd窗口输入命令遇到You must run this command from a command prompt with administrator privilege怎么办?...
查看>>
ElasticSearch入门 第五篇:使用C#查询文档
查看>>
设置数据库状态
查看>>
Android之读取 AndroidManifest.xml 中的数据:版本号、应用名称、自定义K-V数据(meta-data)...
查看>>
获取指定的内容---MXCMS ReadNews标签说明
查看>>
SPRING源码分析:IOC容器
查看>>
linux系统性能分析
查看>>