概念
双向绑定概念其实很简单,就是视图(View)的变化能实时让数据模型(Model)发生变化,而数据的变化也能实时更新到视图层。我们所说的单向数据绑定就是从数据到视图这一方向的关系。
分析
1、响应式数据
使用Object.defineProperty、Proxy对数据进行监听拦截。
Object.defineProperty(obj, prop, descriptor)
vue3.0 开始 Proxy代替Object.defineProperty
let p = new Proxy(target, handler);
2、input事件监听
绑定事件处理函数,实时修改数据。
3、相关dom操作
将数据与相关dom节点绑定在一起,修改数据的时候对应的dom节点也应一起改变。
实现
1、html页面
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>v-model</title>
</head>
<body>
<div id="app">
<input type="text" v-model="name" placeholder="姓名"/>
<input type="text" v-model="age" placeholder="年龄"/>
<div>
<p>
姓名:<span>{{name}}</span>
</p>
<p>
年龄:<span>{{age}}</span>
</p>
<p>
<p>
年龄:<span>{{age}}</span>
</p>
</p>
</div>
<button id="btn">修改名字</button>
</div>
</body>
<script src="./VModel.js"></script>
<script>
const app = new VModel('#app',{
name:'',
age:''
});
document.getElementById('btn').addEventListener('click',function() {
app.setData('name','名字改变了');
})
</script>
</html>
2、VModel.js
(1)构造VModel
class VModel {
constructor(el,data) {
this.el = document.querySelector(el);
this._data = data;
this.domPoll = {};
this.init();
}
}
(2)初始化数据对象
使用Object.defineProperty
initData () {
const _this = this;
this.data = {};
for(let key in this._data){
Object.defineProperty(this.data,key,{
get(){
console.log("获取数据",key,_this._data[key]);
return _this._data[key];
},
set(newVal){
console.log("设置数据",key,newVal);
_this.domPoll[key].innerText = newVal;
_this._data[key] = newVal;
}
});
}
}
使用Proxy
initData () {
const _this = this;
this.data = {};
this.data = new Proxy(this.data,{
get(target,key){
return Reflect.get(target,key);
},
set(target,key,value){
_this.domPoll[key].forEach(item => {
item.innerText = value;
})
return Reflect.set(target,key,value);
}
})
}
(3)绑定dom节点
bindDom(el){
const childNodes = el.childNodes;
childNodes.forEach(item => {
if(item.nodeType === 3){
const _value = item.nodeValue;
if(_value.trim().length){
let _isValid = /\{\{(.+?)\}\}/.test(_value);
if(_isValid){
const _key = _value.match(/\{\{(.+?)\}\}/)[1].trim();
if(!this.domPoll[_key]) this.domPoll[_key] = [];
this.domPoll[_key].push(item.parentNode);
item.parentNode.innerText = this.data[_key] || undefined;
}
}
}
item.childNodes && this.bindDom(item);
})
}
####(4)输入框数据绑定
bindInput(el){
const _allInput = el.querySelectorAll('input');
_allInput.forEach(input => {
const _vModel = input.getAttribute('v-model');
if(_vModel){
}
})
}
handleInput(key,input){
const _value = input.value;
this.data[key] = _value;
}
(4)完整代码
class VModel {
constructor(el,data) {
this.el = document.querySelector(el);
this._data = data;
this.domPoll = {};
this.init();
}
init(){
this.initData();
this.initDom();
}
initDom(){
this.bindDom(this.el);
this.bindInput(this.el);
console.log('domPoll',this.domPoll);
}
initData () {
const _this = this;
this.data = {};
this.data = new Proxy(this.data,{
get(target,key){
return Reflect.get(target,key);
},
set(target,key,value){
_this.domPoll[key].forEach(item => {
item.innerText = value;
})
return Reflect.set(target,key,value);
}
})
}
bindDom(el){
const childNodes = el.childNodes;
childNodes.forEach(item => {
if(item.nodeType === 3){
const _value = item.nodeValue;
if(_value.trim().length){
let _isValid = /\{\{(.+?)\}\}/.test(_value);
if(_isValid){
const _key = _value.match(/\{\{(.+?)\}\}/)[1].trim();
if(!this.domPoll[_key]) this.domPoll[_key] = [];
this.domPoll[_key].push(item.parentNode);
item.parentNode.innerText = this.data[_key] || undefined;
}
}
}
item.childNodes && this.bindDom(item);
})
}
bindInput(el){
const _allInput = el.querySelectorAll('input');
_allInput.forEach(input => {
const _vModel = input.getAttribute('v-model');
if(_vModel){
input.addEventListener('keyup',this.handleInput.bind(this,_vModel,input),false);
}
})
}
handleInput(key,input){
const _value = input.value;
this.data[key] = _value;
}
setData(key,value){
this.data[key] = value;
}
}
|