'use strict';
var data = {
_name: '',
_age: 0,
_school: ''
};
var defineGetAndSet = (obj, prop) => {
Object.defineProperty(obj, prop, {
get: () => {
return obj['_' + prop];
},
set: (newValue) => {
obj['_' + prop] = newValue;
render(prop, newValue);
}
});
};
var directives = {
'x-value': function (newValue) {
this.setAttribute('value', newValue);
},
'x-text': function (newValue) {
this.innerHTML = newValue;
}
};
var els = [document.querySelectorAll('[x-value]'), document.querySelectorAll('[x-text]')];
var render = (prop, newValue) => {
els.forEach((item) => {
for (let i = 0, len1 = item.length; i < len1; i ++) {
let attrs = item[i].attributes;
for (let j = 0, len2 = attrs.length; j < len2; j ++) {
if (attrs[j].nodeName.indexOf('x-') !== -1 && attrs[j].nodeValue === prop) {
directives[attrs[j].nodeName].call(item[i], newValue);
}
}
}
});
};
defineGetAndSet(data, 'name');
defineGetAndSet(data, 'age');
defineGetAndSet(data, 'school');
data.name = 'xwj';
data.age = 20;
data.school = 'ouc';
var updateData = (event) => {
if (event.target.hasAttribute('x-value')) {
var prop = event.target.getAttribute('x-value');
data[prop] = event.target.value;
}
}
document.addEventListener('keyup', updateData);
document.addEventListener('change', updateData);