<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>second</title>
</head>
<body>
<div id="app" class="app">
<h2>{{name}} 's age is {{age}}</h2>
<h2>
<span>his bounty is {{money}}</span>
</h2>
<h1>{{message}}</h1>
<input v-model="message" v-on:click="sayHi" type="text">
</div>
<script>
let subjectId = 0
let observerId = 0
let currentObserver = null
class Observer {
constructor(vm, key, callback) {
this.observerId = observerId++
this.subjects = {}
this.vm = vm
this.key = key
this.callback = callback
this.value = this.getValue()
}
getValue() {
currentObserver = this
let value = this.vm[this.key]
currentObserver = null
return value
}
update() {
let oldVal = this.value
let newVal = this.getValue()
if (oldVal !== newVal) {
this.value = newVal
this.callback.bind(this)(oldVal, newVal)
}
}
subscribeTo(subject) {
if (!this.subjects[subject.subjectId]) {
subject.addObserver(this)
this.subjects[subject.subjectId] = subject
}
}
}
class Compile {
constructor(vm) {
this.vm = vm
this.node = vm.$el
this.compile()
}
compile() {
this.traverse(this.node)
}
traverse(node) {
if (node.nodeType === 1) {
this.compileNode(node)
node.childNodes.forEach(childNode => {
this.traverse(childNode)
})
} else if (node.nodeType === 3) {
this.compileText(node)
}
}
compileText(node) {
let reg = /{{(.+?)}}/g
let match
while (match = reg.exec(node.nodeValue)) {
let raw = match[0]
let key = match[1].trim()
node.nodeValue = node.nodeValue.replace(raw, this.vm[key])
new Observer(this.vm, key, function (oldValue, value) {
node.nodeValue = node.nodeValue.replace(oldValue, value)
})
}
}
compileNode(node) {
let attrs = [...node.attributes]
attrs.forEach(attr => {
if (this.isBindModel(attr.name)) {
this.bindModel(node, attr)
} else if (this.isBindEventHander(attr.name)) {
this.bindEventHander(node, attr)
}
})
}
bindModel(node, attr) {
let key = attr.value
node.value = this.vm[key]
new Observer(this.vm, key, function ({ }, newVal) {
node.value = newVal///
})
node.oninput = (e) => {
let val = e.target.value
this.vm[key] = val///setter
}
}
bindEventHander(node, attr) {
let eventType = attr.name.substr(5)
let methodName = attr.value
node.addEventListener(eventType, this.vm.$methods[methodName])
}
isBindModel(attrName) {
return attrName === 'v-model'
}
isBindEventHander(attrName) {
return attrName.indexOf('v-on') === 0
}
}
function observer(data) {
if (!data || typeof data !== 'object') { return }
for (let key in data) {
let value = data[key]
let subject = new Subject()
Object.defineProperty(data, key, {
enumerable: true,
configurable: true,
get: function () {
if (currentObserver) {
currentObserver.subscribeTo(subject)
}
return value
},
set: function (newVal) {
value = newVal
subject.notify()
}
})
if (typeof val === 'object') {
observe(val)
}
}
}
class Subject {
constructor() {
this.subjectId = subjectId++
this.observers = []
}
addObserver(observer) {
this.observers.push(observer)
}
removeObserver(observer) {
let index = this.observers.indexOf(observer)
if (index > -1) {
this.observers.splice(index, 1)
}
}
notify() {
this.observers.forEach(observer => {
observer.update()
})
}
}
class mvvm {
constructor(opts) {
this.init(opts)
observer(this.$data)
new Compile(this)
}
init(opts) {
this.$el = document.querySelector(opts.el)
this.$data = opts.data || {}
this.$methods = opts.methods || {}
for (let key in this.$data) {
Object.defineProperty(this, key, {
configurable: true,
enumerable: true,
get: () => {
return this.$data[key]
},
set: (newVal) => {
this.$data[key] = newVal
}
})
}
for (let key in this.$methods) {
this.$methods[key] = this.$methods[key].bind(this)
}
}
}
let vm = new mvvm({
el: '#app',
data: {
name: 'luffy',
age: 19,
money: 500000000,
message: `Wow,It's amazing!`
},
methods: {
sayHi() { alert(`hello,${this.name}`) }
}
})
let clock = setInterval(function () {
vm.money++
}, 1000)
//1循序漸進(jìn)2搞清楚順序,先聲明容器再依次加?xùn)|西3傳遞參數(shù)搞清楚到底想要的是什么才行4想清楚
//內(nèi)在的邏輯順序,事件發(fā)生的先后順序,函數(shù)聲明及執(zhí)行的順序非常重要.
//此處發(fā)的錯(cuò)誤有:最后聲明subjects,顛倒了渲染和劫持?jǐn)?shù)據(jù)的順序,不清楚該傳入哪個(gè)參數(shù).
//傳參順序,newval和oldval
//正則檢索的邏輯不嚴(yán)謹(jǐn)導(dǎo)致輸入的內(nèi)容和計(jì)數(shù)相同時(shí)會(huì)混亂
</script>
</body>
</html>
mvvm
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門个从,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人,你說(shuō)我怎么就攤上這事嗦锐∠铀桑” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵奕污,是天一觀的道長(zhǎng)萎羔。 經(jīng)常有香客問(wèn)我,道長(zhǎng)碳默,這世上最難降的妖魔是什么贾陷? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮嘱根,結(jié)果婚禮上髓废,老公的妹妹穿的比我還像新娘。我一直安慰自己该抒,他們只是感情好慌洪,可當(dāng)我...
- 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著凑保,像睡著了一般冈爹。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上愉适,一...
- 那天犯助,我揣著相機(jī)與錄音,去河邊找鬼维咸。 笑死剂买,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的癌蓖。 我是一名探鬼主播瞬哼,決...
- 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼租副!你這毒婦竟也來(lái)了坐慰?” 一聲冷哼從身側(cè)響起,我...
- 序言:老撾萬(wàn)榮一對(duì)情侶失蹤用僧,失蹤者是張志新(化名)和其女友劉穎结胀,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體责循,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡糟港,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了院仿。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片秸抚。...
- 正文 年R本政府宣布吭敢,位于F島的核電站碰凶,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏省有。R本人自食惡果不足惜痒留,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望蠢沿。 院中可真熱鬧伸头,春花似錦、人聲如沸舷蟀。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)野宜。三九已至扫步,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間匈子,已是汗流浹背河胎。 一陣腳步聲響...
- 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像其徙,于是被迫代替她去往敵國(guó)和親胚迫。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 1. Mvvm定義: MVVM是Model-View-ViewModel的簡(jiǎn)寫期犬。即模型-視圖-視圖模型”芊蹋【模型】指...
- 主要還是數(shù)據(jù)流向的不同。 https://blog.csdn.net/victoryzn/article/deta...
- 文章結(jié)構(gòu) 1.MVX架構(gòu)問(wèn)題1.1理解Model層1.2 萬(wàn)惡的ViewController1.3 View的復(fù)用...
- MVVM(Model View ViewModel)是一種基于MVC的設(shè)計(jì)轨功,開(kāi)發(fā)人員在HTML上寫一些Bindin...
- 很喜歡去書(shū)店古涧,但是對(duì)于買什么書(shū)卻眼花繚亂不知所措垂券。不知如何選擇時(shí)只好看作者,所以同時(shí)買了太宰治的《斜陽(yáng)》和《...