Vue.js源码(2):初探List Rendering

开发 前端
本文先用几张图片回顾和整理下上一篇Vue.js源码(1):Hello World的背后的内容,这将对本篇的compile,link和bind过程的理解有帮助。这篇文章将要一起分析:observe array、terminal directive、v-for指令过程。

[[171811]]

下面例子来自官网,虽然看上去就比Hello World多了一个v-for,但是内部多了好多的处理过程。但是这就是框架,只给你留下最美妙的东西,让生活变得简单。

  1. <div id="mountNode"
  2.     <ul> 
  3.         <li v-for="todo in todos"
  4.           {{ todo.text }} 
  5.         </li> 
  6.     </ul> 
  7. </div>  
  1. var vm = new Vue({ 
  2.     el: '#mountNode'
  3.         data: { 
  4.            todos: [ 
  5.                { text: 'Learn JavaScript' }, 
  6.                { text: 'Learn Vue.js' }, 
  7.                { text: 'Build Something Awesome' } 
  8.            ] 
  9.         } 
  10.     })  

这篇文章将要一起分析:

  • observe array
  • terminal directive
  • v-for指令过程

recap

这里先用几张图片回顾和整理下上一篇Vue.js源码(1):Hello World的背后的内容,这将对本篇的compile,link和bind过程的理解有帮助:

copmile阶段:主要是得到指令的descriptor 

 

link阶段:实例化指令,替换DOM

 bind阶段:调用指令的bind函数,创建watcher

 用一张图表示即为:

 observe array

