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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
| class SelfVue{ constructor(options){ var self = this; this.data = options.data; this.methods = options.methods; this.vm = this; Object.keys(this.data).forEach(function(key) { self.proxyKeys(key); });
new observe(this.data); new Compile(options, this.vm); options.mounted.call(this); } proxyKeys(key) { var self = this; Object.defineProperty(this, key, { enumerable: false, configurable: true, get: function getter () { return self.data[key]; }, set: function setter (newVal) { self.data[key] = newVal; } }); } }
class observe{ constructor(data){ if(!data || typeof data !== 'object') { return; } Object.keys(data).forEach(key => { this.defineReactive(data, key, data[key]); }) } defineReactive(data,key,value){ new observe(value); var dep = new Dep(); Object.defineProperty(data, key, { enumerable: true, configurable: true, set(newVal){ if (value === newVal) { return; } value = newVal; console.log('属性' + key + '已经被监听了,现在值为:“' + newVal.toString() + '”'); dep.notify(); }, get(){ if (Dep.target) { dep.addSub(Dep.target); } return value; } }) } } class Dep{ constructor(){ this.subs = []; this.target = null; } addSub(sub) { this.subs.push(sub); } notify() { this.subs.forEach(function(sub) { sub.update(); }); } } class Watcher{ constructor(vm, exp, cb){ this.vm = vm; this.exp = exp; this.cb = cb; this.value = this.get() } update(){ this.run(); } run(){ let value = this.vm.data[this.exp] let oldValue = this.value; if(value !== oldValue){ this.value = value; this.cb.call(this.vm, value, oldValue); } } get(){ Dep.target = this; let value = this.vm.data[this.exp]; Dep.target = null; return value; } } class Compile { constructor(el, vm){ this.vm = vm; this.el = document.querySelector(el.el); this.fragment = null; this.init(); } init() { if (this.el) { this.fragment = this.nodeToFragment(this.el); this.compileElement(this.fragment); this.el.appendChild(this.fragment); } else { console.log('Dom元素不存在'); } } nodeToFragment (el) { var fragment = document.createDocumentFragment(); var child = el.firstChild; while (child) { fragment.appendChild(child); child = el.firstChild } return fragment; } compileElement(el) { var childNodes = el.childNodes; var self = this; [].slice.call(childNodes).forEach(function(node) { var reg = /\{\{(.*)\}\}/; var text = node.textContent;
if (self.isElementNode(node)) { self.compile(node); } else if (self.isTextNode(node) && reg.test(text)) { self.compileText(node, reg.exec(text)[1]); }
if (node.childNodes && node.childNodes.length) { self.compileElement(node); } }); } compile(node) { var nodeAttrs = node.attributes; var self = this; Array.prototype.forEach.call(nodeAttrs, function(attr) { var attrName = attr.name; if (self.isDirective(attrName)) { var exp = attr.value; var dir = attrName.substring(2); if (self.isEventDirective(dir)) { self.compileEvent(node, self.vm, exp, dir); } else { self.compileModel(node, self.vm, exp, dir); } node.removeAttribute(attrName); } }); } compileText(node, exp) { var self = this; var initText = this.vm[exp]; this.updateText(node, initText); new Watcher(this.vm, exp, function (value) { self.updateText(node, value); }); } compileEvent(node, vm, exp, dir) { var eventType = dir.split(':')[1]; var cb = vm.methods && vm.methods[exp];
if (eventType && cb) { node.addEventListener(eventType, cb.bind(vm), false); } } compileModel(node, vm, exp, dir) { var self = this; var val = this.vm[exp]; this.modelUpdater(node, val); new Watcher(this.vm, exp, function (value) { self.modelUpdater(node, value); });
node.addEventListener('input', function(e) { var newValue = e.target.value; if (val === newValue) { return; } self.vm[exp] = newValue; val = newValue; }); } updateText (node, value) { node.textContent = typeof value == 'undefined' ? '' : value; } modelUpdater(node, value, oldValue) { node.value = typeof value == 'undefined' ? '' : value; } isDirective(attr) { return attr.indexOf('v-') == 0; } isEventDirective(dir) { return dir.indexOf('on:') === 0; } isElementNode (node) { return node.nodeType == 1; } isTextNode(node) { return node.nodeType == 3; } }
|