forked from hubit-brno/two-way-binding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
two-way-binding.html
146 lines (123 loc) · 3.8 KB
/
two-way-binding.html
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<!DOCTYPE html>
<html>
<head>
<style>
ul
{ list-style: none; margin: 0; padding: 0; }
li
{ margin: .5rem 0; }
p
{ margin: 1rem 0; }
p span
{ font-weight: bold; }
</style>
</head>
<body>
<form id="form">
<ul>
<li>
<label for="first-name-el">First name</label>
<input id="first-name-el"/>
</li>
<li>
<label for="last-name-el">Last name</label>
<input id="last-name-el"/>
</li>
</ul>
<p>
Full name: <span id="full-name-el">-</span>
</p>
</form>
<script>
window.onload = function () {
var firstNameInput = document.getElementById('first-name-el'),
lastNameInput = document.getElementById('last-name-el'),
fullNameSpan = document.getElementById('full-name-el'),
formEl = document.getElementById('form');
Element.prototype.twoWayBind = twoWayBindingFn;
/* --------------------------------
*
* firstNameInput.value = "Luke";
* fullNameSpan.innerText = "Luke Skyrunner";
*
* document.querySelectorAll('.someClass') => [el, el, el...]
*/
firstNameInput.onkeyup = function (value) {
var key = value.keyCode;
if (key == 13 || key == 9) //enter || tabulator
{
return false;
}
else {
window.person.firstName = firstNameInput.value;
updateSpan();
}
console.log(window.person);
};
lastNameInput.onkeyup = function (value) {
var key = value.keyCode;
if (key == 13 || key == 9) //enter || tabulator
{
return false;
}
else {
window.person.lastName = lastNameInput.value;
updateSpan();
}
console.log(window.person);
};
var person = {
// firstName: 'Luke',
// lastName: 'Skyrunner'
};
window.person = Object.create(Object.prototype, {
'firstName': {
enumerable: false,
get: function () {
return this._firstName;
},
set: function (value) {
this._firstName = value;
firstNameInput.value = value;
updateSpan();
}
},
'_firstName': {value: '', writable: true},
'lastName': {
enumerable: false,
get: function () {
return this._lastName;
},
set: function (value) {
this._lastName = value;
lastNameInput.value = value;
updateSpan();
}
},
'_lastName': {value: '', writable: true}
});
var updateSpan = function()
{
fullNameSpan.innerHTML = window.person.firstName + ' ' + window.person.lastName;
};
// Object.defineProperty(window.person, 'firstName', {
// enumerable: false,
// get: function() {
// console.log(this);
//// console.log(this.firstName);
//// return this.firstName;
// },
// set: function(value)
// {
// console.log(value);
// }
// });
function twoWayBindingFn(data) {
// 1. zmena v inputu by mela automaticky zmenit hodnotu patricne vlastnosti v objektu window.person
// 2. zmena vlastnosti v objektu window.person by mela zpusobit aktualizaci UI
// 3. pozor na cykly
};
};
</script>
</body>
</html>