超浓缩 双向绑定

▼魔方 西西 提交于 2019-12-04 04:44:18
Object.prototype.bind = function (key, obj, prop, event) {
    var that = this
    if (this[key] == undefined) this[key] = '';
    var bc = undefined;
    if (!this.__bc__) {
        this.__bc__ = {};
        Object.defineProperty(this, "__bc__", { enumerable: false });
    }
    if (!this.__bc__[key]) {
        this.__bc__[key] = [];
        this.__bc__[key].value = this[key]
        bc = this.__bc__[key];
        Object.defineProperty(this, key, {
            get: function () {
                return bc.value
            },
            set: function (value) {
                if (bc.value == value) return;
                bc.forEach(function (l) { l(value); })
                bc.value = value
            }
        })
    }
    bc = this.__bc__[key];
    if (prop == undefined) prop = "value";
    if (obj[prop] == undefined) prop = 'innerHTML';
    if (event == undefined) event = 'change';
    if (typeof obj == 'string') {
        var els = document.querySelectorAll(obj);
        for (var i = 0; i < els.length; i++) {
            var el = els[i];
            bc.push(function (value) { el[prop] = value; });
            if (el.addEventListener) {
                el.addEventListener(event, function (e) {
                    that[key] = el[prop];
                })
            }
            el[prop] = bc.value;
        }
    } else if (typeof obj == 'function') {
        bc.push(obj);
        obj(bc.value);
    } else if (typeof obj == 'object' && obj.toString().indexOf("HTML") == 8) {
        bc.push(function (value) { obj[prop] = value; })
        obj[prop] = bc.value;
        if (obj.addEventListener) {
            obj.addEventListener(event, function (e) {
                that[key] = obj[prop];
            })
        }
    } else {
        console.log("obj = " + obj + " 不是 [selector,function,element] 中的任何一种,绑定失败!")
    }
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>测试项目</title>
    <script src="bind.js"></script>

</head>

<body>
    <input type="text" id="i1">
    <input type="text" id="i2">
    <script>
        var a = { value: 's' }

        a.bind('value', i1)
        a.bind('name', i2)
    </script>
</body>

</html>

可绑定到 selector,function,el

Object.prototype.bind = function (key, obj, prop, event) {
    var that = this
    if (this[key] == undefined) this[key] = '';
    var bc = undefined;
    if (!this.__bc__) {
        this.__bc__ = {};
        Object.defineProperty(this, "__bc__", { enumerable: false });
    }
    if (!this.__bc__[key]) {
        this.__bc__[key] = [];
        this.__bc__[key].value = this[key]
        bc = this.__bc__[key];
        Object.defineProperty(this, key, {
            get: function () {
                return bc.value
            },
            set: function (value) {
                if (bc.value == value) return;
                bc.forEach(function (l) { l(value); })
                bc.value = value
            }
        })
    }
    bc = this.__bc__[key];
    if (prop == undefined) prop = "value";
    if (obj[prop] == undefined) prop = 'innerHTML';
    if (event == undefined) event = 'change';
    if (typeof obj == 'string') {
        var els = document.querySelectorAll(obj);
        for (var i = 0; i < els.length; i++) {
            var el = els[i];
            bc.push(function (value) { el[prop] = value; });
            if (el.addEventListener) {
                el.addEventListener(event, function (e) {
                    that[key] = el[prop];
                })
            }
            el[prop] = bc.value;
        }
    } else if (typeof obj == 'function') {
        bc.push(obj);
        obj(bc.value);
    } else if (typeof obj == 'object' && obj.toString().indexOf("HTML") == 8) {
        bc.push(function (value) { obj[prop] = value; })
        obj[prop] = bc.value;
        if (obj.addEventListener) {
            obj.addEventListener(event, function (e) {
                that[key] = obj[prop];
            })
        }
    } else {
        console.log("obj = " + obj + " 不是 [selector,function,element] 中的任何一种,绑定失败!")
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!