做爰高潮a片〈毛片〉,尤物av天堂一区二区在线观看,一本久久A久久精品VR综合,添女人荫蒂全部过程av

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當前位置: 首頁 - 科技 - 知識百科 - 正文

MVVM 雙向綁定的實現(xiàn)代碼

來源:懂視網 責編:小采 時間:2020-11-27 22:12:44
文檔

MVVM 雙向綁定的實現(xiàn)代碼

MVVM 雙向綁定的實現(xiàn)代碼:這篇文章主要記錄學習 JS 雙向綁定過程中的一些概念與具體的實現(xiàn) MVVM 具體概念 MVVM 中有一些概念是通用的,具體如下 Directive (指令) 自定義的執(zhí)行函數(shù),例如 Vue 中的 v-click、v-bind 等。這些函數(shù)封裝了 DOM 的一些基本可復用函數(shù)API。 F
推薦度:
導讀MVVM 雙向綁定的實現(xiàn)代碼:這篇文章主要記錄學習 JS 雙向綁定過程中的一些概念與具體的實現(xiàn) MVVM 具體概念 MVVM 中有一些概念是通用的,具體如下 Directive (指令) 自定義的執(zhí)行函數(shù),例如 Vue 中的 v-click、v-bind 等。這些函數(shù)封裝了 DOM 的一些基本可復用函數(shù)API。 F

這篇文章主要記錄學習 JS 雙向綁定過程中的一些概念與具體的實現(xiàn)

MVVM 具體概念

MVVM 中有一些概念是通用的,具體如下

Directive (指令)

自定義的執(zhí)行函數(shù),例如 Vue 中的 v-click、v-bind 等。這些函數(shù)封裝了 DOM 的一些基本可復用函數(shù)API。

Filter (過濾器)

用戶希望對傳入的初始數(shù)據(jù)進行處理,然后將處理結果交給 Directive 或者下一個 Filter。例如:v-bind="time | formatTime"。formatTime 是將 time 轉換成指定格式的 Filter 函數(shù)。

表達式

類似前端普通的頁面模板表達式,作用是控制頁面內容安裝具體的條件顯示。例如:if...else 等

ViewModel

傳入的 Model 數(shù)據(jù)在內存中存放,提供一些基本的操作 API 給開發(fā)者,使其能夠對數(shù)據(jù)進行讀取與修改

雙向綁定(數(shù)據(jù)變更檢測)

View 層的變化改變 Model:通過給元素添加 onchange 事件來觸發(fā)對 Model 數(shù)據(jù)進行修改

Model 層的變化改變 View:

  1. 手動觸發(fā)綁定
  2. 臟數(shù)據(jù)檢測
  3. 對象劫持
  4. Proxy

實現(xiàn)方式

手動觸發(fā)綁定

即 Model 對象改變之后,需要顯示的去觸發(fā) View 的更新

首先編寫 HTML 頁面

Two way binding

編寫實現(xiàn) MVVM 的 代碼

// Manual trigger
let elems = [document.getElementById('el'), document.getElementById('input')]
// 數(shù)據(jù) Model
let data = {
 value: 'hello'
}

// 定義 Directive
let directive = {
 text: function(text) {
 this.innerHTML = text
 },
 value: function(value) {
 this.setAttribute('value', value)
 this.value = value
 }
}

// 掃描所有的元素
function scan() {
 // 掃描帶指令的節(jié)點屬性
 for (let elem of elems) {
 elem.directive = []
 for (let attr of elem.attributes) {
 if (attr.nodeName.indexOf('q-') >= 0) {
 directive[attr.nodeName.slice(2)].call(elem, data[attr.nodeValue])
 elem.directive.push(attr.nodeName.slice(2))
 }
 }
 }
}

// ViewModel 更新函數(shù)
function ViewModelSet(key, value) {
 // 修改數(shù)據(jù)對象后
 data[key] = value
 // 手動地去觸發(fā) View 的修改
 scan()
}

