Compile 是什么?
在vue中主要作用就是解析模板,解析数据和一些相关的指令,绑定函数更新视图。
通过源码我们就可以看到一些相关的知识:
function Compile(el, mvvm) {
this.$vm = mvvm; //this Compile的实例 $vm 是MVVM的实例 (vm)
// el == "#app" 判断当前用户传递的el属性是元素节点还是选择器,如果是元素节点则直接保存到$el中通,
//如不是 则根据选择器 去查找对应的元素 然后保存
this.$el = this.isElementNode(el) ? el : document.querySelector(el);
//确定元素是否真正存在
if (this.$el) {//#app
this.$fragment = this.node2Fragment(this.$el);
this.$vm.$pick.beforeMounted && this.$vm.$pick.beforeMounted();
this.init();//初始化
this.$el.appendChild(this.$fragment);//添加文档碎片
this.$vm.$pick.mounted && this.$vm.$pick.mounted();
}
}
Compile.prototype = {
/**
* node to fragment 把节点转换成文档碎片
* @param el
* @returns {DocumentFragment}
*/
node2Fragment: function(el) {
var fragment = document.createDocumentFragment(),//文档碎片
child;
// 将原生节点拷贝到fragment
while (child = el.firstChild) {
fragment.appendChild(child);
}
return fragment;//返回一个文档碎片
},
init: function() {//初始化函数
//解析多个的元素节点
this.compileElement(this.$fragment);
},
compileElement: function(ele) {//解析元素
//初始化数据,保存所有子节点 保存this
var childNodes = ele.childNodes,
that = this;
/*
* 对所有子节点进行=>递归遍历
* [].slice.call() 将伪数组arguments转为数组
*/
[].slice.call(childNodes).forEach(function(node) {
var text = node.textContent;//text节点的文本内容
var reg = /\{\{(.*)\}\}/;//声明匹配大括号表达式的正则
//判断当前节点是不是元素节点
if (that.isElementNode(node)) {
//解析指令
that.compile(node);
//判断当前元素是否为文本节点 并且 文本节点中是否拥有{{xxx}}
} else if (that.isTextNode(node) && reg.test(text)) {
//解析文本(大括号表达式)并且赋值
that.compileText(node, Regexp.$1); //name
}
//如果当前节点还有子节点 那么就需要递归查找所有的子节点是否符合以上条件
if (node.childNodes && node.childNodes.length) {
that.compileElement(node);
}
});
},
//解析指令
compile: function(node) {
//获取元素中的所有属性节点
var nodeAttribute= node.attributes,
that = this;
//遍历所有属性节点
[].slice.call(nodeAttribute).forEach(function(attr) {
var attrName = attr.name;//取出属性名
if (that.isDirective(attrName)) {//判断当前属性名是否为指令 (根据是否有v-)
var Instruction = attr.value;//show //获取指令值
var dir = attrName.substring(2);//on:click //去掉v- 取出指令名
// 判断当前指令是否为事件指令(是否有on)
if (that.isEventDirective(dir)) {
// node.addEventListener("dir",Instruction,false);
//为当前元素绑定事件
//那么参数 node节点、that.$vm为mvvm实例、Instruction是指令、dir就是on
compileUtil.eventHandler(node, that.$vm, Instruction, dir);
} else {
// 普通指令
compileUtil[dir] && compileUtil[dir](node, that.$vm, Instruction);
}
//移除解析完成的指令
node.removeAttribute(attrName);
}
});
},
compileText: function(node, Instruction) {//Instruction为当前指令==>形参
compileUtil.text(node, this.$vm, Instruction);
},
isDirective: function(attr) {
return attr.indexOf('v-') == 0;
},
isEventDirective: function(dir) {
return dir.indexOf('on') === 0;
},
/**
* 判断当前的node是不是元节点节点
* @param node 节点
* @returns {boolean}
*/
isElementNode: function(node) {
// node = "#app"
//node.nodeType 1 element元素
return node.nodeType == 1;
},
isTextNode: function(node) {//判断当前节点是否为文本节点 返回boolean值
return node.nodeType == 3;
}
};
// 指令处理集合
var compileUtil = {
//解析v-text指令
text: function(node, mvvm, Instruction) {
this.bind(node, mvvm, Instruction, 'text');
},
//解析v-html指令
html: function(node, mvvm, Instruction) {
this.bind(node, mvvm, Instruction, 'html');
},
//解析v-model指令
model: function(node, mvvm, Instruction) {
this.bind(node, mvvm, Instruction, 'model');
var that = this,
val = this._getVMVal(mvvm, Instruction);
node.addEventListener('input', function(e) {
var newValue = e.target.value;
if (val === newValue) {
return;
}
that._setVMVal(mvvm, Instruction, newValue);
val = newValue;
});
},
//解析v-class指令
class: function(node, mvvm, Instruction) {
this.bind(node, mvvm, Instruction, 'class');
},
//解析v-bind指令
bind: function(node, mvvm, Instruction, dir) {
//根据指令名称获取对应的更新函数
var updaterFnc = updater[dir + 'Updater'];
//如果更新函数存在 则执行更新
// updaterFn && updaterFn(node, this._getVMVal(vm, Instruction));
if(updaterFnc){
//node 当前的文本节点, 值 name
//updaterFn ==> node #text {{name}} _data.name
// updaterFn(node, this._data.name);
updaterFn(node, this._getVMVal(mvvm, Instruction));
}
//Watcher监听者 vm实例 Instruction表达式{{a}}/v-text="a"
new Watcher(vm, Instruction, function(value, oldValue) {
updaterFn && updaterFn(node, value, oldValue);
});
},
// 事件处理
eventHandler: function(node, vm, Instruction, dir) {//dir==>on:click Instruction==>"show"
//从指令名中取出事件名
//根据指令的值(表达式)从methods中得到对应的回调函数
var eventType = dir.split(':')[1],
fn = vm.$pick.methods && vm.$pick.methods[Instruction];
if (eventType && fn) {
//给当前元素节点绑定指定的事件名和回调函数(指定this指向为vm)
node.addEventListener(eventType, fn.bind(vm), false);
}
},
//获取vm中data里相对应的属性值
_getVMVal: function(vm, expression) {
//vm => $vm expression==>"name" "age.a1"
var val = vm._data;
expression = expression.split('.'); //[age, a1]
expression.forEach(function(k) {//age
//val就相当于data中的深层数据值
val = val[k];
});
return val;
},
//设置vm中data里相对应的属性值
_setVMVal: function(vm, expression, value) {
var val = vm._data;
expression = expression.split('.');
expression.forEach(function(k, i) {
// 非最后一个key,更新val的值
if (i < expression.length - 1) {
val = val[k];
} else {
val[k] = value;
}
});
}
};
//更新器 操作原生DOM的方法
var updater = {
//更新节点的textContent属性
textUpdater: function(node, value) {
node.textContent = typeof value == 'undefined' ? '' : value;
},
//更新节点的innerHTML属性
htmlUpdater: function(node, value) {
node.innerHTML = typeof value == 'undefined' ? '' : value;
},
//更新节点的className属性
classUpdater: function(node, value, oldValue) {
var className = node.className; //className = > "bb"
node.className = className + (className?' ':'') + value; //bb aa
/* className = className.replace(oldValue, '').replace(/\s$/, '');
var space = className && String(value) ? ' ' : '';*/
// node.className = className + space + value; //bb aa
},
//更新节点的value属性
modelUpdater: function(node, value, oldValue) {
node.value = typeof value == 'undefined' ? '' : value;
}
};