欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Vue 事件驅(qū)動和依賴追蹤

 更新時間:2017年04月22日 08:29:55   作者:Pawn.風(fēng)為裳  
本篇文章主要介紹了詳解Vue 事件驅(qū)動和依賴追蹤 ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

之前關(guān)于 Vue 數(shù)據(jù)綁定原理的一點分析,最近需要回顧,就順便發(fā)到隨筆上了

在之前實現(xiàn)一個自己的Mvvm中,用 setter 來觀測model,將界面上所有的 viewModel 綁定到 model 上。 當(dāng)model改變,更新所有的viewModel,將新值渲染到界面上 。同時監(jiān)聽界面上通過v-model 綁定的所有 input,并通過 addEventListener事件將新值更新到 model 上,以此來完成雙向綁定 。

但是那段程序除了用來理解 defineProperty,其它一文不值。

  1. 沒有編譯節(jié)點 。
  2. 沒有處理表達式依賴 。

這里我將解決表達式依賴這個問題,vue 模板的編譯我會在下一節(jié)介紹 。

為數(shù)據(jù)定義 getter & setter

class Observer {
 constructor(data) {
  this._data = data;
  this.walk(this._data);
 }

 walk(data) {
  Object.keys(data).forEach((key) => { this.defineRective(data, key, data[key]) })
 };
 defineRective(vm, key, value) {
  var self = this;
  if (value && typeof value === "object") {
   this.walk(value);
  }
  Object.defineProperty(vm, key, {
   get: function() {
    return value;
   },
   set: function(newVal) {
    if (value != newVal) {
     if (newVal && typeof newVal === "object") {
      self.walk(newVal);
     }
     value = newVal;
    }
   }
  })
 }
}

module.exports = Observer;

這樣,就為每個屬性添加了 getter setter ,當(dāng)屬性是一個對象,那么就遞歸添加。

 一旦獲取屬性值或者為屬性賦值就會觸發(fā) get set ,當(dāng)觸發(fā)了 set,即model變化,就可以發(fā)布一個消息,通知所有viewModel 更新。

defineRective(vm, key, value) {
 // 將這個屬性的依賴表達式存儲在閉包中。
 var dep = new Dep();
 var self = this;
 if (value && typeof value === "object") {
  this.walk(value);
 }
 Object.defineProperty(vm, key, {
  get: function() {
   return value;
  },
  set: function(newVal) {
   if (value != newVal) {
    if (newVal && typeof newVal === "object") {
     self.walk(newVal);
    }
    value = newVal;
    // 通知所有的 viewModel 更新
    dep.notify();
   }
  }
 })
}

那么怎么定義 Dep 呢??

class Dep {
 constructor() {
  // 依賴列表
  this.dependences = [];
 }
 // 添加依賴
 addDep(watcher) {
  if (watcher) {
   this.dependences.push(watcher);
  }
 }
 // 通知所有依賴更新
 notify() {
  this.dependences.forEach((watcher) => {
   watcher.update();
  })
 }
}

module.exports = Dep;

這里的每個依賴就是一個Watcher

看看如何定義 Watcher

這里每一個 Watcher 都會有一個唯一的id號,它擁有一個表達式和一個回調(diào)函數(shù) 。

比如 表達式 a +b ; 會在get 計算時 訪問 a b , 由于 JavaScript是單線程,任一時刻只有一處JavaScript代碼在執(zhí)行, 用Dep.target 作為一個全局變量來表示當(dāng)前 Watcher 的表達式,然后通過 compute 訪問 a ,b ,觸發(fā) a b getter,在 getter 里面將 Dep.target 添加為依賴 。

一旦 a b set 觸發(fā),調(diào)用 update 函數(shù),更新依賴的值 。

var uid = 0;
class Watcher {
 constructor(viewModel, exp, callback) {
  this.viewModel = viewModel;
  this.id = uid++;
  this.exp = exp;
  this.callback = callback;
  this.oldValue = "";
  this.update();
 }

 get() {
  Dep.target = this;
  var res = this.compute(this.viewModel, this.exp);
  Dep.target = null;
  return res;
 }

 update() {
  var newValue = this.get();
  if (this.oldValue === newValue) {
   return;
  }
  // callback 里進行Dom 的更新操作
  this.callback(newValue, this.oldValue);
  this.oldValue = newValue;
 }

 compute(viewModel, exp) {
  var res = replaceWith(viewModel, exp);
  return res;
 }
}

