-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathapp.js
58 lines (54 loc) · 1.98 KB
/
app.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
57
58
(function() {
var elements = document.querySelectorAll('[data-tw-bind]'),
scope = {};
elements.forEach(function(element) {
//execute scope setter
if(element.type === 'text'|| element.type === 'textarea'){
var propToBind = element.getAttribute('data-tw-bind');
addScopeProp(propToBind);
element.onkeyup = function(){
scope[propToBind] = element.value;
}
};
//bind prop to elements
function addScopeProp(prop){
//add property if needed
if(!scope.hasOwnProperty(prop)){
//value to populate with newvalue
var value;
Object.defineProperty(scope, prop, {
set: function (newValue) {
value = newValue;
elements.forEach(function(element){
//change value to binded elements
if(element.getAttribute('data-tw-bind') === prop){
if(element.type && (element.type === 'text' ||
element.type === 'textarea')){
element.value = newValue;
}
else if(!element.type){
element.innerHTML = newValue;
}
}
});
},
get: function(){
return value;
},
enumerable: true
});
}
}
});
log = function() {
Object.keys(scope).forEach(function(key){
console.log(key + ': ' + scope[key]);
});
}
changeNameByCode = function() {
scope.name = 'name Changed by Code';
}
changeSurnameByCode = function() {
scope.surname = 'surname Changed by Code';
}
})();