Ai
2 Star 7 Fork 1

zhouccccc/Blog

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
index.js 1.63 KB
一键复制 编辑 原始数据 按行查看 历史
Chengcheng ZHOU 提交于 2018-07-02 13:48 +08:00 . iniy
//数据源
let vm = {
value: 0
}
//用于管理watcher的Dep对象
let Dep = function () {
this.list = [];
this.add = function(watcher){
this.list.push(watcher)
},
this.notify = function(newValue){
this.list.forEach(function (fn) {
fn(newValue)
})
}
};
// 模拟compile,通过对Html的解析生成一系列订阅者(watcher)
function renderInput(newValue) {
let el = document.getElementById('inp');
if (el) {
el.value = newValue
}
}
function renderTitle(newValue) {
let el = document.getElementById('h1');
if (el) {
el.innerHTML = newValue
}
}
//将解析出来的watcher存入Dep中待用
let dep = new Dep();
dep.add(renderInput);
dep.add(renderTitle)
//核心方法
function initMVVM(vm) {
Object.keys(vm).forEach(function (key) {
observer(vm, key, vm[key])
})
}
function observer(vm, key, value) {
Object.defineProperty(vm, key, {
enumerable: true,
configurable: true,
get: function () {
console.log('Get');
return value
},
set: function (newValue) {
if (value !== newValue) {
value = newValue
console.log('Update')
//将变动通知给相关的订阅者
dep.notify(newValue)
}
}
})
}
//页面引用的方法
function inputChange(ev) {
let value = Number.parseInt(ev.target.value);
vm.value = (Number.isNaN(value)) ? 0 : value;
}
function btnAdd() {
vm.value = vm.value + 1;
}
//初始化数据源
initMVVM(vm)
//初始化页面
dep.notify(vm.value);
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/merico/Blog.git
git@gitee.com:merico/Blog.git
merico
Blog
Blog
master

搜索帮助