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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
| <body> <div id="app"> <input type="text" v-model="msg"> <div>{{msg}}</div> </div> <script name="Watcher"> class Watcher { constructor (vm, expr, callback) { this.vm = vm this.expr = expr this.callback = callback Dep.target = this this.oldVal = this.getVmData() Dep.target = null } getVmData () { let val = this.vm.$data[this.expr] return val } update () { let newVal = this.getVmData() if (this.oldVal !== newVal) { this.callback(newVal, this.oldVal) this.oldVal = newVal } } }
class Dep { constructor () { this.subs = [] } addSubs (watcher) { this.subs.push(watcher) } notify () { this.subs.forEach(sub => { sub.update() }) } } </script> <script name="Observer"> class Observer { constructor (data) { this.data = data this.walk(data) } walk (data) { if (!data || typeof data !== "object") return Object.keys(data).forEach(key => { this.defineReactive(data, key, data[key]) this.walk(data[key]) }) } defineReactive (obj, key, val) { let dep = new Dep() Object.defineProperty(obj, key, { enumerable:true, configurable: true, get: function () { Dep.target && dep.addSubs(Dep.target) return val }, set: (newVal) => { if (val === newVal) return val = newVal this.walk(newVal) dep.notify() } }) } } </script> <script name="Compile"> class Compile { constructor (el, vm) { this.el = typeof el === 'string' ? document.querySelector(el) : el this.vm = vm if (el) { let fragment = this.node2fragment(this.el) this.compileFragment(fragment) this.el.appendChild(fragment) } } node2fragment (el) { let fragment = document.createDocumentFragment() let childNodes = el.childNodes this.toArray(childNodes).forEach(child => { fragment.appendChild(child) }) return fragment } compileFragment (fragment) { let childNodes = fragment.childNodes this.toArray(childNodes).forEach(node => { if (this.isElementNode(node)) { this.compileElementNode(node) } if (this.isTextNode(node)) { this.compileTextNode(node) } if (node.childNodes && node.childNodes.length) { this.compileFragment(node) } }) } isElementNode (node) { return node.nodeType === 1 } isTextNode (node) { return node.nodeType === 3 } compileElementNode (node) { let that = this let attrs = node.attributes this.toArray(attrs).forEach(attr => { if (attr.nodeName === 'v-model') { let expr = attr.nodeValue node.value = this.vm.$data[expr] node.addEventListener('input', function () { that.vm.$data[expr] = this.value }) new Watcher(this.vm, expr, newVal => { node.value = newVal }) } }) } compileTextNode (node) { let reg = /\{\{(.*)\}\}/ if (reg.test(node.nodeValue)) { let expr = RegExp.$1 node.textContent = this.vm.$data[expr] new Watcher(this.vm, expr, newVal => { node.textContent = newVal }) } } toArray(classArray) { return [].slice.call(classArray) } } </script> <script name="Vue"> class Vue { constructor (options = {}) { this.$data = options.data this.$methods = options.methods this.$el = options.el this.proxyData(this.$data) this.proxyData(this.$methods) new Observer(this.$data) if (this.$el) { new Compile(this.$el, this) } } proxyData (data) { if (!data) return Object.keys(data).forEach(key => { Object.defineProperty(this, key, { enumerable: true, configurable: true, get () { return data[key] }, set (v) { if (data[key] === v) return data[key] = v } }) }) } } </script> <script> let vm = new Vue({ el: '#app', data: { msg: 'hi' } }) setTimeout(() => { vm.msg = 'hhh' }, 1000); </script> </body>
|