亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2018年06月21日 16:39:13   作者:OreChou的小號(hào)  
這篇文章主要介紹了MVVM 雙向綁定的實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

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

MVVM 具體概念

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

Directive (指令)

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

Filter (過濾器)

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

表達(dá)式

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

ViewModel

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

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

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

Model 層的變化改變 View:

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

實(shí)現(xiàn)方式

手動(dòng)觸發(fā)綁定

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

首先編寫 HTML 頁面

Two way binding

編寫實(shí)現(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é)點(diǎn)屬性
 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ù)對(duì)象后
 data[key] = value
 // 手動(dòng)地去觸發(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ù)劫持實(shí)現(xiàn)。實(shí)現(xiàn)方式是通過 Object.defineProperty 和 Object.defineProperies 方法對(duì) Model 對(duì)象的 get 和 set 函數(shù)進(jìn)行監(jiān)聽。當(dāng)有數(shù)據(jù)讀取或賦值操作時(shí),掃描(或者通知)對(duì)應(yīng)的元素執(zhí)行 Directive 函數(shù),實(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
 }
}

// 定義對(duì)象屬性設(shè)置劫持
// obj: 指定的 Model 數(shù)據(jù)對(duì)象
// 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 中,這里不會(huì)去掃描所有的元素,而是通過訂閱發(fā)布模式,通知那些訂閱了該數(shù)據(jù)的 view 進(jìn)行更新
   scan()
  },
  enumerable: true,
  configurable: true
 })
}

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

// 掃描所有的元素
function scan() {
 // 掃描帶指令的節(jié)點(diǎn)屬性
 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ù)設(shè)置新值之后,在 set 方法中會(huì)去更新 view
 data.value = 'Hello world'
}, 1000);

基于 Proxy 的實(shí)現(xiàn)

Proxy 是 ES6 中的新特性??梢栽谝延械膶?duì)象基礎(chǔ)上定義一個(gè)新對(duì)象,并重新定義對(duì)象原型上的方法。例如 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
 }
}

// 設(shè)置對(duì)象的代理
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é)點(diǎn)屬性
 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)測(cè)

基本原理是在 Model 對(duì)象的屬性值發(fā)生變化的時(shí)候找到與該屬性值相關(guān)的所有元素,然后判斷數(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)檢測(cè)
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
    // 進(jìn)行臟數(shù)據(jù)檢測(cè),如果數(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);

總結(jié)

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

參考

現(xiàn)代前端技術(shù)解析

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • js中鍵盤事件實(shí)例簡(jiǎn)析

    js中鍵盤事件實(shí)例簡(jiǎn)析

    這篇文章主要介紹了js中鍵盤事件,以一個(gè)較為簡(jiǎn)單的實(shí)例形式分析了js響應(yīng)鍵盤事件的操作技巧,需要的朋友可以參考下
    2015-01-01
  • JavaScript 對(duì)象、函數(shù)和繼承

    JavaScript 對(duì)象、函數(shù)和繼承

    JavaScript可以說是一個(gè)基于對(duì)象的編程語言,為什么說是基于對(duì)象而不是面向?qū)ο?,因?yàn)镴avaScript自身只實(shí)現(xiàn)了封裝,而沒有實(shí)現(xiàn)繼承和多態(tài)。
    2009-07-07
  • 一個(gè)高效的JavaScript壓縮工具下載集合

    一個(gè)高效的JavaScript壓縮工具下載集合

    這里介紹的是一些比較常用的js 壓縮工具,隨著問題的不斷解決,好用的工具要經(jīng)得起考驗(yàn)。
    2007-03-03
  • 理解javascript中Map代替循環(huán)

    理解javascript中Map代替循環(huán)

    這篇文章主要幫助大家理解javascript中Map代替循環(huán),感興趣的小伙伴們可以參考一下
    2016-02-02
  • 微信小程序開發(fā)之左右分欄效果的實(shí)例代碼

    微信小程序開發(fā)之左右分欄效果的實(shí)例代碼

    本文以一個(gè)簡(jiǎn)單的小例子,簡(jiǎn)述在微信小程序開發(fā)中左右分欄功能的實(shí)現(xiàn)方式,主要涉及scroll-view ,列表數(shù)據(jù)綁定,及簡(jiǎn)單樣式等內(nèi)容,感興趣的朋友跟隨小編一起看看吧
    2019-05-05
  • 基于webpack.config.js 參數(shù)詳解

    基于webpack.config.js 參數(shù)詳解

    下面小編就為大家分享一篇基于webpack.config.js 參數(shù)詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • Highcharts使用簡(jiǎn)例及異步動(dòng)態(tài)讀取數(shù)據(jù)

    Highcharts使用簡(jiǎn)例及異步動(dòng)態(tài)讀取數(shù)據(jù)

    Highcharts 是一個(gè)用純JavaScript編寫的一個(gè)圖表庫, 能夠很簡(jiǎn)單便捷的在web網(wǎng)站或是web應(yīng)用程序添加有交互性的圖表,并且免費(fèi)提供給個(gè)人學(xué)習(xí)、個(gè)人網(wǎng)站和非商業(yè)用途使用,通過本文給大家介紹Highcharts使用簡(jiǎn)例及異步動(dòng)態(tài)讀取數(shù)據(jù)的相關(guān)知識(shí),感興趣的朋友一起學(xué)習(xí)吧
    2015-12-12
  • JavaScript實(shí)現(xiàn)時(shí)鐘功能

    JavaScript實(shí)現(xiàn)時(shí)鐘功能

    這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)時(shí)鐘功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • 屏蔽Flash右鍵信息的js代碼

    屏蔽Flash右鍵信息的js代碼

    網(wǎng)上好多屏蔽flash右鍵的方法,可多數(shù)是過期的或不合適的,現(xiàn)記錄下用JS方法屏蔽flash右鍵相關(guān)版權(quán)信息。
    2010-01-01
  • js調(diào)用AJAX時(shí)Get和post的亂碼解決方法

    js調(diào)用AJAX時(shí)Get和post的亂碼解決方法

    在使用"get"時(shí),抓取的頁面最后加上編碼類型,在使用post時(shí)用vbscript解決了編碼問題,具體實(shí)現(xiàn)如下,有類似情況的朋友可以參考下哈
    2013-06-06

最新評(píng)論