-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinding.js
48 lines (41 loc) · 1.08 KB
/
binding.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
function TwoWayBinding() {
this.elms = parseDOM(document.body.children,{});
this.attachEvent();
}
TwoWayBinding.prototype.attachEvent = function () {
var elms = this.elms;
if(!Array.isArray(elms)){
elms = [elms];
}
elms.forEach(function (e) {
Object.keys(e).forEach(function (value) {
value.onkeypress = onKeyPressEvent.bind(this);
});
});
};
TwoWayBinding.prototype.get = function (key) {
if(this.elms.hasOwnProperty(key)){
return this.elms[key].value;
}
};
TwoWayBinding.prototype.set = function (key,value) {
if(this.elms.hasOwnProperty(key)){
this.elms[key].value = value;
}
};
var bind = new TwoWayBinding();
// Keypress function
function onKeyPressEvent(e) {
console.log(e.target);
}
function parseDOM(elms,output) {
return Array.prototype.reduce.call(elms,function (o,i) {
if(i.getAttribute("m-model")){
o[i.getAttribute("m-model")] = i;
}
if(i.children.length > 0){
parseDOM(i.children,o);
}
return o;
},output);
}