// View 綁定監(jiān)聽
elems[1].addEventListener('keyup', function(e) {
 ViewModelSet('value', e.target.value)
}, false)

// -------- 程序執(zhí)行 -------
scan()
setTimeout(() => {
 ViewModelSet('value', 'hello world')
}, 1000);

數(shù)據(jù)劫持

數(shù)據(jù)劫持是目前比較廣泛的方式,Vue 的雙向綁定就是通過數(shù)據(jù)劫持實現(xiàn)。實現(xiàn)方式是通過 Object.defineProperty 和 Object.defineProperies 方法對 Model 對象的 get 和 set 函數(shù)進行監(jiān)聽。當有數(shù)據(jù)讀取或賦值操作時,掃描(或者通知)對應的元素執(zhí)行 Directive 函數(shù),實現(xiàn) View 的刷新。

HTML 的代碼不變,js 代碼如下

// Hijacking
let elems = [document.getElementById('el'), document.getElementById('input')]
let data = {
 value: 'hello'
}

// 定義 Directive
let directive = {
 text: function(text) {
 this.innerHTML = text
 },
 value: function(value) {
 this.setAttribute('value', value)
 this.value = value
 }
}

// 定義對象屬性設置劫持
// obj: 指定的 Model 數(shù)據(jù)對象
// propName: 指定的屬性名稱
function defineGetAndSet(obj, propName) {
 let bValue
 // 使用 Object.defineProperty 做數(shù)據(jù)劫持
 Object.defineProperty(obj, propName, {
 get: function() {
 return bValue
 },
 set: function(value) {
 bValue = value
 // 在 vue 中,這里不會去掃描所有的元素,而是通過訂閱發(fā)布模式,通知那些訂閱了該數(shù)據(jù)的 view 進行更新
 scan()
 },
 enumerable: true,
 configurable: true
 })
}

// View 綁定監(jiān)聽
elems[1].addEventListener('keyup', function(e) {
 data.value = e.target.value
}, false)

// 掃描所有的元素
function scan() {
 // 掃描帶指令的節(jié)點屬性
 for (let elem of elems) {
 elem.directive = []
 for (let attr of elem.attributes) {
 if (attr.nodeName.indexOf('q-') >= 0) {
 directive[attr.nodeName.slice(2)].call(elem, data[attr.nodeValue])
 elem.directive.push(attr.nodeName.slice(2))
 }
 }
 }
}

// -------- 程序執(zhí)行 -------
scan()
defineGetAndSet(data, 'value')
setTimeout(() => {
 // 這里為數(shù)據(jù)設置新值之后,在 set 方法中會去更新 view
 data.value = 'Hello world'
}, 1000);

基于 Proxy 的實現(xiàn)

Proxy 是 ES6 中的新特性。可以在已有的對象基礎上定義一個新對象,并重新定義對象原型上的方法。例如 get 和 set 方法。

// Hijacking
let elems = [document.getElementById('el'), document.getElementById('input')]

// 定義 Directive
let directive = {
 text: function(text) {
 this.innerHTML = text
 },
 value: function(value) {
 this.setAttribute('value', value)
 this.value = value
 }
}

// 設置對象的代理
let data = new Proxy({}, {
 get: function(target, key, receiver) {
 return target.value
 },
 set: function (target, key, value, receiver) { 
 target.value = value
 scan()
 return target.value
 }
})

// View 綁定監(jiān)聽
elems[1].addEventListener('keyup', function(e) {
 data.value = e.target.value
}, false)

// 掃描所有的元素
function scan() {
 // 掃描帶指令的節(jié)點屬性
 for (let elem of elems) {
 elem.directive = []
 for (let attr of elem.attributes) {
 if (attr.nodeName.indexOf('q-') >= 0) {
 directive[attr.nodeName.slice(2)].call(elem, data[attr.nodeValue])
 elem.directive.push(attr.nodeName.slice(2))
 }
 }
 }
}

