Skip to content

Instantly share code, notes, and snippets.

@superMDguy
Last active September 28, 2017 13:33
Show Gist options
  • Save superMDguy/6eab7f1ea729ee6789cf14932d254e35 to your computer and use it in GitHub Desktop.
Save superMDguy/6eab7f1ea729ee6789cf14932d254e35 to your computer and use it in GitHub Desktop.
A minimalist reactive web app
<html>
<head>
<title>
Reactivity
</title>
<style>
div {
background: #989898;
color: #00FF11;
font-family: monospace;
font-size: 18px;
padding: 20px;
margin: 15px;
}
</style>
</head>
<body>
<input type="text" oninput="window.name = this.value" placeholder="Name">
<div>Hello, <span id="name"></span>!</div>
<input type="number" oninput="window.age = this.value" placeholder="Age">
<div>You are <span id="years"></span> years old.</div>
<script>
function bind(varName, bindToSelector) {
const bindToEl = document.querySelector(bindToSelector)
let val = bindToEl.innerText = window[varName]
Object.defineProperty(this, varName, {
get: function reactiveGetter() {
return val
},
set: function reactiveSetter(newVal) {
val = bindToEl.innerText = newVal
}
})
}
let name, age
bind('name', "#name");
bind('age', "#years");
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment