Created
November 24, 2017 14:34
-
-
Save amoe/2130dde546fa487c2875ccf011243dc9 to your computer and use it in GitHub Desktop.
one way binding in vanilla js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// proxies.js | |
const initialState = { | |
count: 0 | |
}; | |
const configuration = { | |
count: '#my-textarea' | |
}; | |
const state = makeReactive(initialState, configuration); | |
function makeReactive(state, configuration) { | |
handler = { | |
'set': function(target, property, value, receiver) { | |
console.log("setting property"); | |
const selector = configuration[property]; | |
const domTarget = document.querySelector(selector); | |
domTarget.textContent = value; | |
target[property] = value; | |
return true; | |
} | |
}; | |
var proxy = new Proxy(state, handler); | |
return proxy; | |
} | |
function increment(event) { | |
state.count += 1; | |
} | |
function wireInterface(event) { | |
const button = document.querySelector('#increment'); | |
button.addEventListener('click', increment); | |
} | |
document.addEventListener('DOMContentLoaded', wireInterface); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment