实现双向绑定,一般有下面这两种途径:
1.脏检查:代表angular
2.Object.defineProperty:代表avalon.js,vue.js
下面用defineProperty方法实现一个简单的双向绑定,测试地址
1.定义一个用于初始化的全局变量:
var avalon = {
define: function (obj) {
var result = {};
for (var key in obj) {
var bValue = obj[key];
Object.defineProperty(result, key, {
set: function (x) {
bValue = x;
pubSub.publish("change", key, x)},
get: function () {
return bValue;},
enumerable: true,
configurable: true});
}
return result
}
}
var vm = avalon.define({name: "das"})
通过define函数返回一个带有属性监视器的对象,然后初始化,由于这一过程只能在定义中实现,这也是avalon等框架强调先声明后使用的原因
var pubSub = {
callbacks: {},
on: function (msg, callback) {
this.callbacks[msg] = this.callbacks[msg] || []; this.callbacks[msg].push(callback); },
publish: function (msg) {
this.callbacks[msg] = this.callbacks[msg] || []
for (var i = 0, len = this.callbacks[msg].length; i < len; i++) { this.callbacks[msg][i].apply(this, arguments); } }};
这里运用的PubSub模式,学名叫发布订阅模式,简单来说就是消息发布者不直接向订阅者发布消息,而是发布到中介,而中介根据不同主题对消息进行过滤,并通知对该主题感兴趣的订阅者。
pubSub.on("change", function (evt, prop_name, new_val) { var elements = document.querySelectorAll("[" + bindName + "=" + prop_name + "]"), tag_name; for (var i = 0, len = elements.length; i < len; i++) { tag_name = elements[i].tagName.toLowerCase(); if (elements[i].value != new_val && elements[i].innerHTML != new_val) { if (tag_name === "input" || tag_name === "textarea" || tag_name === "select") { elements[i].value = new_val; } else { elements[i].innerHTML = new_val; } } }});pubSub.on("htmlChange", function (evt, prop_name, new_val) { vm[prop_name] = new_val});
最后注册两个事件,一个处理页面改动,一个处理变量改动
网友评论