module.exports = Watcher;

由于當(dāng)前表達式需要在 當(dāng)前的model下面執(zhí)行,所以 采用replaceWith 函數(shù)來代替 with 。

通過get 添加依賴

Object.defineProperty(vm, key, {
 get: function() {
  var watcher = Dep.target;
  if (watcher && !dep.dependences[watcher.id]) {
   dep.addDep(watcher);
  }
  return value;
 },
 set: function(newVal) {
  if (value != newVal) {
   if (newVal && typeof newVal === "object") {
    self.walk(newVal);
   }
   value = newVal;
   dep.notify();
  }
 }
})

這種添加依賴的方式實在太巧妙了 。

這里我畫了一個圖來描述

最后通過一段代碼簡單測試一下

const Observer = require('./Observer.js');
const Watcher = require('./watcher.js');
var data = {
 a: 10,
 b: {
  c: 5,
  d: {
   e: 20,
  }
 }
}

var observe = new Observer(data);

var watcher = new Watcher(data, "a+b.c", function(newValue, oldValue) {
 console.log("new value is " + newValue);
 console.log("oldValue is " + oldValue);
});
console.log("\r\n");
console.log("a has changed to 50,then the expr should has value 55");
data.a = 50;

console.log("\r\n");
console.log("b.c has changed to 50,then the expr should has value 122");
data.b.c = 72;;

console.log("\r\n");
console.log("b.c has reseted an object,then the expr should has value 80");
data.b = { c: 30 }

OK 大功告成

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

相關(guān)文章

  • vue使用pdf.js預(yù)覽pdf文件的方法

    vue使用pdf.js預(yù)覽pdf文件的方法

    在頁面進行pdf預(yù)覽的時候,由于文件不能夠打印和下載很難滿足客戶的需求,接下來通過本文給大家介紹vue使用pdf.js來進行pdf預(yù)覽,需要的朋友可以參考下
    2021-12-12
  • 詳解Vue 換膚方案驗證

    詳解Vue 換膚方案驗證

    這篇文章主要介紹了Vue 換膚方案驗證,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • 解決Vue使用百度地圖BMapGL內(nèi)存泄漏問題?Out?of?Memory

    解決Vue使用百度地圖BMapGL內(nèi)存泄漏問題?Out?of?Memory

    這篇文章主要介紹了解決Vue使用百度地圖BMapGL內(nèi)存泄漏問題?Out?of?Memory,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Vue組件實現(xiàn)原理詳細分析

    Vue組件實現(xiàn)原理詳細分析

    這篇文章主要介紹了Vue組件基礎(chǔ)操作,組件是vue.js最強大的功能之一,而組件實例的作用域是相互獨立的,這就意味著不同組件之間的數(shù)據(jù)無法相互進行直接的引用
    2023-01-01
  • Django+Vue跨域環(huán)境配置詳解

    Django+Vue跨域環(huán)境配置詳解

    這篇文章主要介紹了Django+Vue跨域環(huán)境配置詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07
  • 如何解決ElementUI導(dǎo)航欄重復(fù)點菜單報錯問題

    如何解決ElementUI導(dǎo)航欄重復(fù)點菜單報錯問題

    這篇文章主要介紹了如何解決ElementUI導(dǎo)航欄重復(fù)點菜單報錯問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • VSCode插件安裝完成后的配置(常用配置)

    VSCode插件安裝完成后的配置(常用配置)

    這篇文章主要介紹了VSCode插件安裝完成后的配置,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-08-08
  • 如何使用vue實現(xiàn)前端導(dǎo)入excel數(shù)據(jù)

    如何使用vue實現(xiàn)前端導(dǎo)入excel數(shù)據(jù)

    在實際開發(fā)中導(dǎo)入功能是非常常見的,導(dǎo)入功能前端并不難,下面這篇文章主要給大家介紹了關(guān)于如何使用vue實現(xiàn)前端導(dǎo)入excel數(shù)據(jù)的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-04-04
  • vue中destroyed方法及使用示例講解

    vue中destroyed方法及使用示例講解

    這篇文章主要為大家介紹了vue中destroyed方法及使用示例講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-06-06
  • 關(guān)于vue-router的beforeEach無限循環(huán)的問題解決

    關(guān)于vue-router的beforeEach無限循環(huán)的問題解決

    本篇文章主要介紹了關(guān)于vue-router的beforeEach無限循環(huán)的問題解決,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-09-09

最新評論