-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel-view.html
73 lines (66 loc) · 1.56 KB
/
model-view.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta content="width=device-width,maximum-scale=1.0,minimum-scale=1.0,initial-scale=1.0,user-scalable=no" name="viewport">
</head>
<body>
<div id="app">
<p>
看这里:<span>{{name}}</span>
</p>
</div>
<div id="input-wrap">
Val: <input type="text" id="input">
</div>
<script type="text/javascript">
class gl{
constructor(options){
this.$data = options.data
this.$tpl = options.template
this.$el = options.el
this.$d = this.$setData(this.$data)
this._render(this.$tpl,this.$data)
}
$setData(obj,fn){
let self = this
return new Proxy(obj,{
set(target,property,value){
target[property] = value
console.log('change')
self._render(self.$tpl,self.$data)
return true
},
get(target, key, receiver){
/*console.log(target)
console.log(key)
console.log(receiver)*/
return target[key]
}
})
}
_render(tplString,data){
let html = this._replaceFun(tplString,data)
document.querySelector(this.$el).innerHTML = html
}
_replaceFun(tpl,data){
let reg = new RegExp('{{([^{}]*)}}','g')
return tpl.replace(reg,(a,b)=>{
return data[b]
})
}
}
let tpl = document.querySelector('#app').innerHTML
// 创建对象
let g = new gl({
data:{name:'guolei'},
template: tpl,
el: '#app',
})
// 只需要修改data 后期加上双向自动绑定 v-model(自定义指令)
document.querySelector('#input').oninput =(e)=>{
g.$d.name = e.target.value;
}
</script>
</body>
</html>