-
Notifications
You must be signed in to change notification settings - Fork 0
/
data-bind.js
56 lines (52 loc) · 1.22 KB
/
data-bind.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class Leb {
constructor(options) {
this.init(options);
}
init(options) {
this.$options = options;3
this.$el = document.querySelector(options.el);
this.$data = options.data;
this.$methods = options.methods;
this.parseData(this.$data);
this.parseFun(this.$methods);
}
convert(key, val) {
Object.defineProperty(this.$data, key, {
enumerable: true,
configurable: true,
get: function () {
console.log(`get ${val}`);
return val;
},
set: function (newVal) {
console.log(`set ${newVal}`);
val = newVal;
}
});
}
parseData(obj) {
var value;
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
value = obj[key];
if(typeof value ==='object'){
this.parseData(value);
}
this.convert(key,value);
}
}
}
parseFun(methods) {
var _this = this;
for (let key in methods) {
if (methods.hasOwnProperty(key)) {
var fun = methods[key];
methods[key] = (function () {
return function () {
fun.apply(_this.$data, arguments);
}
})();
}
}
}
}