初始化中的merge options,proxy过程和Hello World的过程基本一样,所以这里直接从observe开始分析。

  1. // file path: src/observer/index.js 
  2. var ob = new Observer(value) // value = data = {todos: [{message: 'Learn JavaScript'}, ...]}  
  1. // file path: src/observer/index.js 
  2. export function Observer (value) { 
  3.   this.value = value 
  4.   this.dep = new Dep() 
  5.   def(value, '__ob__', this) 
  6.   if (isArray(value)) {     // 数组分支 
  7.     var augment = hasProto 
  8.       ? protoAugment 
  9.       : copyAugment         // 选择增强方法 
  10.     augment(value, arrayMethods, arrayKeys)     // 增强数组 
  11.     this.observeArray(value) 
  12.   } else {                  // plain object分支 
  13.     this.walk(value) 
  14.   } 
  15.  

增强数组

增强(augment)数组,即对数组进行扩展,使其能detect change。这里面有两个内容,一个是拦截数组的mutation methods(导致数组本身发生变化的方法),一个是提供两个便利的方法$set和$remove。

拦截有两个方法,如果浏览器实现__proto__那么就使用protoAugment,否则就使用copyAugment。

  1. // file path: src/util/evn.js 
  2. export const hasProto = '__proto__' in {} 
  3.  
  4. // file path: src/observer/index.js 
  5. // 截取原型链 
  6. function protoAugment (target, src) { 
  7.   target.__proto__ = src 
  8.  
  9. // file path: src/observer/index.js 
  10. // 定义属性 
  11. function copyAugment (target, src, keys) { 
  12.   for (var i = 0, l = keys.length; i < l; i++) { 
  13.     var key = keys[i] 
  14.     def(target, key, src[key]) 
  15.   } 
  16.  

为了更直观,请看下面的示意图:

增强之前:

 通过原型链拦截: 

 通过定义属性拦截: 

 在拦截器arrayMethods里面,就是对这些mutation methods进行包装:

  1. 调用原生的Array.prototype中的方法
  2. 检查是否有新的值被插入(主要是push, unshift和splice方法)
  3. 如果有新值插入,observe它们
  4. ***就是notify change:调用observer的dep.notify()

代码如下: 

  1. // file path: src/observer/array.js 
  2. ;[ 
  3.   'push'
  4.   'pop'
  5.   'shift'
  6.   'unshift'
  7.   'splice'
  8.   'sort'
  9.   'reverse' 
  10. .forEach(function (method) { 
  11.   // cache original method 
  12.   var original = arrayProto[method] 
  13.   def(arrayMethods, method, function mutator () { 
  14.     // avoid leaking arguments: 
  15.     // http://jsperf.com/closure-with-arguments 
  16.     var i = arguments.length 
  17.     var args = new Array(i) 
  18.     while (i--) { 
  19.       args[i] = arguments[i] 
  20.     } 
  21.     var result = original.apply(this, args) 
  22.     var ob = this.__ob__ 
  23.     var inserted 
  24.     switch (method) { 
  25.       case 'push'
  26.         inserted = args 
  27.         break 
  28.       case 'unshift'
  29.         inserted = args 
  30.         break 
  31.       case 'splice'
  32.         inserted = args.slice(2) 
  33.         break 
  34.     } 
  35.     if (inserted) ob.observeArray(inserted) 
  36.     // notify change 
  37.     ob.dep.notify() 
  38.     return result 
  39.   }) 
  40. })  

observeArray()

知道上一篇的observe(),这里的observeArray()就很简单了,即对数组对象都observe一遍,为各自对象生成Observer实例。 

  1. // file path: src/observer/index.js 
  2. Observer.prototype.observeArray = function (items) { 
  3.   for (var i = 0, l = items.length; i < l; i++) { 
  4.     observe(items[i]) 
  5.   } 
  6.  

compile

在介绍v-for的compile之前,有必要回顾一下compile过程:compile是一个递归遍历DOM tree的过程,这个过程对每个node进行指令类型,指令参数,表达式,过滤器等的解析。

递归过程大致如下:

  1. compile当前node
  2. 如果当前node没有terminal directive,则遍历child node,分别对其compile node
  3. 如果当前node有terminal directive,则跳过其child node

这里有个terminal directive的概念,这个概念在Element Directive中提到过:

A big difference from normal directives is that element directives are terminal, which means once Vue encounters an element directive, it will completely skip that element

实际上自带的directive中也有两个terminal的directive,v-for和v-if(v-else)。

terminal directive

在源码中找到:

terminal directive will have a terminal link function, which build a node link function for a terminal directive. A terminal link function terminates the current compilation recursion and handles compilation of the subtree in the directive.

也就是上面递归过程中描述的,有terminal directive的node在compile时,会跳过其child node的compile过程。而这些child node将由这个directive单独compile(partial compile)。

以图为例,红色节点有terminal directive,compile时(绿线)将其子节点跳过: 

 为什么是v-for和v-if?因为它们会带来节点的增加或者删除。

Compile的中间产物是directive的descriptor,也可能会创建directive来管理的document fragment。这些产物是在link阶段时需要用来实例化directive的。从racap中的图可以清楚的看到,compile过程产出了和link过程怎么使用的它们。那么现在看看v-for的情况:

compile之后,只得到了v-for的descriptor,link时将用它实例化v-for指令。 

  1. descriptor = { 
  2.     name'for'
  3.     attrName: 'v-for'
  4.     expression: 'todo in todos'
  5.     raw: 'todo in todos'
  6.     def: vForDefinition 
  7.  

link

Hello World中,link会实例化指令,并将其与compile阶段创建好的fragment(TextNode)进行绑定。但是本文例子中,可以看到compile过程没有创建fragment。这里的link过程只实例化指令,其他过程将发生在v-for指令内部。

bind

主要的list rendering的魔法都在v-for里面,这里有FragmentFactory,partial compile还有diff算法(diff算法会在单独的文章介绍)。

在v-for的bind()里面,做了三件事:

  1. 重新赋值expression,找出alias:"todo in todos"里面,todo是alias,todos才是真正的需要监听的表达式
  2. 移除<li v-for="todo in todos">{{todo.text}}</li>元素,替换上start和end锚点(anchor)。锚点用来帮助插入最终的li节点
  3. 创建FragmentFactory:factory会compile被移除的li节点,得到并缓存linker,后面会用linker创建Fragment 
  1. // file path: /src/directives/public/for.js 
  2. bind () { 
  3.     // 找出alias,赋值expression = "todos" 
  4.     var inMatch = this.expression.match(/(.*) (?:in|of) (.*)/) 
  5.     if (inMatch) { 
  6.       var itMatch = inMatch[1].match(/\((.*),(.*)\)/) 
  7.       if (itMatch) { 
  8.         this.iterator = itMatch[1].trim() 
  9.         this.alias = itMatch[2].trim() 
  10.       } else { 
  11.         this.alias = inMatch[1].trim() 
  12.       } 
  13.       this.expression = inMatch[2] 
  14.     } 
  15.      
  16.     ... 
  17.      
  18.     // 创建锚点,移除LI元素 
  19.     this.start = createAnchor('v-for-start'
  20.     this.end = createAnchor('v-for-end'
  21.     replace(this.el, this.end
  22.     before(this.start, this.end
  23.  
  24.     ... 
  25.  
  26.     // 创建FragmentFactory 
  27.     this.factory = new FragmentFactory(this.vm, this.el) 
  28.   } 

 

Fragment & FragmentFactory

这里的Fragment,指的不是DocumentFragment,而是Vue内部实现的一个类,源码注释解释为:

Abstraction for a partially-compiled fragment. Can optionally compile content with a child scope.

FragmentFactory会compile<li>{{todo.text}}</li>,并保存返回的linker。在v-for中,数组发生变化时,将创建scope,克隆template,即<li>{{todo.text}}</li>,使用linker,实例化Fragment,然后挂在end锚点上。

在Fragment中调用linker时,就是link和bind<li>{{todo.text}}</li>,和Hello World中一样,创建v-text实例,创建watcher。 

scope

为什么在v-for指令里面可以通过别名(alias)todo访问循环变量?为什么有$index和$key这样的特殊变量?因为使用了child scope。

还记得Hello World中watcher是怎么识别simplePath的吗? 

  1. var getter = new Function('scope''return scope.message;'

在这里,说白了就是访问scope对象的todo,$index或者$key属性。在v-for指令里,会扩展其父作用域,本例中父作用域对象就是vm本身。在调用factory创建每一个fragment时,都会以下面方式创建合适的child scope给其使用: 

  1. // file path: /src/directives/public/for.js 
  2. create (value, alias, indexkey) { 
  3.     // index是遍历数组时的下标 
  4.     // value是对应下标的数组元素 
  5.     // alias = 'todo' 
  6.     // key是遍历对象时的属性名称 
  7.     ... 
  8.     var parentScope = this._scope || this.vm 
  9.     var scope = Object.create(parentScope) // 以parent scope为原型链创建child scope 
  10.     ... 
  11.     withoutConversion(() => { 
  12.       defineReactive(scope, alias, value) // 添加alias到child scope 
  13.     }) 
  14.     defineReactive(scope, '$index'index) // 添加$index到child scope 
  15.     ... 
  16.     var frag = this.factory.create(host, scope, this._frag) 
  17.     ... 
  18.   }  

detect change

到这里,基本上“初探”了一下List Rendering的过程,里面有很多概念没有深入,打算放在后面结合其他使用这些概念的地方一起在分析,应该能体会到其巧妙的设计。

***举两个例子,回顾上面的内容

例一: 

  1. vm.todos[0].text = 'Learn JAVASCRIPT'

改变的是数组元素中text属性,由于factory创建的fragment的v-text指令observe todo.text,因此这里直接由v-text指令更新对应li元素的TextNode内容。

例二: 

  1. vm.todos.push({text: 'Learn Vue Source Code'}); 

增加了数组元素,v-for指令的watcher通知其做update,diff算法判断新增了一个元素,于是创建scope,factory克隆template,创建新的fragment,append在#end-anchor的前面,fragment中的v-text指令observe新增元素的text属性,将值更新到TextNode上。

更多数组操作放在diff算法中再看。

到这里,应该对官网上的这句话有更深的理解了:

Instead of a Virtual DOM, Vue.js uses the actual DOM as the template and keeps references to actual nodes for data bindings.

责任编辑:庞桂玉 来源: segmentfault
相关推荐

2016-09-21 12:56:31

JavascriptWeb前端

2018-04-04 10:32:13

前端JavascriptVue.js

2017-07-04 17:55:37

Vue.js插件开发

2016-11-04 19:58:39

vue.js

2017-07-11 18:00:21

vue.js数据组件

2017-07-20 11:18:22

Vue.jsMVVMMVC

2022-01-19 22:18:56

Vue.jsVue SPA开发

2021-04-15 08:15:27

Vue.js源码方法

2016-11-01 19:10:33

vue.js前端前端框架

2017-07-14 10:10:08

Vue.jsMixin

2021-01-22 11:47:27

Vue.js响应式代码

2020-09-07 14:40:20

Vue.js构建工具前端

2020-09-16 06:12:30

Vue.js 3.0Suspense组件前端

2017-08-30 17:10:43

前端JavascriptVue.js

2023-03-16 14:29:48

Vue.js测试

2024-01-18 11:50:28

2019-07-26 14:40:58

Vue.jsSocket.IO前端

2021-07-30 05:06:48

Vue 2Vue 3

2022-04-04 16:53:56

Vue.js设计框架

2020-06-01 14:02:25

Vue.js框架模板
点赞
收藏

51CTO技术栈公众号