Giter Site home page Giter Site logo

mvvm_test's Introduction

mvvm_test

a simple mvvm framework test for learning

一个双向绑定的简单实现

原理

  1. 核心**是对数据仓库中的每个属性,建立发布者,对应的文档节点作为订阅者订阅该属性的更新。实现方法是对数据的setter进行劫持。每次更新值的时候,在setter中调用对应发布者的notify函数,通知订阅者更新。
  2. 反向数据流通过解析文档树,拿到节点上自定义的命令或双引号引用变量。如v-bind指令,将当前节点绑定为对应数据的订阅者。这里将节点绑定为对应属性的订阅者是通过对数据的getter进行劫持,在执行时判断有无需要添加的订阅者。
  3. 所以这里的关键就是建立这种发布/订阅者的关系。负责建立这种关系的角色就是observer。它的具体作用是,对每个key建立一个发布者,并让对应的订阅者订阅改发布者。
  4. 在元素的事件中,只更新仓库中的值,然后仓库的更新引起dom中的更新。即,不在元素的事件中直接更改dom。

实现

  1. 支持{{key}}和v-bind形式的绑定。
  2. 支持input的v-model属性。

mvvm_test's People

Contributors

xlldj avatar ginobilee avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.