// -------- 程序執(zhí)行 -------
data['value'] = 'Hello'
scan()
setTimeout(() => {
 data.value = 'Hello world'
}, 1000);

臟數(shù)據(jù)監(jiān)測

基本原理是在 Model 對象的屬性值發(fā)生變化的時候找到與該屬性值相關的所有元素,然后判斷數(shù)據(jù)是否發(fā)生變化,若變化則更新 View。

編寫頁面代碼如下:Two way binding

js 代碼如下:

// Dirty detection
let elems = [document.getElementById('el'), document.getElementById('input')]
let data = {
 value: 'hello'
}

// 定義 Directive
let directive = {
 text: function(text) {
 this.innerHTML = text
 },
 value: function(value) {
 this.setAttribute('value', value)
 this.value = value
 }
}

// 臟數(shù)據(jù)循環(huán)檢測
function digest(elems) {
 for (let elem of elems) {
 if (elem.directive === undefined) {
 elem.directive = {}
 }
 for (let attr of elem.attributes) {
 if (attr.nodeName.indexOf('q-event') >= 0) {
 let dataKey = elem.getAttribute('q-bind') || undefined
 // 進行臟數(shù)據(jù)檢測,如果數(shù)據(jù)改變,則重新執(zhí)行命令
 if (elem.directive[attr.nodeValue] !== data[dataKey]) {
 directive[attr.nodeValue].call(elem, data[dataKey])
 elem.directive[attr.nodeValue] = data[dataKey]
 }
 }
 }
 }
}

// 數(shù)據(jù)監(jiān)聽
function $digest(value) {
 let list = document.querySelectorAll('[q-bind=' + value + ']')
 digest(list)
}

// View 綁定監(jiān)聽
elems[1].addEventListener('keyup', function(e) {
 data.value = e.target.value
 $digest(e.target.getAttribute('q-bind'))
}, false)

// -------- 程序執(zhí)行 -------
$digest('value')
setTimeout(() => {
 data.value = "Hello world"
 $digest('value')
}, 1000);

總結

上面只是簡單地實現(xiàn)了雙向綁定,但實際上一個完整的 MVVM 框架要考慮很多東西。在上面的實現(xiàn)中數(shù)據(jù)劫持的方法更新View 是使用了 Scan 函數(shù),但實際的實現(xiàn)中(比如 Vue)是使用了發(fā)布訂閱的模式。它只會去更新那些與該 Model 數(shù)據(jù)綁定的元素,而不會去掃描所有元素。而在臟數(shù)據(jù)檢測中,它去找到了所有綁定的元素,然后判斷數(shù)據(jù)是否發(fā)生變化,這種方式只有一定的性能開銷的。

參考

《現(xiàn)代前端技術解析》

代碼下載:https://github.com/OreChou/twowaybinding

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

MVVM 雙向綁定的實現(xiàn)代碼

MVVM 雙向綁定的實現(xiàn)代碼:這篇文章主要記錄學習 JS 雙向綁定過程中的一些概念與具體的實現(xiàn) MVVM 具體概念 MVVM 中有一些概念是通用的,具體如下 Directive (指令) 自定義的執(zhí)行函數(shù),例如 Vue 中的 v-click、v-bind 等。這些函數(shù)封裝了 DOM 的一些基本可復用函數(shù)API。 F
推薦度:
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 肃宁县| 揭东县| 辰溪县| 昔阳县| 利津县| 沙坪坝区| 休宁县| 中方县| 汝州市| 巴塘县| 迁安市| 东明县| 清丰县| 宜良县| 萨迦县| 银川市| 安吉县| 江山市| 安丘市| 嘉祥县| 株洲市| 伊吾县| 洛南县| 富平县| 鲜城| 双江| 富川| 马龙县| 新余市| 天台县| 尼勒克县| 温泉县| 江孜县| 扬州市| 康保县| 靖边县| 靖宇县| 大港区| 兴义市| 吉水县| 米易县|