美文网首页
双向数据绑定demo

双向数据绑定demo

作者: 大乔是个美少女 | 来源:发表于2019-03-05 16:56 被阅读0次

1.遍历dom树解析指令,寻找事件对应的属性名。
2.将属性名分类存储在订阅者信息中,并根据不同的dom生成对应的watch实例。
3.将数据绑定在视图上。
4.将视图数据更新到对应的watch实例。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>MVVM</title>
    <script>
        // 实现基本架构
        // 把模型的数据显示到视图
        // 把视图上的修改更新到模型上


        // 订阅数据变化,绑定更新,添加订阅者

        // 发布者
        class Vue {
            constructor(options){
                this.options = options;
                this.$data = options.data; // 获取数据
                this.$el = document.querySelector(options.el); // 获取对象
                this._directives = {} // 存放订阅者

                this.Observer(this.$data);
                this.Compile(this.$el);
            }

            // 劫持数据
            Observer(data){
                for (let key in data){
                    this._directives[key] = [];
                    let Val = data[key]
                    let _this = this._directives[key]
                    Object.defineProperty(this.$data, key, {
                        get: function(){
                            return Val;
                        },
                        set: function(newVal){
                            if (newVal !== Val) {
                                Val = newVal;

                                // 遍历订阅者实例,更新视图
                                _this.forEach( watch => {
                                    watch.update();
                                })
                            }
                        }
                    });
                }
            }

            // 解析指令
            Compile(el){
                let nodes = el.children;
                for (let i = 0; i < nodes.length; i++) {
                    let node = nodes[i];
                    if (node.children.length) {
                        this.Compile(node);
                    }
                    if (node.hasAttribute("v-text")) {
                        let attrVal = node.getAttribute("v-text");
                        this._directives[attrVal].push(new Watcher(node, attrVal, this, 'innerHTML'));
                    }
                    if (node.hasAttribute("v-model")) {
                        let attrVal = node.getAttribute("v-model");
                        this._directives[attrVal].push(new Watcher(node, attrVal, this, 'value'));
                        let _this = this;
                        node.addEventListener("input", (function(){
                            return function(){
                                // 更新视图同步到模型
                                _this.$data[attrVal] = node.value;
                            }
                        })());
                    }
                }
            }
        }

        // 订阅者
        class Watcher {
            constructor(el, vm, mySelf, attr){
                this.el = el; // 事件Dom
                this.vm = vm; // 绑定属性名
                this.mySelf = mySelf; // Vue实例
                this.attr = attr;
                this.update(); // 初始化数据
            }
            update(){
                this.el[this.attr] = this.mySelf.$data[this.vm];
            }
        }
    </script>
</head>
<body>
    <div id="app">
        <h1>数据响应式</h1>
        <div>
            <div v-text="myText"></div>
            <div v-text="myBox"></div>
            <input type="text" v-model="myBox">
        </div>
    </div>
    <script>
        const app = new Vue({
            el: "#app",
            data: {
                myText: "大吉大利,今晚吃鸡!",
                myBox: '局部'
            }
        })
    </script>
</body>
</html>

相关文章

  • 双向数据绑定demo

    1.遍历dom树解析指令,寻找事件对应的属性名。2.将属性名分类存储在订阅者信息中,并根据不同的dom生成对应的w...

  • Vue 中的双向数据绑定

    双向绑定 单向数据流 双向绑定 or 单向数据流 Vue 是单向数据流,不是双向绑定 Vue 的双向绑定是语法糖 ...

  • Vue之表单双向数据绑定和组件

    三、表单双向数据绑定和组件 目录:双向数据绑定、组件 1.双向数据绑定 1)什么是双向数据绑定Vue.js是一个M...

  • Vue双向数据绑定v-model

    v-model 数据双向绑定用作双向数据绑定 一、组件内部双向数据绑定 1、在实例的data中,设置content...

  • Vue入门(二)——数据绑定

    一、什么是双向数据绑定 双向数据绑定是Vue的核心功能之一。所谓双向数据绑定是指:HTML标签上的数据绑定到Vue...

  • [转] DataBinding 数据绑定

    数据绑定分为单项绑定和双向绑定两种。单向绑定上,数据的流向是单方面的,只能从代码流向 UI;双向绑定的数据是双向的...

  • 02Vue.js的数据绑定

    理解Vue的双向数据绑定 Vue有一个显著的地方就是它拥有双向数据绑定功能,那么何为双向数据绑定呢?双向是指:HT...

  • Vue和React数据绑定对比

    在数据绑定上来说,vue的特色是双向数据绑定,而在react中是单向数据绑定。 一 单向和双向数据绑定其实不是完全...

  • vue源码探究(第六弹)

    vue源码探究(第六弹) 继续之前的,差不多到最后一part了,数据的双向绑定。 双向数据绑定 双向数据绑定是建立...

  • vue双向数据绑定

    一、单选框的数据双向绑定 二、复选框的双向数据绑定

网友评论

      本文标题:双向数据绑定demo

      本文链接:https://www.haomeiwen.com/subject/raeouqtx.html